Ë
    þÍ: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$	 	 	 	 	 	 d=d/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%y)>é    ©Údefaultdict)ÚSequence)Úchain)ÚListÚOptionalÚUnionN)ÚTensorÚtensor)Ú_validate_inputsg¼‰Ø—²Òœ<z !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~Ún_char_orderÚn_word_orderÚreturnc                 ó
  — t        | «      D �ci c]  }|dz   t        d«      “Œ }}t        |«      D �ci c]  }|dz   t        d«      “Œ }}t        | «      D �ci c]  }|dz   t        d«      “Œ }}t        |«      D �ci c]  }|dz   t        d«      “Œ }}t        | «      D �ci c]  }|dz   t        d«      “Œ }}t        |«      D �ci c]  }|dz   t        d«      “Œ }}||||||fS c c}w c c}w c c}w c c}w c c}w c c}w )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.

    é   ç        )Úranger   )	r   r   ÚnÚ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	            úv/home/mcse/projects/srt_converter/srt-converter-venv/lib/python3.12/site-packages/torchmetrics/functional/text/chrf.pyÚ_prepare_n_grams_dictsr   &   s#  € ô  PUÐUaÓObÖ2cÈ!°1°q±5¼&À»+Ñ3EÐ2cÐÐ2cÜOTÐUaÓObÖ2cÈ!°1°q±5¼&À»+Ñ3EÐ2cÐÐ2cÜPUÐVbÓPcÖ3dÈ1°A¸±E¼6À#»;Ñ4FÐ3dÐÐ3dÜPUÐVbÓPcÖ3dÈ1°A¸±E¼6À#»;Ñ4FÐ3dÐÐ3dÜRWÐXdÓReÖ5fÈQ°a¸!±e¼VÀC»[Ñ6HÐ5fÐÐ5fÜRWÐXdÓReÖ5fÈQ°a¸!±e¼VÀC»[Ñ6HÐ5fÐÐ5fð 	!Ø Ø!Ø!Ø#Ø#ðð ùò 3dùÚ2cùÚ3dùÚ3dùÚ5fùÚ5fs"   ŽC'³C,ÁC1Á=C6Â"C;ÃD ÚsentenceÚ
