§
    ’©ºj0e  ã            *       ó´  — d dl mZ 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 dlmZ  ed¦  «        Z ed	¦  «        Zd
ededeeeef         eeef         eeef         eeef         eeef         eeef         f         fd„Zdededee         fd„Zdedee         fd„Zdedee         fd„Zdee         dedeeeeedf         ef         f         fd„Zded
ededededeeeeeedf         ef         f         eeeeedf         ef         f         eeef         eeef         f         fd„Zdeeeeedf         ef         f         deeeeedf         ef         f         deeef         fd„Zdeeef         deeef         deeef         fd„Zd eeef         d!eeef         d"eeef         d#eeef         d$eeef         d%eeef         d&e d'e defd(„Z!d)ee         d*eeeeedf         ef         f         d+eeeeedf         ef         f         d,eeef         d-eeef         d
eded&e d'e dededeeeeef         eeef         eeef         eeef         f         fd.„Z"	 d@d/e	eee         f         d0e	ee         eee                  f         d1eeef         d2eeef         d3eeef         d4eeef         d5eeef         d6eeef         d
eded&e d'e deded7eee                  deeeef         eeef         eeef         eeef         eeef         eeef         eee                  f         f d8„Z#d1eeef         d2eeef         d3eeef         d4eeef         d5eeef         d6eeef         d&e d'e defd9„Z$	 	 	 	 	 	 dAd/e	eee         f         d0ee	eee         f                  d
eded'e deded>ede	eeeef         f         fd?„Z%dS )Bé    ©Údefaultdict)ÚSequence)Úchain)ÚListÚOptionalÚUnionN)ÚTensorÚtensor)Ú_validate_inputsg¼‰Ø—²Òœ<z !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~Ún_char_orderÚn_word_orderÚreturnc                 ó>  — d„ t          | ¦  «        D ¦   «         }d„ t          |¦  «        D ¦   «         }d„ t          | ¦  «        D ¦   «         }d„ t          |¦  «        D ¦   «         }d„ t          | ¦  «        D ¦   «         }d„ t          |¦  «        D ¦   «         }||||||fS )ah  Prepare dictionaries with default zero values for total ref, hypothesis and matching character and word n-grams.

    Args:
        n_char_order: A character n-gram order.
        n_word_order: A word n-gram order.

    Return:
        Dictionaries with default zero values for total reference, hypothesis and matching character and word
        n-grams.

    c                 ó4   — i | ]}|d z   t          d¦  «        “ŒS ©é   ç        ©r   ©Ú.0Úns     úq/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/text/chrf.pyú
