Ë
    þÍ:j®  ã                   ó`  — d dl mZ d dlmZ d dlmZmZmZ d dlZd dlm	Z	m
Z
 dee   dedefd	„Zd
edee   fd„Zdefdee   deee      de	de	de	de	dedeegee   f   dee	e	f   fd„Zde	de	de	de	dede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edeee      de	fd„Zy)é    )ÚCounter)ÚSequence)ÚCallableÚOptionalÚUnionN)ÚTensorÚtensorÚngram_input_listÚn_gramÚreturnc                 ó¼   — t        «       }t        d|dz   «      D ]?  }t        t        | «      |z
  dz   «      D ]   }t        | |||z    «      }||xx   dz  cc<   Œ" ŒA |S )a  Count how many times each word appears in a given text with ngram.

    Args:
        ngram_input_list: A list of translated text or reference texts
        n_gram: gram value ranged 1 to 4

    Return:
        ngram_counter: a collections.Counter object of ngram

    é   )r   ÚrangeÚlenÚtuple)r
   r   Úngram_counterÚiÚjÚ	ngram_keys         úv/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchmetrics/functional/text/bleu.pyÚ_count_ngramr      sw   € ô %›Y€Mä�1�f˜q‘jÓ!ò *ˆÜ”sÐ+Ó,¨qÑ0°1Ñ4Ó5ò 	*ˆAÜÐ.¨q°A¸±EÐ;Ó<ˆIØ˜)Ó$¨Ñ)Ô$ñ	*ð*ð
 Ðó    Úsentencec                 ó"   — | j                  «       S )z‘Tokenizes sentence into list of words.

    Args:
        sentence: A sentence separated by white space.

    Return:
        List of words

    )Úsplit)r   s    r   Ú_tokenize_fnr   0   s   € ð �>‰>ÓÐr   é   ÚpredsÚtargetÚ	numeratorÚdenominatorÚ	preds_lenÚ
target_lenÚ	tokenizerc           
      ó¬  — |D ��	cg c]  }|D �	cg c]  }	|	r ||	«      ng ‘Œ c}	‘Œ }
}}	| D �	cg c]  }	|	r ||	«      ng ‘Œ }}	t        ||
«      D ]æ  \  }}|t        |«      z  }|D �cg c]  }t        |«      ‘Œ }}|D �cg c]  }t        t        |«      |z
  «      ‘Œ }}|||j                  t	        |«      «         z  }t        ||«      }t        «       }|D ]  }|t        ||«      z  }Œ ||z  }|D ]  }|t        |«      dz
  xx   ||   z  cc<   Œ  |D ]  }|t        |«      dz
  xx   ||   z  cc<   Œ  Œè ||fS c c}	w c c}	}w c c}	w c c}w c c}w )a€  Update and returns variables required to compute the BLEU score.

    Args:
        preds: An iterable of machine translated corpus
        target: An iterable of iterables of reference corpus
        numerator: Numerator of precision score (true positives)
        denominator: Denominator of precision score (true positives + false positives)
        preds_len: count of words in a candidate prediction
        target_len: count of words in a reference translation
        target: count of words in a reference translation
        n_gram: gram value ranged 1 to 4
        tokenizer: A function that turns sentence into list of words

    r   )Úzipr   ÚabsÚindexÚminr   r   )r   r   r    r!   r"   r#   r   r$   ÚtÚlineÚtarget_Úpreds_ÚpredÚtargetsÚtgtÚtarget_len_listÚxÚtarget_len_diffÚpreds_counterÚtarget_counterÚngram_counter_clipÚcounter_clipÚcounters                          r   Ú_bleu_score_updater9   =   s”  € ð0 lr×1rÐfgÐ_`Ö2aÐW[Ád±9¸T´?ÐPRÑ3RÔ2aÐ1r€GÑ1rØSXÖ&YÈ4¹$¡y°¤ÀBÑ'FÐ&Y€FÐ&Yä˜V WÓ-ò D‰ˆˆgØ”S˜“YÑˆ	Ø/6Ö7¨œ3˜s�8Ð7ˆÐ7Ø7FÖG°!œ3œs 4›y¨1™}Õ-ÐGˆÐGØ�o o×&;Ñ&;¼CÀÓ<PÓ&QÑRÑRˆ
Ü!-¨d°FÓ!;ˆÜ")£)ˆàò 	8ˆCØœl¨3°Ó7Ñ7‰Nð	8ð +¨^Ñ;Ðà.ò 	QˆLØ”c˜,Ó'¨!Ñ+Ó,Ð0BÀ<Ñ0PÑPÔ,ð	Qð %ò 	DˆGØœ˜G› qÑ(Ó)¨]¸7Ñ-CÑCÔ)ñ	Dð!Dð& �jÐ Ð ùò- 3bùÓ1rùÚ&Yùò 8ùÚGs&   †	E�D<¢E®EÁ'EÁ?EÄ<EÚweightsÚsmoothc           
      óF  — |j                   }t        |«      dk(  rt        d|¬«      S |rwt        j                  t        j
                  |t        j                  ||¬«      «      t        j
                  |t        j                  ||¬«      «      «      }|d   |d   z  |d<   n||z  }t        ||¬«      t        j                  |«      z  }	t        j                  t        j                  |	«      «      }
| |kD  rt        d|¬«      nt        j                  d|| z  z
  «      }||