whitespacec                 ón   — |rt        | «      S t        | j                  «       j                  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_charactersr%   G   s0   € ñ Ü�H‹~ÐÜ�—‘Ó ×(Ñ(¨¨bÓ1Ó2Ð2ó    Ú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)r'   s    r   Ú_separate_word_and_punctuationr,   W   s_   € ô ˆ4ƒy�A‚~ØˆvˆàˆB�x”=Ñ Ø�S�b�	˜4 ™8Ð$Ð$ØˆA�w”-ÑØ�Q‘˜˜a˜b˜Ð"Ð"Øˆ6€Mr&   c                 ó„   — t        t        j                  d„ | j                  «       j	                  «       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   ó2   K  — | ]  }t        |«      –— Œ y ­w©N)r,   )Ú.0r'   s     r   ú	<genexpr>z-_get_words_and_punctuation.<locals>.<genexpr>x   s   è ø€ Ò#nÈTÔ$BÀ4×$HÑ#nùs   ‚)r"   r   Úfrom_iterabler#   Úsplit)r   s    r   Ú_get_words_and_punctuationr4   n   s3   € ô ”×#Ñ#Ñ#nÐU]×UcÑUcÓUe×UkÑUkÓUmÔ#nÓnÓoÐor&   Úchar_or_word_listÚn_gram_order.c                 óÎ   ‡ ‡— t        d„ «      }t        d|dz   «      D ]D  Šˆ ˆfd„t        t        ‰ «      ‰z
  dz   «      D «       D ]  }|‰   |xx   t        d«      z  cc<   Œ ŒF |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€ r&   r   r<   r&   r   r=   z_ngram_counts.<locals>.<lambda>†   s   € Ì;ÑWjÓKk€ r&   r   c              3   ó@   •K  — | ]  }t        ‰||‰z    «      –— Œ y ­wr/   )Útuple)r0   Úir5   r   s     €€r   r1   z _ngram_counts.<locals>.<genexpr>ˆ   s"   øè ø€ Òi¸a”eÐ-¨a°!°a±%Ð8×9Ñiùs   ƒ)r   r   r*   r   )r5   r6   ÚngramsÚngramr   s   `   @r   Ú_ngram_countsrC   {   sv   ù€ ô 8CÑCkÓ7l€FÜ�1�l QÑ&Ó'ò *ˆÜiÄ5ÌÐM^ÓI_ÐbcÑIcÐfgÑIgÓChÔiò 	*ˆEØ�1‰I�eÓ¤ q£	Ñ)Ôñ	*ð*ð €Mr&   Ú	lowercasec                 óº  ‡— 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   rD   r   .c                 ó†   •— |r| j                  «       } t        t        | ‰«      |«      }t        t        | «      |«      }||fS )z@Get a dictionary of dictionaries with a counts of given n-grams.)ÚlowerrC   r%   r4   )r   r   r   rD   Ú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¦   sI   ø€ ñ Ø—~‘~Ó'ˆHÜ+¬O¸HÀjÓ,QÐS_Ó`ÐÜ+Ô,FÀxÓ,PÐR^Ó_ÐØ"Ð$7Ð7Ð7r&   Ún_grams_countsc                 ó¢   — t        d„ «      }| D ]=  }t        | |   j                  «       «      j                  «       j	                  «       ||<   Œ? |S )z.Get total sum of n-grams over n-grams w.r.t n.c                  ó   — t        d«      S r:   r;   r<   r&   r   r=   zQ_get_n_grams_counts_and_total_ngrams.<locals>._get_total_ngrams.<locals>.<lambda>²   s
   € ¼vÀc»{€ r&   )r   ÚsumÚvaluesÚdetachÚclone)rK   Útotal_n_gramsr   s      r   Ú_get_total_ngramsz?_get_n_grams_counts_and_total_ngrams.<locals>._get_total_ngrams°   sS   € ä+6Ñ7JÓ+KˆØò 	PˆAÜ" >°!Ñ#4×#;Ñ#;Ó#=Ó>×EÑEÓG×MÑMÓOˆM˜!Òð	PàÐr&   )ÚstrÚintÚboolr?   Údictr
   )r   r   r   rD   r   rJ   rS   rH   rI   Útotal_char_n_gramsÚtotal_word_n_gramss       `      r   Ú$_get_n_grams_counts_and_total_ngramsrZ   �   sù   ø€ ð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ð¬$¬s´D¼¼sÀC¸x¹Ì&Ð9PÑ4QÐ/QÑ*Rð ÔW[Ô\_ÔagÐ\gÑWhó ñ 0LØ�, ¨ió0Ñ,ÐÐ,ñ +Ð+>Ó?ÐÙ*Ð+>Ó?ÐàÐ 3Ð5GÐI[Ð[Ð[r&   Úhyp_n_grams_countsÚref_n_grams_countsc           	      óî   — t        d„ «      }| D ]^  }| |   D �cg c]$  }t        j                  ||   |   | |   |   «      ‘Œ& }}t        |«      j	                  «       j                  «       ||<   Œ` |S c c}w )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 r:   r;   r<   r&   r   r=   z$_get_ngram_matches.<locals>.<lambda>Î   s
   € ¼fÀS»k€ r&   )r   ÚtorchÚminrN   rP   rQ   )r[   r\   Úmatching_n_gramsr   Ún_gramÚmin_n_gramss         r   Ú_get_ngram_matchesrd   À   s“   € ô +6Ñ6IÓ*JÐØò @ˆàbtÐuvÑbwö
ØX^ŒE�I‰IÐ(¨Ñ+¨FÑ3Ð5GÈÑ5JÈ6Ñ5RÕSð
ˆð 
ô " +Ó.×5Ñ5Ó7×=Ñ=Ó?Ð˜Òð	@ð
 Ðùò	
s   ™)A2rR   Ún_gramsc                 ó4   — |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.

    r<   )rR   re   r   s      r   Ú_sum_over_dictsrg   ×   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                 óX  — 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	        |	j                  «       «      t	        |