<dictcomp>z*_prepare_n_grams_dicts.<locals>.<dictcomp>6   ó$   € Ð2cÐ2cÐ2cÈ!°1°q±5½&À¹+¼+Ð2cÐ2cÐ2có    c                 ó4   — i | ]}|d z   t          d¦  «        “ŒS r   r   r   s     r   r   z*_prepare_n_grams_dicts.<locals>.<dictcomp>7   r   r   c                 ó4   — i | ]}|d z   t          d¦  «        “ŒS r   r   r   s     r   r   z*_prepare_n_grams_dicts.<locals>.<dictcomp>8   ó$   € Ð3dÐ3dÐ3dÈ1°A¸±E½6À#¹;¼;Ð3dÐ3dÐ3dr   c                 ó4   — i | ]}|d z   t          d¦  «        “ŒS r   r   r   s     r   r   z*_prepare_n_grams_dicts.<locals>.<dictcomp>9   r   r   c                 ó4   — i | ]}|d z   t          d¦  «        “ŒS r   r   r   s     r   r   z*_prepare_n_grams_dicts.<locals>.<dictcomp>:   ó$   € Ð5fÐ5fÐ5fÈQ°a¸!±e½VÀC¹[¼[Ð5fÐ5fÐ5fr   c                 ó4   — i | ]}|d z   t          d¦  «        “ŒS r   r   r   s     r   r   z*_prepare_n_grams_dicts.<locals>.<dictcomp>;   r"   r   )Úrange)r   r   Útotal_preds_char_n_gramsÚtotal_preds_word_n_gramsÚtotal_target_char_n_gramsÚtotal_target_word_n_gramsÚtotal_matching_char_n_gramsÚtotal_matching_word_n_gramss           r   Ú_prepare_n_grams_dictsr+   &   sÕ   € ð  3dÐ2cÍuÐUaÑObÔObÐ2cÑ2cÔ2cÐØ2cÐ2cÍuÐUaÑObÔObÐ2cÑ2cÔ2cÐØ3dÐ3dÕPUÐVbÑPcÔPcÐ3dÑ3dÔ3dÐØ3dÐ3dÕPUÐVbÑPcÔPcÐ3dÑ3dÔ3dÐØ5fÐ5fÕRWÐXdÑReÔReÐ5fÑ5fÔ5fÐØ5fÐ5fÕRWÐXdÑReÔReÐ5fÑ5fÔ5fÐð 	!Ø Ø!Ø!Ø#Ø#ðð r   ÚsentenceÚ
whitespacec                 óŽ   — |rt          | ¦  «        S t          |                      ¦   «                              dd¦  «        ¦  «        S )a   Split sentence into individual characters.

    Args:
        sentence: An input sentence to split.
        whitespace: An indication whether to keep whitespaces during character n-gram extraction.

    Return:
        A list of separated characters.

    ú Ú )ÚlistÚstripÚreplace)r,   r-   s     r   Ú_get_charactersr4   G   s@   € ð ð Ý�H‰~Œ~ÐÝ�—’Ñ Ô ×(Ò(¨¨bÑ1Ô1Ñ2Ô2Ð2r   Úwordc                 ó¸   — t          | ¦  «        dk    r| gS | d         t          v r| dd…         | d         gS | d         t          v r| d         | dd…         gS | gS )ax  Separates out punctuation from beginning and end of words for chrF.

    Adapted from https://github.com/m-popovic/chrF and
    https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/metrics/chrf.py.

    Args:
        word: An input word to be separated from a punctuation if present.

    Return:
        A list of a single word or a separated word and punctuation.

    r   éÿÿÿÿNr   )ÚlenÚ_PUNCTUATIONS)r5   s    r   Ú_separate_word_and_punctuationr:   W   sm   € õ ˆ4�y„y�A‚~€~ØˆvˆàˆB„x•=Ð Ð Ø�S�b�S”	˜4 œ8Ð$Ð$ØˆA„w•-ÐÐØ�Q”˜˜a˜b˜bœÐ"Ð"Øˆ6€Mr   c                 ó    — t          t          j        d„ |                      ¦   «                              ¦   «         D ¦   «         ¦  «        ¦  «        S )zëSeparates out punctuation from beginning and end of words for chrF for all words in the sentence.

    Args:
        sentence: An input sentence to split

    Return:
        An aggregated list of separated words and punctuation.

    c              3   ó4   K  — | ]}t          |¦  «        V — Œd S ©N)r:   )r   r5   s     r   ú	<genexpr>z-_get_words_and_punctuation.<locals>.<genexpr>x   s+   è è € Ð#nÐ#nÈTÕ$BÀ4Ñ$HÔ$HÐ#nÐ#nÐ#nÐ#nÐ#nÐ#nr   )r1   r   Úfrom_iterabler2   Úsplit)r,   s    r   Ú_get_words_and_punctuationrA   n   sE   € õ •Ô#Ð#nÐ#nÐU]×UcÒUcÑUeÔUe×UkÒUkÑUmÔUmÐ#nÑ#nÔ#nÑnÔnÑoÔoÐor   Úchar_or_word_listÚn_gram_order.c                 ó   ‡ ‡— t          d„ ¦  «        }t          d|dz   ¦  «        D ]WŠˆ ˆfd„t          t          ‰ ¦  «        ‰z
  dz   ¦  «        D ¦   «         D ]%}|‰         |xx         t          d¦  «        z  cc<   Œ&ŒX|S )zèCalculate n-gram counts.

    Args:
        char_or_word_list: A list of characters of words
        n_gram_order: The largest number of n-gram.

    Return:
        A dictionary of dictionaries with a counts of given n-grams.

    c                  ó"   — t          d„ ¦  «        S )Nc                  ó    — t          d¦  «        S ©Nr   r   © r   r   ú<lambda>z1_ngram_counts.<locals>.<lambda>.<locals>.<lambda>†   s   € Õ_eÐfiÑ_jÔ_j€ r   r   rH   r   r   rI   z_ngram_counts.<locals>.<lambda>†   s   € Í;ÐWjÐWjÑKkÔKk€ r   r   c              3   óL   •K  — | ]}t          ‰||‰z   …         ¦  «        V — Œd S r=   )Útuple)r   ÚirB   r   s     €€r   r>   z _ngram_counts.<locals>.<genexpr>ˆ   s9   øè è € ÐiÐi¸a•eÐ-¨a°!°a±%¨iÔ8Ñ9Ô9ÐiÐiÐiÐiÐiÐir   )r   r$   r8   r   )rB   rC   ÚngramsÚngramr   s   `   @r   Ú_ngram_countsrO   {   s¬   øø€ õ 8CÐCkÐCkÑ7lÔ7l€FÝ�1�l QÑ&Ñ'Ô'ð *ð *ˆØiÐiÐiÐiÐiÅ5ÍÐM^ÑI_ÔI_ÐbcÑIcÐfgÑIgÑChÔChÐiÑiÔið 	*ð 	*ˆEØ�1ŒI�eÐÐÔ¥ q¡	¤	Ñ)ÐÐÑÐð	*à€Mr   Ú	lowercasec                 óB  ‡— dt           dt          dt          dt          dt          t          t          t          t          t           df         t
          f         f         t          t          t          t          t           df         t
          f         f         f         f
