§
    ’©º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dS )é    )ÚCounter)ÚSequence)ÚCallableÚOptionalÚUnionN)ÚTensorÚtensorÚngram_input_listÚn_gramÚreturnc                 óì   — t          ¦   «         }t          d|dz   ¦  «        D ]Q}t          t          | ¦  «        |z
  dz   ¦  «        D ],}t          | |||z   …         ¦  «        }||xx         dz  cc<   Œ-ŒR|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         úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/text/bleu.pyÚ_count_ngramr      s›   € õ %™YœY€Må�1�f˜q‘jÑ!Ô!ð *ð *ˆÝ•sÐ+Ñ,Ô,¨qÑ0°1Ñ4Ñ5Ô5ð 	*ð 	*ˆAÝÐ.¨q°A¸±E¨{Ô;Ñ<Ô<ˆIØ˜)Ð$Ð$Ô$¨Ñ)Ð$Ð$Ñ$Ð$ð	*ð Ðó    Úsentencec                 ó*   — |                       ¦   «         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                 óB  ‡‡— ˆfd„|D ¦   «         }ˆfd„| D ¦   «         }	t          |	|¦  «        D ]í\  Š}
|t          ‰¦  «        z  }d„ |
D ¦   «         }ˆfd„|D ¦   «         }|||                     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 )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

    c                 ó,   •— g | ]}ˆfd „|D ¦   «         ‘ŒS )c                 ó.   •— g | ]}|r ‰|¦  «        ng ‘ŒS © r(   ©Ú.0Úliner$   s     €r   ú
<listcomp>z1_bleu_score_update.<locals>.<listcomp>.<listcomp>U   s,   ø€ Ð2aÐ2aÐ2aÐW[ÀdÐ3R°9°9¸T±?´?°?ÐPRÐ2aÐ2aÐ2ar   r(   )r*   Útr$   s     €r   r,   z&_bleu_score_update.<locals>.<listcomp>U   s0   ø€ Ð1rÐ1rÐ1rÐfgÐ2aÐ2aÐ2aÐ2aÐ_`Ð2aÑ2aÔ2aÐ1rÐ1rÐ1rr   c                 ó.   •— g | ]}|r ‰|¦  «        ng ‘ŒS r(   r(   r)   s     €r   r,   z&_bleu_score_update.<locals>.<listcomp>V   s*   ø€ Ð&YÐ&YÐ&YÈ4¸$Ð'F y y°¡¤ ÀBÐ&YÐ&YÐ&Yr   c                 ó,   — g | ]}t          |¦  «        ‘ŒS r(   )r   ©r*   Útgts     r   r,   z&_bleu_score_update.<locals>.<listcomp>Z   s   € Ð7Ð7Ð7¨�3˜s™8œ8Ð7Ð7Ð7r   c                 óN   •— g | ]!}t          t          ‰¦  «        |z
  ¦  «        ‘Œ"S r(   )Úabsr   )r*   ÚxÚpreds     €r   r,   z&_bleu_score_update.<locals>.<listcomp>[   s+   ø€ ÐGÐGÐG°!�3�s 4™yœy¨1™}Ñ-Ô-ÐGÐGÐGr   r   )Úzipr   ÚindexÚminr   r   )r   r   r    r!   r"   r#   r   r$   Útarget_Úpreds_ÚtargetsÚtarget_len_listÚtarget_len_diffÚpreds_counterÚtarget_counterr1   Úngram_counter_clipÚcounter_clipÚcounterr5   s          `           @r   Ú_bleu_score_updaterC   =   s™  øø€ ð0 2sÐ1rÐ1rÐ1rÐkqÐ1rÑ1rÔ1r€GØ&YÐ&YÐ&YÐ&YÐSXÐ&YÑ&YÔ&Y€Få˜V WÑ-Ô-ð Dð D‰ˆˆgØ•S˜‘Y”YÑˆ	Ø7Ð7¨wÐ7Ñ7Ô7ˆØGÐGÐGÐG°ÐGÑGÔGˆØ�o o×&;Ò&;½CÀÑ<PÔ<PÑ&QÔ&QÔRÑRˆ
Ý!-¨d°FÑ!;Ô!;ˆÝ")¡)¤)ˆàð 	8ð 	8ˆCØ�l¨3°Ñ7Ô7Ñ7ˆNˆNà*¨^Ñ;Ðà.ð 	Qð 	QˆLØ•c˜,Ñ'Ô'¨!Ñ+Ð,Ð,Ô,Ð0BÀ<Ô0PÑPÐ,Ð,Ñ,Ð,à$ð 	Dð 	DˆGØ�˜G™œ qÑ(Ð)Ð)Ô)¨]¸7Ô-CÑCÐ)Ð)Ñ)Ð)ð	Dð �jÐ Ð r   ÚweightsÚsmoothc           
      óZ  — |j         }t          |¦  «        dk    rt          d|¬¦  «        S |rxt          j        t          j        |t          j        ||¬¦  «        ¦  «        t          j        |t          j        ||¬¦  «        ¦  «        ¦  «        }|d         |d         z  |d<   n||z  }t          ||¬¦  «        t          j        |¦  «        z  }	t          j        t          j	        |	¦  «        ¦  «        }