j                  «       «      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.

    ra   Úref_n_gramsÚhyp_n_gramsro   r   c           	      óœ  — | D �ci c]!  }|||   dkD  r| |   ||   z  n
t        d«      “Œ# }}| D �ci c]!  }|||   dkD  r| |   ||   z  n
t        d«      “Œ# }}| D �ci c],  }|t        j                  |dz  ||   z  ||   z   t        «      “Œ. }}| D �ci c]  }|d|dz  z   ||   z  ||   z  ||   z  “Œ }}|S c c}w c c}w c c}w c c}w )zGet n-gram level f-score.r   r   é   r   )r   r_   ÚmaxÚ_EPS_SMOOTHING)	ra   rq   rr   ro   r   Ú	precisionÚrecallÚdenominatorÚf_scores	            r   Ú_get_n_gram_fscorez-_calculate_fscore.<locals>._get_n_gram_fscore  s/  € ð
 euö(
Ø_`ˆA°{À1±~ÈÒ7IÐ Ñ" [°¡^Ò3ÌvÐVYË{ÑZð(
ˆ	ð (
ð euö%
Ø_`ˆA°{À1±~ÈÒ7IÐ Ñ" [°¡^Ò3ÌvÐVYË{ÑZð%
ˆð %
ð Wgö*
ØQRˆAŒu�y‰y˜˜q™ 9¨Q¡<Ñ/°&¸±)Ñ;¼^ÓLÑLð*
ˆð *
ð Scö&
ØMNˆA��D˜!‘G‘˜y¨™|Ñ+¨f°Q©iÑ7¸+Àa¹.ÑHÑHð&
ˆð &
ð ˆùò(
ùò%
ùò*
ùò&
s   …&B:±&B?Á1CÂ"C	)rW   rU   r
   ÚfloatrN   rO   r   )rh   ri   rj   rk   rl   rm   rn   ro   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ÐÙ,Ð-BÐDTÐVfÐhlÓmÐäÐ#×*Ñ*Ó,Ó-´Ð4G×4NÑ4NÓ4PÓ0QÑQÔU[Ð\cÓUdÑdÐdr&   ÚtargetsÚpred_char_n_grams_countsÚpred_word_n_grams_countsÚpred_char_n_gramsÚpred_word_n_gramsc                 ó0  — t        d«      }t        d„ «      }t        d„ «      }t        d„ «      }t        d„ «      }| D ]P  }t        ||||	|
«      \  }}}}t        ||«      }t        ||«      }t	        ||||||||«      }||kD  sŒG|}|}|}|}|}ŒR |||||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 r:   r;   r<   r&   r   r=   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>L  ó
   € ÌÈsË€ r&   c                  ó   — t        d«      S r:   r;   r<   r&   r   r=   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>M  r‡   r&   c                  ó   — t        d«      S r:   r;   r<   r&   r   r=   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>N  ó
   € ÄfÈSÃk€ r&   c                  ó   — t        d«      S r:   r;   r<   r&   r   r=   z6_calculate_sentence_level_chrf_score.<locals>.<lambda>O  rŠ   r&   )r   r   rZ   rd   r   )r€   r�   r‚   rƒ   r„   r   r   rn   ro   rD   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_gramsrh   ri   rz   s                           r   Ú$_calculate_sentence_level_chrf_scorer–      sô   € ôV ˜#“;€LÜ4?Ñ@SÓ4TÐÜ4?Ñ@SÓ4TÐÜ2=Ñ>QÓ2RÐÜ2=Ñ>QÓ2RÐàò ;ˆô 1°¸À|ÐU^Ð`jÓkñ	
