Ë
    þÍ:jlB  ã            	       ó^  — d dl Z d dlmZ d dlmZmZ d dlmZ dZdZ	 e
d«      Ze G d„ d	ee«      «       Z G d
„ d«      Zdeee   eee      f   deeee   f   deeee      ee   f   fd„Zdee   dee   de
fd„Zdeedf   deedf   fd„Zdeedf   deee
e
f   ee
   ee
   f   fd„Zy)é    N)ÚSequence)ÚEnumÚunique)ÚUnioné   i'  g €à7yÃACc                   ó$   — e Zd ZdZdZdZdZdZdZy)Ú_EditOperationsz1Enumerations for the Levenhstein edit operations.ÚinsertÚdeleteÚ
substituteÚnothingÚ	undefinedN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	OP_INSERTÚ	OP_DELETEÚOP_SUBSTITUTEÚ
OP_NOTHINGÚOP_UNDEFINED© ó    úx/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchmetrics/functional/text/helper.pyr	   r	   ,   s   „ á;à€IØ€IØ €MØ€JØ�Lr   r	   c                   óš  — e Zd ZdZ	 ddee   dedededdf
d„Zd	ee   deeee	d
f   f   fd„Z
d	ee   dedeeeee	f         deeeeeee	f         ee	d
f   f   fd„Zdedeeeee	f         dee	d
f   fd„Zd	ee   deeeee	f         ddfd„Zd	ee   deeeeeee	f         f   fd„Zdedeeee	f      fd„Zdedeeee	f      fd„Zy)Ú_LevenshteinEditDistancea.  A convenience class for calculating the Levenshtein edit distance.

    Class will cache some intermediate values to hasten the calculation. The implementation follows the implementation
    from https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/metrics/lib_ter.py,
    where the most of this implementation is adapted and copied from.

    Args:
        reference_tokens: list of reference tokens
        op_insert: cost of insertion operation
        op_delete: cost of deletion operation
        op_substitute: cost of substitution operation

    Úreference_tokensÚ	op_insertÚ	op_deleteÚop_substituteÚreturnNc                 óœ   — || _         t        |«      | _        i | _        d| _        || _        || _        || _        d| _        t        | _
        y )Nr   )r   ÚlenÚreference_lenÚcacheÚ
cache_sizer   r   r    Ú
op_nothingÚ_INT_INFINITYÚop_undefined)Úselfr   r   r   r    s        r   Ú__init__z!_LevenshteinEditDistance.__init__F   sM   € ð !1ˆÔÜ Ð!1Ó2ˆÔà13ˆŒ
ØˆŒà"ˆŒØ"ˆŒØ*ˆÔØˆŒÜ)ˆÕr   Úprediction_tokens.c                 ó„   — | j                  |«      \  }}| j                  |||«      \  }}}| j                  ||«       ||fS )a(  Calculate edit distance between self._words_ref and the hypothesis. Uses cache to skip some computations.

        Args:
            prediction_tokens: A tokenized predicted sentence.

        Return:
            A tuple of a calculated edit distance and a trace of executed operations.

        )Ú_find_cacheÚ_levenshtein_edit_distanceÚ
_add_cache)r*   r,   Ústart_positionÚcached_edit_distanceÚedit_distance_intÚedit_distanceÚtraces          r   Ú__call__z!_LevenshteinEditDistance.__call__U   sW   € ð 04×/?Ñ/?Ð@QÓ/RÑ,ˆÐ,à26×2QÑ2QØ˜~Ð/Có3
Ñ/Ð˜=¨%ð 	�‰Ð)¨=Ô9à  %Ð'Ð'r   Úprediction_startr%   c                 ó¾  — t        |«      }t        ||z
  «      D �cg c]&  }t        | j                  | j                  «      «      ‘Œ( }}||z   }|r| j                  |z  nd}|dz  t
        kD  rt        j                  |dz  t
        z   «      nt
        }	t        |dz   |dz   «      D �]†  }
