§
    ’©ºj¡D  ã                   óJ  — d dl Z d dlZd dlmZ d dlmZ d dlmZmZm	Z	 d dl
mZmZmZ d dlmZ d dlmZ ded	ed
efd„Z	 	 	 	 d)dedededededed
efd„Zded
efd„Zded
efd„Zdee         d
efd„Zde	eee         f         dee	eee         f                  ded         d
ee	eee         f         ee	eee         f                  f         fd „Z	 	 	 	 d)ded!e	eee         f         dedededed
efd"„Z	 	 	 	 	 	 d*de	eee         f         dee	eee         f                  ded         dedededed$eee                  d
ee         fd%„Z	 	 	 	 	 	 d+de	eee         f         dee	eee         f                  ded         d'ededededed
e	eeeef         f         fd(„ZdS ),é    N)ÚSequence)Úinf)ÚListÚOptionalÚUnion)ÚTensorÚstackÚtensor)ÚLiteral)Ú_validate_inputsÚ
preds_wordÚtarget_wordÚreturnc                 ó(   — t          | |k    ¦  «        S )a.  Distance measure used for substitutions/identity operation.

    Code adapted from https://github.com/rwth-i6/ExtendedEditDistance/blob/master/EED.py.

    Args:
        preds_word: hypothesis word string
        target_word: reference word string

    Return:
        0 for match, 1 for no match

    )Úint)r   r   s     úp/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/text/eed.pyÚ_distance_between_wordsr   e   s   € õ ˆz˜[Ò(Ñ)Ô)Ð)ó    ç       @ç333333Ó?çš™™™™™É?ç      ð?ÚhypÚrefÚalphaÚrhoÚdeletionÚ	insertionc                 ó–  ‡— dgt          | ¦  «        dz   z  }dgt          | ¦  «        dz   z  }d|d<   t          gt          | ¦  «        dz   z  }t          dt          |¦  «        dz   ¦  «        D �]}	t          t          | ¦  «        dz   ¦  «        D ]l}
|
dk    rVt          ||
dz
           |z   ||
dz
           t	          | |
dz
           ||	dz
           ¦  «        z   ||
         |z   ¦  «        ||
<   Œ^||
         dz   ||
<   Œm|                     t          |¦  «        ¦  «        }||xx         dz  cc<   ||	dz
           dk    r|||         z   Šˆfd„|D ¦   «         }|}t          gt          | ¦  «        dz   z  }�Œ|t          d„ |D ¦   «         ¦  «        z  }t          d|d         |z   t          t          |¦  «        ¦  «        |z   z  ¦  «        S )	a8  Compute extended edit distance score for two lists of strings: hyp and ref.

    Code adapted from: https://github.com/rwth-i6/ExtendedEditDistance/blob/master/EED.py.

    Args:
        hyp: A hypothesis string
        ref: A reference string
        alpha: optimal jump penalty, penalty for jumps between characters
        rho: coverage cost, penalty for repetition of characters
        deletion: penalty for deletion of character
        insertion: penalty for insertion or substitution of character

    Return:
        Extended edit distance score as float
    éÿÿÿÿé   r   ç        r   ú c                 ó0   •— g | ]}t          |‰¦  «        ‘ŒS © )Úmin)Ú.0ÚxÚjumps     €r   ú