| |k    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   )
rH   r8   r	   ÚtorchÚdivÚaddÚonesÚlogÚexpÚsum)r"   r#   r    r!   r   rD   rE   rH   Úprecision_scoresÚlog_precision_scoresÚgeometric_meanÚbrevity_penaltys               r   Ú_bleu_score_computerU   n   s0  € ð* Ô€FÝ
ˆ9�~„~˜ÒÐÝ�c &Ð)Ñ)Ô)Ð)àð 3Ý œ9ÝŒI�i¥¤¨F¸6Ð!BÑ!BÔ!BÑCÔCÝŒI�k¥5¤:¨f¸VÐ#DÑ#DÔ#DÑEÔEñ
ô 
Ðð (¨œl¨[¸¬^Ñ;Ð˜ÑÐà$ {Ñ2Ðå! '°&Ð9Ñ9Ô9½E¼IÐFVÑ<WÔ<WÑWÐÝ”Y�uœyÐ)=Ñ>Ô>Ñ?Ô?€NØ4=À
Ò4JÐ4J•f˜S¨Ð0Ñ0Ô0Ð0ÕPUÔPYÐZ[Ð_iÐluÑ_uÑZvÑPwÔPw€OØ˜^Ñ+Ð+r   Fc           
      ób  — t          | t          ¦  «        r| gn| }d„ |D ¦   «         }t          |¦  «        t          |¦  «        k    r/t          dt          |¦  «        › dt          |¦  «        › �¦  «        ‚|�5t          |¦  «        |k    r"t          dt          |¦  «        › d|› �¦  «        ‚|€	d|z  g|z  }t	          j        |¦  «        }t	          j        |¦  «        }t          d¦  «        }	t          d¦  «        }
t          |||||	|
|t          ¦  «        \  }	}
t          |	|
|||||¦  «        S )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`_

    c                 óB   — g | ]}t          |t          ¦  «        r|gn|‘ŒS r(   )Ú
isinstanceÚstrr0   s     r   r,   zbleu_score.<locals>.<listcomp>¿   s-   € ÐHÐHÐH¸#�
 3­Ñ,Ô,Ð5�ˆuˆu°#ÐHÐHÐHr   zCorpus has different size z != Nz5List of weights has different weights than `n_gram`: rI   rG   )
rX   rY   r   Ú
ValueErrorrJ   Úzerosr	   rC   r   rU   )r   r   r   rE   rD   r:   r9   r    r!   r"   r#   s              r   Ú
bleu_scorer\   –   sA  € õP # 5­#Ñ.Ô.Ð9ˆeˆWˆW°E€FØHÐHÀÐHÑHÔH€Gå
ˆ6�{„{•c˜'‘l”lÒ"Ð"ÝÐUµc¸&±k´kÐUÐUÅsÈ7Á|Ä|ÐUÐUÑVÔVÐVàÐ�s 7™|œ|¨vÒ5Ð5ÝÐkÕQTÐU\ÑQ]ÔQ]ÐkÐkÐciÐkÐkÑlÔlÐlØ€Ø˜‘<�. 6Ñ)ˆå”˜FÑ#Ô#€IÝ”+˜fÑ%Ô%€KÝ�s‘”€IÝ˜‘”€Jå.Ø�˜ K°¸JÈÕP\ñô Ñ€Iˆzõ ˜y¨*°iÀÈfÐV]Ð_eÑfÔfÐfr   )r   FN)Úcollectionsr   Úcollections.abcr   Útypingr   r   r   rJ   r   r	   rY   Úintr   r   r   rC   ÚfloatÚboolrU   r\   r(   r   r   ú<module>rc      s]  ðð&  Ð Ð Ð Ð Ð Ø $Ð $Ð $Ð $Ð $Ð $Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  ð 8¨C¤=ð ¸#ð À'ð ð ð ð ð*
˜3ð 
 8¨C¤=ð 
ð 
ð 
ð 
ð( Ø0<ð.!ð .!Ø�CŒ=ð.!à�X˜c”]Ô#ð.!ð ð.!ð ð	.!ð
 ð.!ð ð.!ð ð.!ð ˜˜˜x¨œ}Ð,Ô-ð.!ð ˆ6�6ˆ>Ôð.!ð .!ð .!ð .!ðb%,Øð%,àð%,ð ð%,ð ð	%,ð
 ð%,ð �eŒ_ð%,ð ð%,ð ð%,ð %,ð %,ð %,ðV ØØ)-ð<gð <gØ��h˜s”mÐ#Ô$ð<gà�U˜3 ¨¤Ð-Ô.Ô/ð<gð ð<gð ð	<gð
 �h˜u”oÔ&ð<gð ð<gð <gð <gð <gð <gð <gr   