o
    !wiE                     @   s   d Z ddlmZmZ ddlmZmZmZ dgZ		ddeeef de	de	d	e
fd
dZ	ddee
 dee
 dee de	d	e
f
ddZdS )z_
Utility method to visualize the alignment between one or more reference and hypothesis
pairs.
    )ListUnion)CharacterOutput
WordOutputAlignmentChunkvisualize_alignmentToutputshow_measuresskip_correctreturnc                 C   s  | j }| j}| j}t| t}d}tt|||D ]0\}\}	}
}|r/t|dkr/|d jdkr/q|d|d  d7 }|t	|	|
|| d7 }|d7 }q|r|dt| d7 }|d	| j
 d
7 }|d| j d
7 }|d| j d
7 }|d| j d7 }|r|d| jd dd7 }|S |d| jd dd7 }|d| jd dd7 }|d| jd dd7 }|d| jd dd7 }|S |dd }|S )a  
    Visualize the output of [jiwer.process_words][process.process_words] and
    [jiwer.process_characters][process.process_characters]. The visualization
    shows the alignment between each processed reference and hypothesis pair.
    If `show_measures=True`, the output string will also contain all measures in the
    output.

    Args:
        output: The processed output of reference and hypothesis pair(s).
        show_measures: If enabled, the visualization will include measures like the WER
                       or CER
        skip_correct: If enabled, the visualization will exclude correct reference and hypothesis pairs

    Returns:
        (str): The visualization as a string

    Example:
        This code snippet
        ```python
        import jiwer

        out = jiwer.process_words(
            ["short one here", "quite a bit of longer sentence"],
            ["shoe order one", "quite bit of an even longest sentence here"],
        )

        print(jiwer.visualize_alignment(out))
        ```
        will produce this visualization:
        ```txt
        sentence 1
        REF:    # short one here
        HYP: shoe order one    *
                I     S        D

        sentence 2
        REF: quite a bit of  #    #  longer sentence    #
        HYP: quite * bit of an even longest sentence here
                   D         I    I       S             I

        number of sentences: 2
        substitutions=2 deletions=2 insertions=4 hits=5

        mer=61.54%
        wil=74.75%
        wip=25.25%
        wer=88.89%
        ```

        When `show_measures=False`, only the alignment will be printed:

        ```txt
        sentence 1
        REF:    # short one here
        HYP: shoe order one    *
                I     S        D

        sentence 2
        REF: quite a bit of  #    #  longer sentence    #
        HYP: quite * bit of an even longest sentence here
                   D         I    I       S             I
        ```
        r   equalz	sentence 
)include_space_seperatorznumber of sentences: zsubstitutions= z
deletions=zinsertions=zhits=z
cer=d   z.2fz%
z
mer=%z
wil=z
wip=z
wer=N)
references
hypotheses
alignments
isinstancer   	enumerateziplentype_construct_comparison_stringsubstitutions	deletions
insertionshitscermerwilwipwer)r   r	   r
   r   
hypothesis	alignmentis_cer	final_stridxgthpchunks r/   L/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/jiwer/alignment.pyr      s:   D



F	referencer'   opsr   c                    s  d}d}d}|D ]}|j dks|j dkr.| |j|j }||j|j }	|j dkr+dnd n>|j dkrI| |j|j }d	d
 tt|D }	d n#|j dkrd||j|j }	dd
 tt|	D }d ntd|j   fdd
tt|D }
t||	|
D ]U\}}}t	t|t|t|}|dkrd
dg| }n|dkrd
dg| }||d| 7 }||d| 7 }|| d| 7 }|r|d7 }|d7 }|d7 }qq|r|d d  d|d d  d|d d  dS | d| d| dS )NzREF: zHYP: z     r   
substituter   sdeletec                 S      g | ]}d qS *r/   .0_r/   r/   r0   
<listcomp>       z0_construct_comparison_string.<locals>.<listcomp>dinsertc                 S   r6   r7   r/   r9   r/   r/   r0   r<      r=   izunparseable op name=c                    s   g | ]} qS r/   r/   r9   op_charr/   r0   r<      r=   r8   r   >r   r   )r   ref_start_idxref_end_idxhyp_start_idxhyp_end_idxranger   
ValueErrorr   maxjoinupper)r1   r'   r2   r   ref_strhyp_strop_stroprefhypop_charsrfr-   cstr_lenr/   rA   r0   r      sH   

.r   N)TT)F)__doc__typingr   r   jiwer.processr   r   r   __all__boolstrr   r   r/   r/   r/   r0   <module>   s6   

m