t        j                  |
|z  «      }t        d||	z
  «      }|
|k(  r| j                  dz   nt        | j                  dz   ||	z   «      }t        ||«      D �]  }|dk(  r2||
dz
     |   d   | j                  z   t        j                  f||
   |<   Œ;||
dz
     | j                  |dz
     k(  r| j                  }t        j                   }n| j"                  }t        j$                  }||
dz
     |dz
     d   |z   |f||
dz
     |   d   | j                  z   t        j                  f||
   |dz
     d   | j&                  z   t        j(                  ff}|D ]  \  }}||
   |   d   |kD  sŒ||f||
   |<   Œ  �Œ �Œ‰ | j+                  ||«      }|d   d   d   |t        |«      d |fS c c}w )a•  Dynamic programming algorithm to compute the Levenhstein edit distance.

        Args:
            prediction_tokens: A tokenized predicted sentence.
            prediction_start: An index where a predicted sentence to be considered from.
            cache: A cached Levenshtein edit distance.

        Returns:
            Edit distance between the predicted sentence and the reference sentence

        g      ð?é   é   r   éÿÿÿÿN)r#   ÚrangeÚlistÚ_get_empty_rowr$   Ú_BEAM_WIDTHÚmathÚceilÚfloorÚmaxÚminr   r	   r   r   r'   r   r    r   r   r   Ú
_get_trace)r*   r,   r7   r%   Úprediction_lenÚ_Ú
empty_rowsr4   Úlength_ratioÚ
beam_widthÚiÚpseudo_diagÚmin_jÚmax_jÚjÚcost_substituteÚoperation_substituteÚ
operationsÚoperation_costÚoperation_namer5   s                        r   r/   z3_LevenshteinEditDistance._levenshtein_edit_distancej   sË  € ô" Ð.Ó/ˆô DIÈÐZjÑIjÓCkö?
Ø>?ŒD�×$Ñ$ T×%7Ñ%7Ó8Õ9ð?
ˆ
ð ?
ð BGÈÑASˆÙ>O�t×)Ñ)¨NÒ:ÐUXˆð COÐQRÑBRÔU`ÒB`”T—Y‘Y˜|¨aÑ/´+Ñ=Ô>Ôfqˆ
ô Ð'¨!Ñ+¨^¸aÑ-?Ó@ó !	QˆAÜŸ*™* Q¨Ñ%5Ó6ˆKÜ˜˜;¨Ñ3Ó4ˆEà*+¨~Ò*=�×"Ñ" QÒ&Ä3Àt×GYÑGYÐ\]ÑG]Ð_jÐmwÑ_wÓCxð ô ˜5 %Ó(ó Q�Ø˜’6à% a¨!¡eÑ,¨QÑ/°Ñ2°T·^±^ÑCÜ'×1Ñ1ð+�M !Ñ$ QÒ'ð
 )¨¨Q©Ñ/°4×3HÑ3HÈÈQÉÑ3OÒOØ*.¯/©/˜Ü/>×/IÑ/IÑ,à*.×*<Ñ*<˜Ü/>×/LÑ/LÐ,ð ' q¨1¡uÑ-¨a°!©eÑ4°QÑ7¸/ÑIÐK_Ð`Ø& q¨1¡uÑ-¨aÑ0°Ñ3°d·n±nÑDÄo×F_ÑF_Ð`Ø& qÑ)¨!¨a©%Ñ0°Ñ3°d·n±nÑDÄo×F_ÑF_Ð`ð"�Jð ;Eò QÑ6˜¨Ø(¨Ñ+¨AÑ.¨qÑ1°NÓBØ2@À.Ð2P˜M¨!Ñ,¨QÒ/òQò1Qð!	QðF —‘ °Ó>ˆà˜RÑ  Ñ$ QÑ'¨´s¸5³z°|Ð)DÀeÐKÐKùò_?