ˆfd„}dt          t          t          t          t           df         t
          f         f         dt          t          t
          f         fd	„} || |||¦  «        \  }} ||¦  «        }	 ||¦  «        }
|||	|
fS )
aÔ  Get n-grams and total n-grams.

    Args:
        sentence: An input sentence
        n_char_order: A character n-gram order.
        n_word_order: A word n-gram order.
        lowercase: An indication whether to enable case-insensitivity.
        whitespace: An indication whether to keep whitespaces during character n-gram extraction.

    Return:
        char_n_grams_counts: A dictionary of dictionaries with sentence character n-grams.
        word_n_grams_counts: A dictionary of dictionaries with sentence word n-grams.
        total_char_n_grams: A dictionary containing a total number of sentence character n-grams.
        total_word_n_grams: A dictionary containing a total number of sentence word n-grams.

    r,   r   r   rP   r   .c                 ó®   •— |r|                       ¦   «         } t          t          | ‰¦  «        |¦  «        }t          t          | ¦  «        |¦  «        }||fS )z@Get a dictionary of dictionaries with a counts of given n-grams.)ÚlowerrO   r4   rA   )r,   r   r   rP   Úchar_n_grams_countsÚword_n_grams_countsr-   s         €r   Ú_char_and_word_ngrams_countszJ_get_n_grams_counts_and_total_ngrams.<locals>._char_and_word_ngrams_counts¦   s]   ø€ ð ð 	(Ø—~’~Ñ'Ô'ˆHÝ+­O¸HÀjÑ,QÔ,QÐS_Ñ`Ô`ÐÝ+Õ,FÀxÑ,PÔ,PÐR^Ñ_Ô_ÐØ"Ð$7Ð7Ð7r   Ún_grams_countsc                 óÌ   — t          d„ ¦  «        }| D ]P}t          | |                              ¦   «         ¦  «                             ¦   «                              ¦   «         ||<   ŒQ|S )z.Get total sum of n-grams over n-grams w.r.t n.c                  ó    — t          d¦  «        S rG   r   rH   r   r   rI   zQ_get_n_grams_counts_and_total_ngrams.<locals>._get_total_ngrams.<locals>.<lambda>²   s   € ½vÀc¹{¼{€ r   )r   ÚsumÚvaluesÚdetachÚclone)rW   Útotal_n_gramsr   s      r   Ú_get_total_ngramsz?_get_n_grams_counts_and_total_ngrams.<locals>._get_total_ngrams°   sh   € å+6Ð7JÐ7JÑ+KÔ+KˆØð 	Pð 	PˆAÝ" >°!Ô#4×#;Ò#;Ñ#=Ô#=Ñ>Ô>×EÒEÑGÔG×MÒMÑOÔOˆM˜!ÑÐØÐr   )ÚstrÚintÚboolrK   Údictr
   )r,   r   r   rP   r-   rV   r_   rT   rU   Útotal_char_n_gramsÚtotal_word_n_gramss       `      r   Ú$_get_n_grams_counts_and_total_ngramsrf   �   s5  ø€ ð28Ýð8Ý%(ð8Ý8;ð8ÝHLð8å	�t•C��e¥C¨ Hœo­vÐ5Ô6Ð6Ô7½½cÅ4ÍÍcÐSVÈhÌÕY_ÐH_ÔC`Ð>`Ô9aÐaÔ	bð8ð 8ð 8ð 8ð 8ð 8ð­$­sµD½½sÀC¸x¼Í&Ð9PÔ4QÐ/QÔ*Rð ÕW[Õ\_ÕagÐ\gÔWhð ð ð ð ð 0LÐ/KØ�, ¨iñ0ô 0Ñ,ÐÐ,ð +Ð*Ð+>Ñ?Ô?ÐØ*Ð*Ð+>Ñ?Ô?ÐàÐ 3Ð5GÐI[Ð[Ð[r   Úhyp_n_grams_countsÚref_n_grams_countsc                 óÎ   ‡ ‡‡— t          d„ ¦  «        }‰ D ]NŠˆ ˆˆfd„‰ ‰         D ¦   «         }t          |¦  «                             ¦   «                              ¦   «         |‰<   ŒO|S )zòGet a number of n-gram matches between reference and hypothesis n-grams.

    Args:
        hyp_n_grams_counts: n-grams counts for hypothesis
        ref_n_grams_counts: n-grams counts for reference

    Return:
        matching_n_grams

    c                  ó    — t          d¦  «        S rG   r   rH   r   r   rI   z$_get_ngram_matches.<locals>.<lambda>Î   s   € ½fÀS¹k¼k€ r   c                 ój   •— g | ]/}t          j        ‰‰         |         ‰‰         |         ¦  «        ‘Œ0S rH   )ÚtorchÚmin)r   Ún_gramrg   r   rh   s     €€€r   ú
<listcomp>z&_get_ngram_matches.<locals>.<listcomp>Ð   sH   ø€ ð 
ð 
ð 
ØX^�EŒIÐ(¨Ô+¨FÔ3Ð5GÈÔ5JÈ6Ô5RÑSÔSð
ð 
ð 
r   )r   rZ   r\   r]   )rg   rh   Úmatching_n_gramsÚmin_n_gramsr   s   ``  @r   Ú_get_ngram_matchesrr   À   s˜   øøø€ õ +6Ð6IÐ6IÑ*JÔ*JÐØð @ð @ˆð
ð 
ð 
ð 
ð 
ð 
ØbtÐuvÔbwð
ñ 
ô 
ˆõ " +Ñ.Ô.×5Ò5Ñ7Ô7×=Ò=Ñ?Ô?Ð˜ÑÐØÐr   r^   Ún_gramsc                 ó<   — |D ]}| |xx         ||         z  cc<   Œ| S )aB  Aggregate total n-grams to keep corpus-level statistics.

    Args:
        total_n_grams: A dictionary containing a total corpus-level number of n-grams.
        n_grams: A dictionary containing a sentence-level number of n-grams.

    Return:
        A dictionary containing a total corpus-level number of n-grams.

    rH   )r^   rs   r   s      r   Ú_sum_over_dictsru   ×   s<   € ð ð 'ð 'ˆØ�aÐÐÔ˜G AœJÑ&ÐÐÑÐØÐr   Úmatching_char_n_gramsÚmatching_word_n_gramsÚhyp_char_n_gramsÚhyp_word_n_gramsÚref_char_n_gramsÚref_word_n_gramsÚn_orderÚbetac                 ó¾  — dt           t          t          f         dt           t          t          f         dt           t          t          f         dt          dt           t          t          f         f
d„} || |||¦  «        }	 |||||¦  «        }
t	          |	                     ¦   «         ¦  «        t	          |
                     ¦   «         ¦  «        z   t          |¦  «        z  S )a  Calculate sentence-level chrF/chrF++ score.

    For given hypothesis and reference statistics (either sentence-level or corpus-level)
    the chrF/chrF++ score is returned.

    Args:
        matching_char_n_grams:
            A total number of matching character n-grams between the best matching reference and hypothesis.
        matching_word_n_grams:
            A total number of matching word n-grams between the best matching reference and hypothesis.
        hyp_char_n_grams: A total number of hypothesis character n-grams.
        hyp_word_n_grams: A total number of hypothesis word n-grams.
        ref_char_n_grams: A total number of reference character n-grams.
        ref_word_n_grams: A total number of reference word n-grams.
        n_order: A sum of character and word n-gram order.
        beta: A parameter determining an importance of recall w.r.t. precision. If `beta=1`, their importance is equal.

    Return:
        A chrF/chrF++ score. This function is universal both for sentence-level and corpus-level calculation.

    rp   Úref_n_gramsÚhyp_n_gramsr}   r   c                 ó’   ‡ ‡‡‡‡‡‡— ˆˆ fd„‰ D ¦   «         Šˆ ˆfd„‰ D ¦   «         Šˆˆˆfd„‰ D ¦   «         Šˆˆˆˆfd„‰ D ¦   «         }|S )zGet n-gram level f-score.c                 ój   •— i | ]/}|‰|         d k    r‰|         ‰|         z  nt          d¦  «        “Œ0S ©r   r   r   )r   r   r€   rp   s     €€r   r   zA_calculate_fscore.<locals>._get_n_gram_fscore.<locals>.<dictcomp>  sQ   ø€ ð (
ð (
ð (
Ø_`ˆA°{À1´~ÈÒ7IÐ7IÐ Ô" [°¤^Ñ3Ð3ÍvÐVYÉ{Ì{ð(
ð (
ð (
r   c                 ój   •— i | ]/}|‰|         d k    r‰|         ‰|         z  nt          d¦  «        “Œ0S rƒ   r   )r   r   rp   r   s     €€r   r   zA_calculate_fscore.<locals>._get_n_gram_fscore.<locals>.<dictcomp>  sQ   ø€ ð %
ð %
ð %
Ø_`ˆA°{À1´~ÈÒ7IÐ7IÐ Ô" [°¤^Ñ3Ð3ÍvÐVYÉ{Ì{ð%
ð %
ð %
r   c                 óp   •— i | ]2}|t          j        ‰d z  ‰|         z  ‰|         z   t          ¦  «        “Œ3S )é   )rl   ÚmaxÚ_EPS_SMOOTHING)r   r   r}   Ú	precisionÚrecalls     €€€r   r   zA_calculate_fscore.<locals>._get_n_gram_fscore.<locals>.<dictcomp>  sJ   ø€ ð *
ð *
ð *
ØQRˆA�uŒy˜˜q™ 9¨Q¤<Ñ/°&¸´)Ñ;½^ÑLÔLð*
ð *
ð *
r   c                 óX   •— i | ]&}|d ‰dz  z   ‰|         z  ‰|         z  ‰|         z  “Œ'S )r   r†   rH   )r   r   r}   Údenominatorr‰   rŠ   s     €€€€r   r   zA_calculate_fscore.<locals>._get_n_gram_fscore.<locals>.<dictcomp>  sK   ø€ ð &
ð &
ð &
ØMNˆA��D˜!‘G‘˜y¨œ|Ñ+¨f°Q¬iÑ7¸+Àa¼.ÑHð&
ð &
ð &
r   rH   )rp   r   r€   r}   Úf_scorerŒ   r‰   rŠ   s   ```` @@@r   Ú_get_n_gram_fscorez-_calculate_fscore.<locals>._get_n_gram_fscore  sÓ   øøøøøøø€ ð(
ð (
ð (
ð (
ð (
Ødtð(
ñ (
ô (
ˆ	ð%
ð %
ð %
ð %
ð %
Ødtð%
ñ %
ô %
ˆð*
ð *
ð *
ð *
ð *
ð *
ØVfð*
ñ *
ô *
ˆð&
ð &
ð &
ð &
ð &
ð &
ð &
ØRbð&
ñ &
ô &
ˆð ˆr   )rc   ra   r
   ÚfloatrZ   r[   r   )rv   rw   rx   ry   rz   r{   r|   r}   rŽ   Úchar_n_gram_f_scoreÚword_n_gram_f_scores              r   Ú_calculate_fscorer’   ç   sâ   € ð@Ý�s¥F˜{Ô+ðÝ:>½sÅF¸{Ô:KðÝZ^Õ_bÕdjÐ_jÔZkðÝsxðå	�c•6ˆkÔ	ðð ð ð ð& -Ð,Ð-BÐDTÐVfÐhlÑmÔmÐØ,Ð,Ð-BÐDTÐVfÐhlÑmÔmÐåÐ#×*Ò*Ñ,Ô,Ñ-Ô-µÐ4G×4NÒ4NÑ4PÔ4PÑ0QÔ0QÑQÕU[Ð\cÑUdÔUdÑdÐdr   ÚtargetsÚpred_char_n_grams_countsÚpred_word_n_grams_countsÚpred_char_n_gramsÚpred_word_n_gramsc                 ót  — t          d¦  «        }t          d„ ¦  «        }t          d„ ¦  «        }t          d„ ¦  «        }t          d„ ¦  «        }| D ]`}t          ||||	|
