U
    -eS                     @   s|   d Z ddlmZmZmZmZ ddlmZmZm	Z	 dgZ
deeef eeedddZdee ee ee	 eed	d
dZdS )z_
Utility method to visualize the alignment between one or more reference and hypothesis
pairs.
    )DictListTupleUnion)CharacterOutput
WordOutputAlignmentChunkvisualize_alignmentT)outputshow_measuresskip_correctreturnc                 C   s  | j }| j}| j}t| t}d}tt|||D ]`\}\}	}
}|r^t|dkr^|d jdkr^q0|d|d  d7 }|t	|	|
|| d7 }|d7 }q0|rv|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 }n`|d| jd dd7 }|d| jd dd7 }|d| jd dd7 }|d| jd dd7 }n|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)
referencesZ
hypothesesZ
alignments
isinstancer   	enumerateziplentype_construct_comparison_stringZsubstitutionsZ	deletions
insertionshitsZcerZmerZwilZwipZwer)r
   r   r   r   
hypothesisZ	alignmentZis_cerZ	final_stridxgthpchunks r%   P/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/jiwer/alignment.pyr	       s<    D
   
F)	referencer    opsr   r   c                    s  d}d}d}|D ]}|j dks*|j dkr^| |j|j }||j|j }	|j dkrXdnd n||j dkr| |j|j }d	d
 tt|D }	d nF|j dkr||j|j }	dd
 tt|	D }d ntd|j   fdd
tt|D }
t||	|
D ]\}}}t	t|t|t|}|dkr>d
dg| }n|dkrXd
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 d S )NzREF: zHYP: z     r   
substituter   sdeletec                 S   s   g | ]}d qS *r%   .0_r%   r%   r&   
<listcomp>   s     z0_construct_comparison_string.<locals>.<listcomp>dinsertc                 S   s   g | ]}d qS r,   r%   r.   r%   r%   r&   r1      s     izunparseable op name=c                    s   g | ]} qS r%   r%   r.   Zop_charr%   r&   r1      s     r-   r   >r   r   )r   Zref_start_idxZref_end_idxZhyp_start_idxZhyp_end_idxranger   
ValueErrorr   maxjoinupper)r'   r    r(   r   Zref_strZhyp_strZop_stroprefZhypZop_charsrfr#   cZstr_lenr%   r5   r&   r      sD    




.r   N)TT)F)__doc__typingr   r   r   r   Zjiwer.processr   r   r   __all__boolstrr	   r   r%   r%   r%   r&   <module>   s&     
m 