<listcomp>z!_eed_function.<locals>.<listcomp>¥   s!   ø€ Ð7Ð7Ð7¨�˜A˜t™œÐ7Ð7Ð7r   c              3   ó*   K  — | ]}|d k    r|ndV — ŒdS )r   r!   Nr%   )r'   r(   s     r   ú	<genexpr>z _eed_function.<locals>.<genexpr>ª   s.   è è € ÐFÐF°˜a 1šf˜f˜˜¨!ÐFÐFÐFÐFÐFÐFr   )Úlenr   Úranger&   r   ÚindexÚsumÚfloat)r   r   r   r   r   r   Únumber_of_visitsÚrowÚnext_rowÚwÚiÚ	min_indexÚcoverager)   s                @r   Ú_eed_functionr9   u   sï  ø€ ð. �t�s 3™xœx¨!™|Ñ,Ðð ˆ%•3�s‘8”8˜a‘<Ñ
 €Cà€Cˆ�FÝˆu�˜C™œ 1™Ñ%€Hå�1•c˜#‘h”h ‘lÑ#Ô#ð *ñ *ˆÝ•s˜3‘x”x !‘|Ñ$Ô$ð 	+ð 	+ˆAØ�1ŠuˆuÝ!Ø˜Q ™U”O hÑ.Ø˜˜A™”JÕ!8¸¸QÀ¹U¼ÀSÈÈQÉÄZÑ!PÔ!PÑPØ˜”F˜YÑ&ñô �˜‘�ð " !œf s™l�˜‘�à—N’N¥3 x¡=¤=Ñ1Ô1ˆ	Ø˜Ð#Ð#Ô# qÑ(Ð#Ð#Ñ#ð ˆq�1‰uŒ:˜ÒÐØ˜8 IÔ.Ñ.ˆDØ7Ð7Ð7Ð7¨hÐ7Ñ7Ô7ˆHàˆÝ�5�C ™HœH q™LÑ)ˆ‰à•SÐFÐFÐ5EÐFÑFÔFÑFÔFÑF€Håˆq�3�r”7˜XÑ%­%µ°C±´©/¬/¸HÑ*DÑEÑFÔFÐFr   Úsentencec                 óp  — t          | t          ¦  «        s t          dt          | ¦  «        › d�¦  «        ‚|                      ¦   «         } g d¢}|D ]\  }}|                      ||¦  «        } Œg d¢}|D ]\  }}t          j        ||| ¦  «        } Œg d¢}|D ]\  }}|                      ||¦  «        } Œd| z   dz   S )zÄPreprocess english sentences.

    Copied from https://github.com/rwth-i6/ExtendedEditDistance/blob/master/util.py.

    Raises:
        ValueError: If input sentence is not of a type `str`.

    ú6Only strings allowed during preprocessing step, found ú instead))ú.z .)ú!z !)Ú?z ?)ú,z ,))z\s+r#   )z(\d) ([.,]) (\d)z\1\2\3)z#(Dr|Jr|Prof|Rev|Gen|Mr|Mt|Mrs|Ms) .z\1.))ze . g .ze.g.)zi . e .zi.e.)zU . S .zU.S.r#   )Ú
isinstanceÚstrÚ
ValueErrorÚtypeÚrstripÚreplaceÚreÚsub)r:   Úrules_interpunctionÚpatternÚreplacementÚrules_res        r   Ú_preprocess_enrN   ¯   s  € õ �h¥Ñ$Ô$ð lÝÐjÕRVÐW_ÑR`ÔR`ÐjÐjÐjÑkÔkÐkà�ŠÑ Ô €Hðð ð Ðð !4ð :ð :Ñˆ�Ø×#Ò# G¨[Ñ9Ô9ˆˆðð ð €Hð
 !)ð :ð :Ñˆ�Ý”6˜' ;°Ñ9Ô9ˆˆðð ð Ðð
 !4ð :ð :Ñˆ�Ø×#Ò# G¨[Ñ9Ô9ˆˆð �‰>˜CÑÐr   c                 ó¾   — t          | t          ¦  «        s t          dt          | ¦  «        › d�¦  «        ‚|                      ¦   «         } t          j        d| ¦  «        S )zÃPreprocess japanese sentences.

    Copy from https://github.com/rwth-i6/ExtendedEditDistance/blob/master/util.py.

    Raises:
        ValueError: If input sentence is not of a type `str`.

    r<   r=   ÚNFKC)rB   rC   rD   rE   rF   ÚunicodedataÚ	normalize)r:   s    r   Ú_preprocess_jarS   Ü   s]   € õ �h¥Ñ$Ô$ð lÝÐjÕRVÐW_ÑR`ÔR`ÐjÐjÐjÑkÔkÐkà�ŠÑ Ô €HåÔ  ¨Ñ2Ô2Ð2r   Úsentence_level_scoresc                 óž   — t          | ¦  «        dk    rt          d¦  «        S t          | ¦  «        t          t          | ¦  «        ¦  «        z  S )z°Reduction for extended edit distance.

    Args:
        sentence_level_scores: list of sentence-level scores as floats

    Return:
        average of scores as a tensor

    r   r"   )r-   r
   r0   )rT   s    r   Ú_eed_computerV   í   sI   € õ Ð Ñ!Ô! QÒ&Ð&Ý�c‰{Œ{ÐåÐ$Ñ%Ô%­­sÐ3HÑ/IÔ/IÑ(JÔ(JÑJÐJr   ÚpredsÚtargetÚlanguage)ÚenÚjac                 óÈ   ‡— t          | |¬¦  «        \  }} |dk    rt          Šn |dk    rt          Šnt          d|› �¦  «        ‚ˆfd„| D ¦   «         } ˆfd„|D ¦   «         }| |fS )au  Preprocess strings according to language requirements.

    Args:
        preds: An iterable of hypothesis corpus.
        target: An iterable of iterables of reference corpus.
        language: Language used in sentences. Only supports English (en) and Japanese (ja) for now. Defaults to en

    Return:
        Tuple of lists that contain the cleaned strings for target and preds

    Raises:
        ValueError: If a different language than ``'en'`` or ``'ja'`` is used
        ValueError: If length of target not equal to length of preds
        ValueError: If objects in reference and hypothesis corpus are not strings

    )Úhypothesis_corpusÚ