¦  «        \  }}}}t          ||¦  «        }t          ||¦  «        }t	          ||||||||¦  «        }||k    r
|}|}|}|}|}Œa|||||fS )aS  Calculate the best sentence-level chrF/chrF++ score.

    For a given pre-processed hypothesis, all references are evaluated and score and statistics
    for the best matching reference is returned.

    Args:
        targets: An iterable of references.
        pred_char_n_grams_counts: A dictionary of dictionaries with hypothesis character n-grams.
        pred_word_n_grams_counts: A dictionary of dictionaries with hypothesis word n-grams.
        pred_char_n_grams: A total number of hypothesis character n-grams.
        pred_word_n_grams: A total number of hypothesis word n-grams.
        n_char_order: A character n-gram order.
        n_word_order: A word n-gram order.
        n_order: A sum of character and word n-gram order.
        beta: A parameter determining an importance of recall w.r.t. precision. If `beta=1`, their importance is equal.
        lowercase: An indication whether to enable case-insensitivity.
        whitespace: An indication whether to keep whitespaces during character n-gram extraction.

    Return:
        Return chrF/chrF++ score and statistics for the best matching hypothesis and reference.

        f_score: A sentence-level chrF/chrF++ score.
        matching_char_n_grams:
            A total number of matching character n-grams between the best matching reference and hypothesis.
        matching_word_n_grams:
            A total number of matching word n-grams between the best matching reference and hypothesis.
        target_char_n_grams: A total number of reference character n-grams.
        target_word_n_grams: A total number of reference word n-grams.

    r   c                  ó    — t          d¦  «        S rG   r   rH   r   r   rI   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>L  ó   € ÍÈsÉÌ€ r   c                  ó    — t          d¦  «        S rG   r   rH   r   r   rI   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>M  rš   r   c                  ó    — t          d¦  «        S rG   r   rH   r   r   rI   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>N  ó   € ÅfÈSÁkÄk€ r   c                  ó    — t          d¦  «        S rG   r   rH   r   r   rI   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>O  r�   r   )r   r   rf   rr   r’   )r“   r”   r•   r–   r—   r   r   r|   r}   rP   r-   Úbest_f_scoreÚbest_matching_char_n_gramsÚbest_matching_word_n_gramsÚbest_target_char_n_gramsÚbest_target_word_n_gramsÚtargetÚtarget_char_n_grams_countsÚtarget_word_n_grams_countsÚtarget_char_n_gramsÚtarget_word_n_gramsrv   rw   r�   s                           r   Ú$_calculate_sentence_level_chrf_scorer©      s   € õV ˜#‘;”;€LÝ4?Ð@SÐ@SÑ4TÔ4TÐÝ4?Ð@SÐ@SÑ4TÔ4TÐÝ2=Ð>QÐ>QÑ2RÔ2RÐÝ2=Ð>QÐ>QÑ2RÔ2RÐàð ;ð ;ˆõ 1°¸À|ÐU^Ð`jÑkÔkñ	