s   œ+IrF   r4   c                 óN  — d}|}| j                   }|dkD  s|dkD  rŠ||   |   d   }|g|¢­}|t        j                  t        j                  fv r|dz  }|dz  }n@|t        j                  k(  r|dz  }n'|t        j
                  k(  r|dz  }nt        d|›�«      ‚|dkD  rŒ„|dkD  rŒŠ|S )a@  Get a trace of executed operations from the edit distance matrix.

        Args:
            prediction_len: A length of a tokenized predicted sentence.
            edit_distance:
                A matrix of the Levenshtedin edit distance. The element part of the matrix is a tuple of an edit
                operation cost and an edit operation itself.

        Return:
            A trace of executed operations returned as a tuple of `_EDIT_OPERATIONS` enumerates.

        Raises:
            ValueError:
                If an unknown operation has been applied.

        r   r   r:   úUnknown operation )r$   r	   r   r   r   r   Ú
ValueError)r*   rF   r4   r5   rK   rO   Ú	operations          r   rE   z#_LevenshteinEditDistance._get_trace®   sÉ   € ð& .0ˆØˆØ×Ñˆà�!Še�q˜1’uØ% aÑ(¨Ñ+¨AÑ.ˆIØÐ' Ñ'ˆEØœ_×:Ñ:¼O×<VÑ<VÐWÑWØ�Q‘�Ø�Q‘‘Øœo×7Ñ7Ò7Ø�Q‘‘Øœo×7Ñ7Ò7Ø�Q‘‘ä Ð#5°i°]Ð!CÓDÐDð �!‹e�q˜1“uð ˆr   c                 ó<  — | j                   t        k\  ry| j                  }t        |«      t        |«      z
  }t	        |«      D ]  }|||      d   }Œ t        ||d |«      D ]8  \  }}||vr%i t        |«      f||<   | xj                   dz  c_         ||   }|d   }Œ: y)a,  Add newly computed rows to cache.

        Since edit distance is only calculated on the hypothesis suffix that was not in cache, the number of rows in
        `edit_distance` matrx may be shorter than hypothesis length. In that case we skip over these initial words.

        Args:
            prediction_tokens: A tokenized predicted sentence.
            edit_distance:
                A matrix of the Levenshtedin edit distance. The element part of the matrix is a tuple of an edit
                operation cost and an edit operation itself.

        Nr   r:   )r&   Ú_MAX_CACHE_SIZEr%   r#   r<   ÚzipÚtuple)	r*   r,   r4   ÚnodeÚskip_numrK   ÚwordÚrowÚvalues	            r   r0   z#_LevenshteinEditDistance._add_cacheÔ   s»   € ð �?‰?œoÒ-Øà�z‰zˆô Ð(Ó)¬C°Ó,>Ñ>ˆô �x“ò 	1ˆAØÐ)¨!Ñ,Ñ-¨aÑ0‰Dð	1ô Ð.¨x¨yÐ9¸=ÓIò 	‰IˆD�#Ø˜4ÑØ ¤%¨£*Ð-��T‘