ref_corpusrZ   r[   z?Expected argument `language` to either be `en` or `ja` but got c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r%   r%   )r'   ÚpredÚpreprocess_functions     €r   r*   z)_preprocess_sentences.<locals>.<listcomp>  s%   ø€ Ð9Ð9Ð9¨4Ð Ð  Ñ&Ô&Ð9Ð9Ð9r   c                 ó,   •— g | ]}ˆfd „|D ¦   «         ‘ŒS )c                 ó&   •— g | ]} ‰|¦  «        ‘ŒS r%   r%   )r'   r   ra   s     €r   r*   z4_preprocess_sentences.<locals>.<listcomp>.<listcomp>  s%   ø€ Ð=Ð=Ð=¨CÐ"Ð" 3Ñ'Ô'Ð=Ð=Ð=r   r%   )r'   Ú	referencera   s     €r   r*   z)_preprocess_sentences.<locals>.<listcomp>  s.   ø€ ÐVÐVÐVÀ)Ð=Ð=Ð=Ð=°9Ð=Ñ=Ô=ÐVÐVÐVr   )r   rN   rS   rD   )rW   rX   rY   ra   s      @r   Ú_preprocess_sentencesre   ý   s™   ø€ õ, %°uÈÐPÑPÔP�M€FˆEð �4ÒÐÝ,ÐÐØ	�TÒ	Ð	Ý,ÐÐåÐeÐ[cÐeÐeÑfÔfÐfà9Ð9Ð9Ð9°5Ð9Ñ9Ô9€EØVÐVÐVÐVÈvÐVÑVÔV€Fà�&ˆ=Ðr   Útarget_wordsc           	      óp   — t           }|D ]}t          | |||||¦  «        }||k     r|}Œt          |¦  «        S )aì  Compute scores for ExtendedEditDistance.

    Args:
        target_words: An iterable of reference words
        preds_word: A hypothesis word
        alpha: An optimal jump penalty, penalty for jumps between characters
        rho: coverage cost, penalty for repetition of characters
        deletion: penalty for deletion of character
        insertion: penalty for insertion or substitution of character

    Return:
        best_score: best (lowest) sentence-level score as a Tensor

    )r   r9   r
   )	r   rf   r   r   r   r   Ú
best_scorerd   Úscores	            r   Ú_compute_sentence_statisticsrj   #  sN   € õ, €Jà!ð ð ˆ	Ý˜j¨)°U¸CÀÈ9ÑUÔUˆØ�:ÒÐØˆJøå�*ÑÔÐr   rZ   Úsentence_eedc           	      ó  — t          | ||¦  «        \  } }|€g }dt          | ¦  «        t          |d         ¦  «        fv r|S t          | |¦  «        D ].\  }}	t          ||	||||¦  «        }
|                     |
¦  «         Œ/|S )a—  Compute scores for ExtendedEditDistance.

    Args:
        preds: An iterable of hypothesis corpus
        target: An iterable of iterables of reference corpus
        language: Language used in sentences. Only supports English (en) and Japanese (ja) for now. Defaults to en
        alpha: optimal jump penalty, penalty for jumps between characters
        rho: coverage cost, penalty for repetition of characters
        deletion: penalty for deletion of character
        insertion: penalty for insertion or substitution of character
        sentence_eed: list of sentence-level scores

    Return:
        individual sentence scores as a list of Tensors

    Nr   )re   r-   Úziprj   Úappend)rW   rX   rY   r   r   r   r   rk   Ú