Ø&Ø&ØØä 2Ð3MÐOgÓ hÐÜ 2Ð3MÐOgÓ hÐä#Ø!Ø!ØØØØØØó	
ˆð �\Ó!Ø"ˆLØ)>Ð&Ø)>Ð&Ø':Ð$Ø':Ñ$ð5;ð: 	Ø"Ø"Ø Ø ðð r&   Úpredsr‘   r   r   r   r   r   r   Úsentence_chrf_scorec                 óŒ  — t        || «      \  }} t        | |«      D ]ž  \  }}t        |||	||«      \  }}}}t        ||«      }t        ||«      }t	        |||||||	|
|||«      \  }}}}}|� |j                  |j                  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.

    r   )r   ÚziprZ   rg   r–   ÚappendÚ	unsqueeze)r—   r‘   r   r   r   r   r   r   r   r   rn   ro   rD   r   r˜   Útarget_corpusÚpredr€   r�   r‚   rƒ   r„   Úsentence_level_f_scorerh   ri   r”   r•   s                              r   Ú_chrf_score_updater    v  s5  € ôx ,¨F°EÓ:Ñ€M�5ä˜U MÓ2ò $j‰ˆˆgô 1°°|À\ÐS\Ð^hÓiñ	
Ø$Ø$ØØä#2Ð3KÐM^Ó#_Ð Ü#2Ð3KÐM^Ó#_Ð ô 1ØØ$Ø$ØØØØØØØØó
ñ	
Ø"Ø!Ø!ØØð Ð*Ø×&Ñ&Ð'=×'GÑ'GÈÓ'JÔKä$3Ð4MÐObÓ$cÐ!Ü$3Ð4MÐObÓ$cÐ!Ü&5Ð6QÐShÓ&iÐ#Ü&5Ð6QÐShÓ&iÑ#ðI$jðN 	!Ø Ø!Ø!Ø#Ø#Øðð 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   rn   ro   s           r   Ú_chrf_score_computer¢   å  s)   € ô6 Ø#Ø#Ø Ø Ø!Ø!ØØó	ð 	r&   Ú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)	Ú
isinstancerU   Ú
ValueErrorr|   r   r    r¢   r_   Úcat)r—   r‘   r   r   ro   rD   r   r£   rn   r   r   r   r   r   r   r˜   Úchrf_f_scores                    r   Ú
chrf_scorer©     s'  € ôn �l¤CÔ(¨L¸1Ò,<ÜÐhÓiÐiÜ�l¤CÔ(¨L¸1Ò,<ÜÐhÓiÐiØˆa‚xÜÐIÓJÐJä�L <Ñ/Ó0€Gô 	˜|¨\Ó:ñØ Ø Ø!Ø!Ø#Ø#ñ 9T±"ÐY]Ðô 	ØØØ Ø Ø!Ø!Ø#Ø#ØØØØØØØó	ñØ Ø Ø!Ø!Ø#Ø#Øô& 'Ø Ø Ø!Ø!Ø#Ø#ØØó	€Lñ ØœUŸY™YÐ':Ó;Ð;Ð;ØÐr&   r/   )é   rt   g       @FFF)&Úcollectionsr   Úcollections.abcr   Ú	itertoolsr   Útypingr   r   r	   r_   r
   r   Ú#torchmetrics.functional.text.helperr   rv   Úsetr+   rU   r?   rW   r   rT   rV   r"   r%   r,   r4   rC   rZ   rd   rg   r|   r   r–   r    r¢   r©   r<   r&   r   ú<module>r±      sË  ðõ. $Ý $Ý ß (Ñ (ã ß  å @á˜“€áÐ8Ó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ð ¨ð °°c±ó ð.
p¨ð 
p°°c±ó 
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\ð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ð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ðJ 37ñ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ð^$Ø" 3¨ ;Ñ/ð$à" 3¨ ;Ñ/ð$ð  $ C¨ KÑ0ð$ð  $ C¨ KÑ0ð	$ð
 "& c¨6 kÑ!2ð$ð "& c¨6 kÑ!2ð$ð ð$ð ð$ð ó$ðT ØØØØØ(-ñrØ��h˜s‘mÐ#Ñ$ðrà�U˜3 ¨¡Ð-Ñ.Ñ/ðrð ðrð ð	rð
 ðrð ðrð ðrð "&ðrð ˆ6�5˜ ˜Ñ(Ð(Ñ)ôrr&   