Ø&Ø&ØØå 2Ð3MÐOgÑ hÔ hÐÝ 2Ð3MÐOgÑ hÔ hÐå#Ø!Ø!ØØØØØØñ	
ô 	
ˆð �\Ò!Ð!Ø"ˆLØ)>Ð&Ø)>Ð&Ø':Ð$Ø':Ð$øð 	Ø"Ø"Ø Ø ðð r   Úpredsr¤   r%   r&   r'   r(   r)   r*   Úsentence_chrf_scorec                 óè  — t          || ¦  «        \  }} t          | |¦  «        D ]Æ\  }}t          |||	||¦  «        \  }}}}t          ||¦  «        }t          ||¦  «        }t	          |||||||	|
|||¦  «        \  }}}}}|�(|                     |                     d¦  «        ¦  «         t          ||¦  «        }t          ||¦  «        }t          ||¦  «        }t          ||¦  «        }ŒÇ|||||||fS )a  Update function for chrf score.

    Args:
        preds: An iterable of hypothesis corpus.
        target: An iterable of iterables of reference corpus.
        total_preds_char_n_grams: A dictionary containing a total number of hypothesis character n-grams.
        total_preds_word_n_grams: A dictionary containing a total number of hypothesis word n-grams.
        total_target_char_n_grams: A dictionary containing a total number of reference character n-grams.
        total_target_word_n_grams: A dictionary containing a total number of reference word n-grams.
        total_matching_char_n_grams:
            A dictionary containing a total number of matching character n-grams between references and hypotheses.
        total_matching_word_n_grams:
            A dictionary containing a total number of total matching word n-grams between references and hypotheses.
        n_char_order: A character n-gram order.
        n_word_order: A word n-gram order.
        n_order: Sum of character and word n-gram order.
        beta: A parameter determining an importance of recall w.r.t. precision. If `beta=1`, their importance is equal.
        lowercase: An indication whether to enable case-insensitivity.
        whitespace: An indication whether to keep whitespaces during character n-gram extraction.
        sentence_chrf_score: A list of sentence-level chrF/chrF++ scores.

    Return:
        total_target_char_n_grams: number of reference character n-grams.
        total_target_word_n_grams: number of reference word n-grams.
        total_preds_char_n_grams: number of hypothesis character n-grams.
        total_preds_word_n_grams: number of hypothesis word n-grams.
        total_matching_char_n_grams: number of matching character n-grams between references and hypotheses.
        total_matching_word_n_grams: number of total matching word n-grams between references and hypotheses.
        sentence_chrf_score: A list of sentence-level chrF/chrF++ scores.

    Raises:
        ValueError:
            If length of ``preds`` and ``target`` differs.

    Nr   )r   Úziprf   ru   r©   ÚappendÚ	unsqueeze)rª   r¤   r%   r&   r'   r(   r)   r*   r   r   r|   r}   rP   r-   r«   Útarget_corpusÚpredr“   r”   r•   r–   r—   Úsentence_level_f_scorerv   rw   r§   r¨   s                              r   Ú_chrf_score_updater³   v  s_  € õx ,¨F°EÑ:Ô:Ñ€M�5å˜U MÑ2Ô2ð $jð $j‰ˆˆgõ 1°°|À\ÐS\Ð^hÑiÔiñ	