Ø—’ 1Ñ$•Ø˜‘JˆEØ˜‘8‰Dñ	r   c                 óº   — | j                   }d}| j                  | j                  «      g}|D ])  }||v r|dz  }||   \  }}|j                  |«       Œ& ||fS  ||fS )a+  Find the already calculated rows of the Levenshtein edit distance metric.

        Args:
            prediction_tokens: A tokenized predicted sentence.

        Return:
            A tuple of a start hypothesis position and `edit_distance` matrix.

            prediction_start: An index where a predicted sentence to be considered from.
            edit_distance:
                A matrix of the cached Levenshtedin edit distance. The element part of the matrix is a tuple of an edit
                operation cost and an edit operation itself.

        r   r:   )r%   Ú_get_initial_rowr$   Úappend)r*   r,   r]   r1   r4   r_   r`   s          r   r.   z$_LevenshteinEditDistance._find_cacheõ   s‚   € ð �z‰zˆØˆØBF×BWÑBWÐX\×XjÑXjÓBkÐAlˆØ%ò 	ˆDØ�t‰|Ø !Ñ#�Ø  ™J‘	��cØ×$Ñ$ SÕ)àà˜}Ð,Ð,ð	ð ˜}Ð,Ð,r   Úlengthc                 óZ   — t        | j                  «      t        j                  fg|dz   z  S )a  Precomputed empty matrix row for Levenhstein edit distance.

        Args:
            length: A length of a tokenized sentence.

        Return:
            A list of tuples containing infinite edit operation costs and yet undefined edit operations.

        r:   )Úintr)   r	   r   )r*   re   s     r   r>   z'_LevenshteinEditDistance._get_empty_row  s-   € ô �T×&Ñ&Ó'¬×)EÑ)EÐFÐGÈ6ÐTUÉ:ÑVÐVr   c                 ó~   — t        |dz   «      D �cg c]!  }|| j                  z  t        j                  f‘Œ# c}S c c}w )a*  First row corresponds to insertion operations of the reference, so 1 edit operation per reference word.

        Args:
            length: A length of a tokenized sentence.

        Return:
            A list of tuples containing edit operation costs of insert and insert edit operations.

        r:   )r<   r   r	   r   )r*   re   rK   s      r   rc   z)_LevenshteinEditDistance._get_initial_row  s8   € ô JOÈvÐXYÉzÓIZÖ[ÀA��T—^‘^Ñ#¤_×%>Ñ%>Ò?Ò[Ð[ùÒ[s   ‘&:)r:   r:   r:   )r   r   r   r   r=   Ústrrg   r+   r\   r	   r6   r/   rE   r0   r.   r>   rc   r   r   r   r   r   7   sÙ  „ ñð ijñ*Ø $ S¡	ð*Ø69ð*ØJMð*Øbeð*à	ó*ð(¨$¨s©)ð (¸¸cÀ5ÈÐZ]ÐI]ÑC^Ð>^Ñ8_ó (ð*BLà ™9ðBLð ðBLð �D˜˜s OÐ3Ñ4Ñ5Ñ6ð	BLð
 
ˆs�D˜˜e C¨Ð$8Ñ9Ñ:Ñ;¸UÀ?ÐTWÐCWÑ=XÐXÑ	YóBLðH$Ø!ð$Ø26°t¸EÀ#ÀÐBVÑ<WÑ7XÑ2Yð$à	ˆ Ð#Ñ	$ó$ðL¨D°©Ið ÀdÈ4ÐPUÐVYÐ[jÐVjÑPkÑKlÑFmð Ðrvó ðB-¨T°#©Yð -¸5ÀÀdÈ4ÐPUÐVYÐ[jÐVjÑPkÑKlÑFmÐAmÑ;nó -ð8
W Sð 
W¨T°%¸¸_Ð8LÑ2MÑ-Nó 
Wð
\ sð 
\¨t°E¸#¸Ð:NÑ4OÑ/Pô 
\r   r   Ú
ref_corpusÚhypothesis_corpusr!   c                 ó>  — t        |t        «      r|g}t        d„ | D «       «      r"t        |«      dk(  r| gn| D �cg c]  }|g‘Œ c}} |rLt        d„ | D «       «      r:t        | «      t        |«      k7  r#t	        dt        | «      › dt        |«      › �«      ‚| |fS c c}w )aô  Check and update (if needed) the format of reference and hypothesis corpora for various text evaluation metrics.

    Args:
        ref_corpus: An iterable of iterables of reference corpus.
        hypothesis_corpus: An iterable of hypothesis corpus.

    Return:
        ref_corpus: An iterable of iterables of reference corpus.
        hypothesis_corpus: An iterable of hypothesis corpus.

    Raises:
        ValueError:
            If length of `ref_corpus` and `hypothesis_corpus` differs.

    c              3   ó<   K  — | ]  }t        |t        «      –— Œ y ­w©N)Ú
isinstanceri   ©Ú.0Úrefs     r   ú	<genexpr>z#_validate_inputs.<locals>.<genexpr>A  s   è ø€ Ò
6 CŒ:�cœ3×Ñ
6ùs   ‚r:   c              3   ó    K  — | ]  }|–— Œ y ­wrn   r   rp   s     r   rs   z#_validate_inputs.<locals>.<genexpr>D  s   è ø€ Ò ;¨¤Ñ ;ùs   ‚zCorpus has different size z != )ro   ri   Úallr#   rW   )rj   rk   rr   s      r   Ú_validate_inputsrv   *  s§   € ô& Ð#¤SÔ)Ø.Ð/Ðô Ñ
6¨:Ô
6Ô6Ü%(Ð):Ó%;¸qÒ%@�j‘\ÐXbÖFcÐQTÈÂuÒFcˆ
áœSÑ ;°
Ô ;Ô;ÄÀJÃÔSVÐWhÓSiÒ@iÜÐ5´c¸*³oÐ5FÀdÌ3ÐO`ÓKaÐJbÐcÓdÐdàÐ(Ð(Ð(ùò Gds   »
Br,   r   c           	      ó<  — t        t        | «      dz   «      D �cg c]  }dgt        |«      dz   z  ‘Œ }}t        t        | «      dz   «      D ]
  }|||   d<   Œ t        t        |«      dz   «      D ]
  }||d   |<   Œ t        dt        | «      dz   «      D ]y  }t        dt        |«      dz   «      D ]\  }| |dz
     ||dz
     k(  r||dz
     |dz
     ||   |<   Œ)t        ||dz
     |   ||   |dz
     ||dz
     |dz
     «      dz   ||   |<   Œ^ Œ{ |d   d   S c c}w )a  Dynamic programming algorithm to compute the edit distance.

    Args:
        prediction_tokens: A tokenized predicted sentence
        reference_tokens: A tokenized reference sentence
    Returns:
        Edit distance between the predicted sentence and the reference sentence

    r:   r   r;   )r<   r#   rD   )r,   r   rG   ÚdprK   rO   s         r   Ú_edit_distancery   J  sk  € ô 6;¼3Ð?PÓ;QÐTUÑ;UÓ5VÖ	W°ˆ1ˆ#”Ð%Ó&¨Ñ*Ó