hypothesisrf   ri   s              r   Ú_eed_updaterp   C  s    € õ4 *¨%°¸ÑBÔB�M€Eˆ6àÐØˆð 	�S�‰ZŒZ�˜V AœY™œÐ(Ð(Ð(ØÐå$'¨¨vÑ$6Ô$6ð #ð #Ñ ˆ
�LÝ,¨Z¸ÀuÈcÐS[Ð]fÑgÔgˆØ×Ò˜EÑ"Ô"Ð"Ð"àÐr   FÚreturn_sentence_level_scorec           	      ó2  — t          g d¢||||g¦  «        D ]H\  }}	t          |	t          ¦  «        rt          |	t          ¦  «        r|	dk     rt          d|› d�¦  «        ‚ŒIt	          | ||||||¦  «        }
t          |
¦  «        }|r|t          |
¦  «        fS |S )uX  Compute extended edit distance score (`ExtendedEditDistance`_) [1] for strings or list of strings.

    The metric utilises the Levenshtein distance and extends it by adding a jump operation.

    Args:
        preds: An iterable of hypothesis corpus.
        target: An iterable of iterables of reference corpus.
        language: Language used in sentences. Only supports English (en) and Japanese (ja) for now. Defaults to en
        return_sentence_level_score: An indication of whether sentence-level EED score is to be returned.
        alpha: optimal jump penalty, penalty for jumps between characters
        rho: coverage cost, penalty for repetition of characters
        deletion: penalty for deletion of character
        insertion: penalty for insertion or substitution of character

    Return:
        Extended edit distance score as a tensor

    Example:
        >>> from torchmetrics.functional.text import extended_edit_distance
        >>> preds = ["this is the prediction", "here is an other sample"]
        >>> target = ["this is the reference", "here is another one"]
        >>> extended_edit_distance(preds=preds, target=target)
        tensor(0.3078)

    References:
        [1] P. Stanchev, W. Wang, and H. Ney, â€œEED: Extended Edit Distance Measure for Machine Translationâ€�,
        submitted to WMT 2019. `ExtendedEditDistance`_

    )r   r   r   r   r   zParameter `z)` is expected to be a non-negative float.)rm   rB   r1   rD   rp   rV   r	   )rW   rX   rY   rq   r   r   r   r   Ú
param_nameÚparamrT   Úaverages               r   Úextended_edit_distancerv   m  sË   € õP !Ð!JÐ!JÐ!JÈUÐTWÐYaÐclÐLmÑnÔnð bð bÑˆ
�EÝ˜%¥Ñ'Ô'ð 	b­J°u½eÑ,DÔ,Dð 	bÈÐQRÊÈÝÐ`¨:Ð`Ð`Ð`ÑaÔaÐaøå'¨¨v°xÀÈÈXÐW`ÑaÔaÐåÐ0Ñ1Ô1€Gà"ð 5Ø�Ð3Ñ4Ô4Ð4Ð4Ø€Nr   )r   r   r   r   )rZ   r   r   r   r   N)rZ   Fr   r   r   r   ) rH   rQ   Úcollections.abcr   Úmathr   Útypingr   r   r   Útorchr   r	   r
   Útyping_extensionsr   Ú#torchmetrics.functional.text.helperr   rC   r   r   r1   r9   rN   rS   rV   Útuplere   rj   rp   Úboolrv   r%   r   r   ú<module>r      s0  ððr 
€	€	€	Ø Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ð 'Ø %Ð %Ð %Ð %Ð %Ð %à @Ð @Ð @Ð @Ð @Ð @ð*¨ð *¸#ð *À#ð *ð *ð *ð *ð& ØØØð7Gð 7GØ	ð7Gà	ð7Gð ð7Gð 
ð	7Gð
 ð7Gð ð7Gð ð7Gð 7Gð 7Gð 7Gðt* ˜Sð *  Sð * ð * ð * ð * ðZ3˜Sð 3 Sð 3ð 3ð 3ð 3ð"K¨¨V¬ð K¸ð Kð Kð Kð Kð #Ø��h˜s”mÐ#Ô$ð#à�U˜3 ¨¤Ð-Ô.Ô/ð#ð �jÔ!ð#ð ˆ5��h˜s”mÐ#Ô$ h¨u°S¸(À3¼-Ð5GÔ/HÔ&IÐIÔJð	#ð #ð #ð #ðR ØØØðð Øðà˜˜X cœ]Ð*Ô+ðð ðð 
ð	ð
 ðð ðð ðð ð ð ðF %)ØØØØØ+/ð'ð 'Ø��h˜s”mÐ#Ô$ð'à�U˜3 ¨¤Ð-Ô.Ô/ð'ð �jÔ!ð'ð ð	'ð
 
ð'ð ð'ð ð'ð ˜4 œ<Ô(ð'ð 
ˆ&„\ð'ð 'ð 'ð 'ðZ %)Ø(-ØØØØð2ð 2Ø��h˜s”mÐ#Ô$ð2à�U˜3 ¨¤Ð-Ô.Ô/ð2ð �jÔ!ð2ð "&ð	2ð
 ð2ð 
ð2ð ð2ð ð2ð ˆ6�5˜ ˜Ô(Ð(Ô)ð2ð 2ð 2ð 2ð 2ð 2r   