Ø$Ø$ØØå#2Ð3KÐM^Ñ#_Ô#_Ð Ý#2Ð3KÐM^Ñ#_Ô#_Ð õ 1ØØ$Ø$ØØØØØØØØñ
ô 
ñ	
Ø"Ø!Ø!ØØð Ð*Ø×&Ò&Ð'=×'GÒ'GÈÑ'JÔ'JÑKÔKÐKå$3Ð4MÐObÑ$cÔ$cÐ!Ý$3Ð4MÐObÑ$cÔ$cÐ!Ý&5Ð6QÐShÑ&iÔ&iÐ#Ý&5Ð6QÐShÑ&iÔ&iÐ#Ð#ð 	!Ø Ø!Ø!Ø#Ø#Øðð r   c           
      ó.   — t          ||| |||||¦  «        S )ak  Compute chrF/chrF++ score based on pre-computed target, prediction and matching character and word n-grams.

    Args:
        total_preds_char_n_grams: number of hypothesis character n-grams.
        total_preds_word_n_grams: number of hypothesis word n-grams.
        total_target_char_n_grams: number of reference character n-grams.
        total_target_word_n_grams: number of reference word n-grams.
        total_matching_char_n_grams: number of matching character n-grams between references and hypotheses.
        total_matching_word_n_grams: number of total matching word n-grams between references and hypotheses.
        n_order: A sum of character and word n-gram order.
        beta:
            A parameter determining an importance of recall w.r.t. precision. If `beta=1`, their importance is equal.

    Return:
        A corpus-level chrF/chrF++ score.

    )r’   )r%   r&   r'   r(   r)   r*   r|   r}   s           r   Ú_chrf_score_computerµ   å  s.   € õ6 Ø#Ø#Ø Ø Ø!Ø!ØØñ	ô 	ð 	r   é   r†   ç       @FÚreturn_sentence_level_scorec                 óÜ  — t          |t          ¦  «        r|dk     rt          d¦  «        ‚t          |t          ¦  «        r|dk     rt          d¦  «        ‚|dk     rt          d¦  «        ‚t          ||z   ¦  «        }t	          ||¦  «        \  }	}
}}}}|rg nd}t          | ||	|
|||||||||||¦  «        \  }	}
}}}}}t          |	|
||||||¦  «        }|r|t          j        |¦  «        fS |S )ua  Calculate `chrF score`_  of machine translated text with one or more references.

    This implementation supports both chrF score computation introduced in [1] and chrF++ score introduced in
    `chrF++ score`_. This implementation follows the implementations from https://github.com/m-popovic/chrF and
    https://github.com/mjpost/sacrebleu/blob/master/sacrebleu/metrics/chrf.py.

    Args:
        preds: An iterable of hypothesis corpus.
        target: An iterable of iterables of reference corpus.
        n_char_order:
            A character n-gram order. If `n_char_order=6`, the metrics refers to the official chrF/chrF++.
        n_word_order:
            A word n-gram order. If `n_word_order=2`, the metric refers to the official chrF++. If `n_word_order=0`, the
            metric is equivalent to the original chrF.
        beta:
            A parameter determining an importance of recall w.r.t. precision. If `beta=1`, their importance is equal.
        lowercase: An indication whether to enable case-insensitivity.
        whitespace: An indication whether to keep whitespaces during character n-gram extraction.
        return_sentence_level_score: An indication whether a sentence-level chrF/chrF++ score to be returned.

    Return:
        A corpus-level chrF/chrF++ score.
        (Optionally) A list of sentence-level chrF/chrF++ scores if `return_sentence_level_score=True`.

    Raises:
        ValueError:
            If ``n_char_order`` is not an integer greater than or equal to 1.
        ValueError:
            If ``n_word_order`` is not an integer greater than or equal to 0.
        ValueError:
            If ``beta`` is smaller than 0.

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

    References:
        [1] chrF: character n-gram F-score for automatic MT evaluation by Maja PopoviÄ‡ `chrF score`_

        [2] chrF++: words helping character n-grams by Maja PopoviÄ‡ `chrF++ score`_

    r   zMExpected argument `n_char_order` to be an integer greater than or equal to 1.r   zMExpected argument `n_word_order` to be an integer greater than or equal to 0.z.Expected argument `beta` to be greater than 0.N)	Ú