+Ð	W€BÐ	WÜ”3Ð(Ó)¨AÑ-Ó.ò ˆØˆˆ1‰ˆaŠðä”3Ð'Ó(¨1Ñ,Ó-ò ˆØˆˆ1‰ˆaŠðä�1”cÐ+Ó,¨qÑ0Ó1ò QˆÜ�qœ#Ð.Ó/°!Ñ3Ó4ò 	QˆAØ   Q¡Ñ'Ð+;¸AÀ¹EÑ+BÒBØ˜a !™e™9 Q¨¡UÑ+��1‘�a’ä˜r ! a¡%™y¨™|¨R°©U°1°q±5©\¸2¸aÀ!¹e¹9ÀQÈÁUÑ;KÓLÈqÑP��1‘�a’ñ		QðQð ˆb‰6�"‰:Ðùò 
Xs   šDr5   .c                 óò   ‡‡— t         j                  t         j                  t         j                  t         j                  iŠdt         dt        t         t         f   dt         fd„Št	        ˆˆfd„| D «       «      S )a  Flip the trace of edit operations.

    Instead of rewriting a->b, get a recipe for rewriting b->a. Simply flips insertions and deletions.

    Args:
        trace: A tuple of edit operations.

    Return:
        inverted_trace:
            A tuple of inverted edit operations.

    rX   Ú_flip_operationsr!   c                 ó0   — | |v r|j                  | «      S | S rn   )Úget)rX   r{   s     r   Ú_replace_operation_or_retainz1_flip_trace.<locals>._replace_operation_or_retaint  s$   € ð Ð(Ñ(Ø#×'Ñ'¨	Ó2Ð2ØÐr   c              3   ó0   •K  — | ]  } ‰|‰«      –— Œ y ­wrn   r   )rq   rX   r{   r~   s     €€r   rs   z_flip_trace.<locals>.<genexpr>{  s   øè ø€ ÒbÈyÑ-¨iÐ9I×JÑbùs   ƒ)r	   r   r   Údictr\   )r5   r{   r~   s    @@r   Ú_flip_tracer�   b  sk   ù€ ô 	×!Ñ!¤?×#<Ñ#<Ü×!Ñ!¤?×#<Ñ#<ð@Ðð
Ü"ðÜ6:¼?ÌOÐ;[Ñ6\ðä	óô ÔbÐ\aÔbÓbÐbr   c                 ó  — dx}}g }g }i }| D ]ó  }|t         j                  k(  r2|dz  }|dz  }|||<   |j                  d«       |j                  d«       ŒH|t         j                  k(  r2|dz  }|dz  }|||<   |j                  d«       |j                  d«       Œ�|t         j                  k(  r|dz  }|j                  d«       Œ·|t         j
                  k(  r|dz  }|||<   |j                  d«       Œæt        d|›d�«      ‚ |||fS )aî  Transform trace of edit operations into an alignment of the sequences.

    Args:
        trace: A trace of edit operations as a tuple of `_EDIT_OPERATIONS` enumerates.

    Return:
        alignments: A dictionary mapping aligned positions between a reference and a hypothesis.
        reference_errors: A list of error positions in a reference.
        hypothesis_errors: A list of error positions in a hypothesis.

    Raises:
        ValueError:
            If an unknown operation is

    r;   r:   r   rV   ú.)r	   r   rd   r   r   r   rW   )r5   Úreference_positionÚhypothesis_positionÚreference_errorsÚhypothesis_errorsÚ