z  S )aâ  Compute the BLEU score.

    Args:
        preds_len: count of words in a candidate translation
        target_len: count of words in a reference translation
        numerator: Numerator of precision score (true positives)
        denominator: Denominator of precision score (true positives + false positives)
        n_gram: gram value ranged 1 to 4
        weights: Weights used for unigrams, bigrams, etc. to calculate BLEU score.
        smooth: Whether to apply smoothing

    ç        )Údevicer   ç      ð?r   )
r>   r)   r	   ÚtorchÚdivÚaddÚonesÚlogÚexpÚsum)r"   r#   r    r!   r   r:   r;   r>   Úprecision_scoresÚlog_precision_scoresÚgeometric_meanÚbrevity_penaltys               r   Ú_bleu_score_computerK   n   sþ   € ð* ×Ñ€FÜ
ˆ9ƒ~˜ÒÜ�c &Ô)Ð)áÜ Ÿ9™9Ü�I‰I�i¤§¡¨F¸6Ô!BÓCÜ�I‰I�k¤5§:¡:¨f¸VÔ#DÓEó
Ðð (¨™l¨[¸©^Ñ;Ð˜Òà$ {Ñ2Ðä! '°&Ô9¼E¿I¹IÐFVÓ<WÑWÐÜ—Y‘YœuŸy™yÐ)=Ó>Ó?€NØ4=À
Ò4J”f˜S¨Õ0ÔPU×PYÑPYÐZ[Ð_iÐluÑ_uÑZvÓPw€OØ˜^Ñ+Ð+r   c           
      ó,  — t        | t        «      r| gn| }|D �cg c]  }t        |t        «      r|gn|‘Œ }}t        |«      t        |«      k7  r#t        dt        |«      › dt        |«      › �«      ‚|�(t        |«      |k7  rt        dt        |«      › d|› �«      ‚|€	d|z  g|z  }t	        j
                  |«      }t	        j
                  |«      }	t        d«      }
t        d«      }t        ||||	|
||t        «      \  }
}t        |
|||	|||«      S c c}w )a3  Calculate `BLEU score`_ of machine translated text with one or more references.

    Args:
        preds: An iterable of machine translated corpus
        target: An iterable of iterables of reference corpus
        n_gram: Gram value ranged from 1 to 4
        smooth: Whether to apply smoothing - see [2]
        weights:
            Weights used for unigrams, bigrams, etc. to calculate BLEU score.
            If not provided, uniform weights are used.

    Return:
        Tensor with BLEU Score

    Raises:
        ValueError: If ``preds`` and ``target`` corpus have different lengths.
        ValueError: If a length of a list of weights is not ``None`` and not equal to ``n_gram``.

    Example:
        >>> from torchmetrics.functional.text import bleu_score
        >>> preds = ['the cat is on the mat']
        >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
        >>> bleu_score(preds, target)
        tensor(0.7598)

    References:
        [1] BLEU: a Method for Automatic Evaluation of Machine Translation by Papineni,
        Kishore, Salim Roukos, Todd Ward, and Wei-Jing Zhu `BLEU`_

        [2] Automatic Evaluation of Machine Translation Quality Using Longest Common Subsequence
        and Skip-Bigram Statistics by Chin-Yew Lin and Franz Josef Och `Machine Translation Evolution`_

    zCorpus has different size z != z5List of weights has different weights than `n_gram`: r?   r=   )
Ú
isinstanceÚstrr   Ú
ValueErrorr@   Úzerosr	   r9   r   rK   )r   r   r   r;   r:   r-   r0   r,   r    r!   r"   r#   s               r   Ú
bleu_scorerQ   –   s%  € ôP # 5¬#Ô.ˆe‰W°E€FØAGÖH¸#œ
 3¬Ô,�‰u°#Ñ5ÐH€GÐHä
ˆ6ƒ{”c˜'“lÒ"ÜÐ5´c¸&³k°]À$ÄsÈ7Ã|ÀnÐUÓVÐVàÐœs 7›|¨vÒ5ÜÐPÔQTÐU\ÓQ]ÐP^Ð^bÐciÐbjÐkÓlÐlØ€Ø˜‘<�. 6Ñ)ˆä—‘˜FÓ#€IÜ—+‘+˜fÓ%€KÜ�s“€IÜ˜“€Jä.Ø�˜ K°¸JÈÔP\óÑ€Iˆzô ˜y¨*°iÀÈfÐV]Ð_eÓfÐfùò' Is   šD)r   FN)Úcollectionsr   Úcollections.abcr   Útypingr   r   r   r@   r   r	   rN   Úintr   r   r   r9   ÚfloatÚboolrK   rQ   © r   r   ú<module>rY      s§  ðõ&  Ý $ß ,Ñ ,ã ß  ð 8¨C¡=ð ¸#ð À'ó ð*
˜3ð 
 8¨C¡=ó 
ð( Ø0<ñ.!Ø�C‰=ð.!à�X˜c‘]Ñ#ð.!ð ð.!ð ð	.!ð
 ð.!ð ð.!ð ð.!ð ˜˜˜x¨™}Ð,Ñ-ð.!ð ˆ6�6ˆ>Ñó.!ðb%,Øð%,àð%,ð ð%,ð ð	%,ð
 ð%,ð �e‰_ð%,ð ð%,ð ó%,ðV ØØ)-ñ<gØ��h˜s‘mÐ#Ñ$ð<gà�U˜3 ¨¡Ð-Ñ.Ñ/ð<gð ð<gð ð	<gð
 �h˜u‘oÑ&ð<gð ô<gr   