isinstancera   Ú
ValueErrorr�   r+   r³   rµ   rl   Úcat)rª   r¤   r   r   r}   rP   r-   r¸   r|   r%   r&   r'   r(   r)   r*   r«   Úchrf_f_scores                    r   Ú
chrf_scorer¾     se  € õn �l¥CÑ(Ô(ð j¨L¸1Ò,<Ð,<ÝÐhÑiÔiÐiÝ�l¥CÑ(Ô(ð j¨L¸1Ò,<Ð,<ÝÐhÑiÔiÐiØˆa‚x€xÝÐIÑJÔJÐJå�L <Ñ/Ñ0Ô0€Gõ 	˜|¨\Ñ:Ô:ñØ Ø Ø!Ø!Ø#Ø#ð 9TÐ2]°"°"ÐY]Ðõ 	ØØØ Ø Ø!Ø!Ø#Ø#ØØØØØØØñ	ô 	ñØ Ø Ø!Ø!Ø#Ø#Øõ& 'Ø Ø Ø!Ø!Ø#Ø#ØØñ	ô 	€Lð ð <Ø�UœYÐ':Ñ;Ô;Ð;Ð;ØÐr   r=   )r¶   r†   r·   FFF)&Úcollectionsr   Úcollections.abcr   Ú	itertoolsr   Útypingr   r   r	   rl   r
   r   Ú#torchmetrics.functional.text.helperr   rˆ   Úsetr9   ra   rK   rc   r+   r`   rb   r1   r4   r:   rA   rO   rf   rr   ru   r�   r’   r©   r³   rµ   r¾   rH   r   r   ú<module>rÅ      s0  ðð. $Ð #Ð #Ð #Ð #Ð #Ø $Ð $Ð $Ð $Ð $Ð $Ø Ð Ð Ð Ð Ð Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à @Ð @Ð @Ð @Ð @Ð @à�˜‘”€à�Ð8Ñ9Ô9€ðØðØ%(ðà
ØˆˆfˆÔ�t˜C ˜KÔ(¨$¨s°F¨{Ô*;¸TÀ#ÀvÀ+Ô=NÐPTÐUXÐZ`ÐU`ÔPaÐcgÐhkÐmsÐhsÔctÐtôðð ð ð ðB3˜cð 3¨tð 3¸¸S¼	ð 3ð 3ð 3ð 3ð ¨ð °°c´ð ð ð ð ð.
p¨ð 
p°°c´ð 
pð 
pð 
pð 
pð T¨#¤Yð ¸cð ÀdÈ3ÐPTÐUZÐ[^Ð`cÐ[cÔUdÐflÐUlÔPmÐKmÔFnð ð ð ð ð$0\Øð0\Ø!$ð0\Ø47ð0\ØDHð0\ØVZð0\à
Øˆˆd�5˜˜c˜”? FÐ*Ô+Ð	+Ô,Øˆˆd�5˜˜c˜”? FÐ*Ô+Ð	+Ô,ØˆˆfˆÔØˆˆfˆÔðôð0\ð 0\ð 0\ð 0\ðfØ˜S $ u¨S°#¨X¤¸Ð'>Ô"?Ð?Ô@ðà˜S $ u¨S°#¨X¤¸Ð'>Ô"?Ð?Ô@ðð 
ˆ#ˆvˆ+Ôðð ð ð ð. 4¨¨V¨Ô#4ð ¸tÀCÈÀKÔ?Pð ÐUYÐZ]Ð_eÐZeÔUfð ð ð ð ð 6eØ  V Ô,ð6eà  V Ô,ð6eð ˜3 ˜;Ô'ð6eð ˜3 ˜;Ô'ð	6eð
 ˜3 ˜;Ô'ð6eð ˜3 ˜;Ô'ð6eð ð6eð ð6eð ð6eð 6eð 6eð 6eðrSØ�#ŒYðSà" 3¨¨U°3¸°8¬_¸fÐ-DÔ(EÐ#EÔFðSð # 3¨¨U°3¸°8¬_¸fÐ-DÔ(EÐ#EÔFðSð ˜C ˜KÔ(ð	Sð
 ˜C ˜KÔ(ðSð ðSð ðSð ðSð ðSð ðSð ðSð ˆ6�4˜˜V˜Ô$ d¨3°¨;Ô&7¸¸cÀ6¸kÔ9JÈDÐQTÐV\ÐQ\ÔL]Ð]Ô^ðSð Sð Sð SðJ 37ðlð lØ��h˜s”mÐ#Ô$ðlà�(˜3”- ¨(°3¬-Ô!8Ð8Ô9ðlð # 3¨ ;Ô/ðlð # 3¨ ;Ô/ð	lð
  $ C¨ KÔ0ðlð  $ C¨ KÔ0ðlð "& c¨6 kÔ!2ðlð "& c¨6 kÔ!2ðlð ðlð ðlð ðlð ðlð ðlð ðlð " $ v¤,Ô/ðlð  ØˆˆfˆÔØˆˆfˆÔØˆˆfˆÔØˆˆfˆÔØˆˆfˆÔØˆˆfˆÔØˆT�&Œ\Ôðôð!lð lð lð lð^$Ø" 3¨ ;Ô/ð$à" 3¨ ;Ô/ð$ð  $ C¨ KÔ0ð$ð  $ C¨ KÔ0ð	$ð
 "& c¨6 kÔ!2ð$ð "& c¨6 kÔ!2ð$ð ð$ð ð$ð ð$ð $ð $ð $ðT ØØØØØ(-ðrð rØ��h˜s”mÐ#Ô$ðrà�U˜3 ¨¤Ð-Ô.Ô/ðrð ðrð ð	rð
 ðrð ðrð ðrð "&ðrð ˆ6�5˜ ˜Ô(Ð(Ô)ðrð rð rð rð rð rr   