alignmentsrX   s          r   Ú_trace_to_alignmentr‰   ~  sI  € ð  02Ð1ÐÐ,Ø"$ÐØ#%ÐØ!#€Jð ò Bˆ	Øœ×2Ñ2Ò2Ø 1Ñ$ÐØ !Ñ#ÐØ-@ˆJÐ)Ñ*Ø×#Ñ# AÔ&Ø×$Ñ$ QÕ'Øœ/×7Ñ7Ò7Ø 1Ñ$ÐØ !Ñ#ÐØ-@ˆJÐ)Ñ*Ø×#Ñ# AÔ&Ø×$Ñ$ QÕ'Øœ/×3Ñ3Ò3Ø 1Ñ$ÐØ×$Ñ$ QÕ'Øœ/×3Ñ3Ò3Ø !Ñ#ÐØ-@ˆJÐ)Ñ*Ø×#Ñ# AÕ&äÐ1°)°¸aÐ@ÓAÐAð+Bð. Ð'Ð):Ð:Ð:r   )r@   Úcollections.abcr   Úenumr   r   Útypingr   r?   rZ   rg   r(   ri   r	   r   r\   rv   r=   ry   r�   r€   r‰   r   r   r   ú<module>r�      s>  ðó> Ý $ß Ý ð €ð €Ù�D“	€ð ô�c˜4ó ó ð÷p\ñ p\ðf)Ø�h˜s‘m X¨h°s©mÑ%<Ð<Ñ=ð)à˜S (¨3¡-Ð/Ñ0ð)ð ˆ8�H˜S‘MÑ" H¨S¡MÐ1Ñ2ó)ð@ d¨3¡ið À4ÈÁ9ð ÐQTó ð0c�u˜_¨cÐ1Ñ2ð c°u¸_ÈcÐ=QÑ7Ró cð8-;˜u _°cÐ%9Ñ:ð -;¸uÀTÈ#ÈsÈ(Á^ÐUYÐZ]ÑU^Ð`dÐehÑ`iÐEiÑ?jô -;r   