§
    ’©ºjú(  ã            	       ó8  — d dl Z d dlmZmZ d dlmZmZmZmZ d dl	Z	d dl	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ d dlmZmZ esd	gZd
ZdZdZ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         Z G d„ de¦  «        ZdS )é    N)ÚIteratorÚSequence)ÚAnyÚListÚOptionalÚUnion)ÚTensorÚtensor)ÚMetric)Ú_chrf_score_computeÚ_chrf_score_updateÚ_prepare_n_grams_dicts)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzCHRFScore.plot)ÚcharÚword)ÚpredsÚtargetÚmatching)Ú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_gramsc                   ó  ‡ — e Zd ZU dZdZeed<   dZeed<   dZeed<   dZ	e
ed<   d	Ze
ed
<   dZeee                  ed<   	 	 	 	 	 	 d)dedede
dededededdfˆ fd„Zdee         deee                  ddfd„Zdeeeeef         f         fd„Zdefd„Zdeddfd„Zed ed!edefd"„¦   «         Zed ed!ed#edefd$„¦   «         Zdeeeeef         ef                  fd%„Z 	 d*d&eeeee         f                  d'ee!         de"fd(„Z#ˆ xZ$S )+Ú	CHRFScoreaÈ  Calculate `chrf score`_ of machine translated text with one or more references.

    This implementation supports both ChrF score computation introduced in `chrF score`_ 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.

    As input to ``forward`` and ``update`` the metric accepts the following input:

    - ``preds`` (:class:`~Sequence`): An iterable of hypothesis corpus
    - ``target`` (:class:`~Sequence`): An iterable of iterables of reference corpus

    As output of ``forward`` and ``compute`` the metric returns the following output:

    - ``chrf`` (:class:`~torch.Tensor`): If `return_sentence_level_score=True` return a list of sentence-level
      chrF/chrF++ scores, else return a corpus-level chrF/chrF++ score

    Args:
        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: 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 keep whitespaces during n-gram extraction.
        return_sentence_level_score: An indication whether a sentence-level chrF/chrF++ score to be returned.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    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.text import CHRFScore
        >>> preds = ['the cat is on the mat']
        >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
        >>> chrf = CHRFScore()
        >>> chrf(preds, target)
        tensor(0.8640)

    FÚis_differentiableTÚhigher_is_betterÚfull_state_updateç        Úplot_lower_boundg      ð?Úplot_upper_boundNÚsentence_chrf_scoreé   é   ç       @Ún_char_orderÚn_word_orderÚbetaÚ	lowercaseÚ
whitespaceÚreturn_sentence_level_scoreÚkwargsÚreturnc                 ó®  •—  t          ¦   «         j        di |¤Ž t          |t          ¦  «        r|dk     rt	          d¦  «        ‚|| _        t          |t          ¦  «        r|dk     rt	          d¦  «        ‚|| _        |dk     rt	          d¦  «        ‚|| _        || _        || _	        || _
        t          ||z   ¦  «        | _        |                      ¦   «         D ]Z\  \  }}	}
t          d|	dz   ¦  «        D ]>}|                      |
||¦  «        }|                      |t#          d¦  «        d¬¦  «         Œ?Œ[| j
        r|                      d	g d
¬¦  «         d S d S )Né   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.r"   Úsum)Údist_reduce_fxr%   Úcat© )ÚsuperÚ__init__Ú
isinstanceÚintÚ
ValueErrorr)   r*   r+   r,   r-   r.   ÚfloatÚn_orderÚ_get_text_n_gram_iteratorÚrangeÚ_get_state_nameÚ	add_stater
   )Úselfr)   r*   r+   r,   r-   r.   r/   Ún_gram_levelÚn_gram_orderÚtextÚnÚ
state_nameÚ	__class__s                €úf/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/text/chrf.pyr8   zCHRFScore.__init__j   s“  ø€ ð 	�‰ŒÔÐ"Ð"˜6Ð"Ð"Ð"å˜,­Ñ,Ô,ð 	n°¸qÒ0@Ð0@ÝÐlÑmÔmÐmØ(ˆÔÝ˜,­Ñ,Ô,ð 	n°¸qÒ0@Ð0@ÝÐlÑmÔmÐmØ(ˆÔØ�!Š8ˆ8ÝÐMÑNÔNÐNØˆŒ	Ø"ˆŒØ$ˆŒØ+FˆÔ(å˜\¨LÑ8Ñ9Ô9ˆŒð 37×2PÒ2PÑ2RÔ2Rð 	Nð 	NÑ.Ñ(ˆ\˜<¨$Ý˜1˜l¨QÑ.Ñ/Ô/ð Nð N�Ø!×1Ò1°$¸ÀaÑHÔH�
Ø—’˜z­6°#©;¬;Àu�ÑMÔMÐMÐMðNð Ô+ð 	LØ�NŠNÐ0°"ÀUˆNÑKÔKÐKÐKÐKð	Lð 	Ló    r   r   c                 ó   — t          ||g|                      ¦   «         ¢| j        ‘| j        ‘| j        ‘| j        ‘| j        ‘| j        ‘| j        r| j	        nd‘R Ž }|  
                    |dd…         ¦  «         | j	        �|d         | _	        dS dS )z*Update state with predictions and targets.Néÿÿÿÿ)r   Ú_convert_states_to_dictsr)   r*   r=   r+   r,   r-   r.   r%   Ú_update_states_from_dicts)rB   r   r   Ún_grams_dicts_tuples       rI   ÚupdatezCHRFScore.updateŽ   sÛ   € å0ØØð
ð ×*Ò*Ñ,Ô,ð
ð Ôð	
ð
 Ôð
ð ŒLð
ð ŒIð
ð ŒNð
ð ŒOð
ð )-Ô(HÐRˆDÔ$Ð$Èdð
ð 
ð 
Ðð 	×&Ò&Ð':¸3¸B¸3Ô'?Ñ@Ô@Ð@ØÔ#Ð/Ø':¸2Ô'>ˆDÔ$Ð$Ð$ð 0Ð/rJ   c                 óò   — | j         �Et          g |                      ¦   «         ¢| j        ‘| j        ‘R Ž t          j        | j         ¦  «        fS t          g |                      ¦   «         ¢| j        ‘| j        ‘R Ž S )zCalculate chrF/chrF++ score.)r%   r   rM   r=   r+   Útorchr5   ©rB   s    rI   ÚcomputezCHRFScore.compute    s„   € àÔ#Ð/å#Ð^ T×%BÒ%BÑ%DÔ%DÐ^ÀdÄlÐ^ÐTXÔT]Ð^Ð^Ð^Ý”	˜$Ô2Ñ3Ô3ðð õ #Ð] D×$AÒ$AÑ$CÔ$CÐ]ÀTÄ\Ð]ÐSWÔS\Ð]Ð]Ð]Ð]rJ   c           	      óª  — t          t          t          t          | j        | j        ¦  «        ¦  «        ¦  «        }|                      ¦   «         D ]d\  \  }}}t          d|dz   ¦  «        D ]H}|                      ||¦  «        }|  	                    |||¦  «        }t          | |¦  «        ||         |<   ŒIŒet          |                     ¦   «         ¦  «        S )z_Convert global metric states to the n-gram dictionaries to be passed in ``_chrf_score_update``.r2   )ÚdictÚzipÚ_DICT_STATES_NAMESr   r)   r*   r>   r?   Ú_get_dict_namer@   ÚgetattrÚtupleÚvalues)rB   Ún_grams_dictsrC   rD   rE   rF   Ú	dict_namerG   s           rI   rM   z"CHRFScore._convert_states_to_dicts©   sã   € å6:ÝÕ"Õ$:¸4Ô;LÈdÔN_Ñ$`Ô$`ÑaÔañ7
ô 7
ˆð 37×2PÒ2PÑ2RÔ2Rð 	Hð 	HÑ.Ñ(ˆ\˜<¨$Ý˜1˜l¨QÑ.Ñ/Ô/ð Hð H�Ø ×/Ò/°°lÑCÔC�	Ø!×1Ò1°$¸ÀaÑHÔH�
å.5°d¸JÑ.GÔ.G�˜iÔ(¨Ñ+Ð+ð	Hõ �]×)Ò)Ñ+Ô+Ñ,Ô,Ð,rJ   rO   c                 óD  — t          t          t          |¦  «        ¦  «        }|                      ¦   «         D ]h\  \  }}}t	          d|dz   ¦  «        D ]L}|                      ||¦  «        }|                      |||¦  «        }t          | |||         |         ¦  «         ŒMŒidS )z]Update global metric states based on the n-gram dictionaries calculated on the current batch.r2   N)rV   rW   rX   r>   r?   rY   r@   Úsetattr)	rB   rO   r]   rC   rD   rE   rF   r^   rG   s	            rI   rN   z#CHRFScore._update_states_from_dicts¸   s¿   € å�SÕ!3Ð5HÑIÔIÑJÔJˆØ26×2PÒ2PÑ2RÔ2Rð 	Gð 	GÑ.Ñ(ˆ\˜<¨$Ý˜1˜l¨QÑ.Ñ/Ô/ð Gð G�Ø ×/Ò/°°lÑCÔC�	Ø!×1Ò1°$¸ÀaÑHÔH�
å˜˜j¨-¸	Ô*BÀ1Ô*EÑFÔFÐFÐFð	Gð	Gð 	GrJ   rE   rC   c                 ó   — d| › d|› d�S )z*Return a dictionary name w.r.t input args.Útotal_Ú_Ú_n_gramsr6   )rE   rC   s     rI   rY   zCHRFScore._get_dict_nameÂ   s   € ð 6˜Ð5Ð5˜|Ð5Ð5Ð5Ð5rJ   rF   c                 ó   — d| › d|› d|› d�S )z,Return a metric state name w.r.t input args.rb   rc   Ú_gramsr6   )rE   rC   rF   s      rI   r@   zCHRFScore._get_state_nameÇ   s%   € ð 8˜Ð7Ð7˜|Ð7Ð7¨aÐ7Ð7Ð7Ð7rJ   c                 ót   — t          j        t          t          | j        | j        g¦  «        t          ¦  «        S )zKGet iterator over char/word and reference/hypothesis/matching n-gram level.)Ú	itertoolsÚproductrW   Ú_N_GRAM_LEVELSr)   r*   Ú_TEXT_LEVELSrS   s    rI   r>   z#CHRFScore._get_text_n_gram_iteratorÌ   s+   € åÔ ¥¥^°dÔ6GÈÔIZÐ5[Ñ!\Ô!\Õ^jÑkÔkÐkrJ   ÚvalÚaxc                 ó.   — |                       ||¦  «        S )aŒ  Plot a single or multiple values from the metric.

        Args:
            val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
                If no value is provided, will automatically call `metric.compute` and plot that result.
            ax: An matplotlib axis object. If provided will add plot to that axis

        Returns:
            Figure and Axes object

        Raises:
            ModuleNotFoundError:
                If `matplotlib` is not installed

        .. plot::
            :scale: 75

            >>> # Example plotting a single value
            >>> from torchmetrics.text import CHRFScore
            >>> metric = CHRFScore()
            >>> preds = ['the cat is on the mat']
            >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torchmetrics.text import CHRFScore
            >>> metric = CHRFScore()
            >>> preds = ['the cat is on the mat']
            >>> target = [['there is a cat on the mat', 'a cat is on the mat']]
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )Ú_plot)rB   rl   rm   s      rI   ÚplotzCHRFScore.plotÐ   s   € ðT �zŠz˜#˜rÑ"Ô"Ð"rJ   )r&   r'   r(   FFF)NN)%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚboolÚ__annotations__r    r!   r#   r<   r$   r%   r   r   r	   r:   r   r8   r   ÚstrrP   r   r[   rT   Ú_DICT_STATES_TYPESrM   rN   ÚstaticmethodrY   r@   r   r>   r   r   rp   Ú__classcell__)rH   s   @rI   r   r   5   sÛ  ø€ € € € € € ð*ð *ðX $Ð�tÐ#Ð#Ñ#Ø!Ð�dÐ!Ð!Ñ!Ø"Ð�tÐ"Ð"Ñ"Ø!Ð�eÐ!Ð!Ñ!Ø!Ð�eÐ!Ð!Ñ!à26Ð˜ $ v¤,Ô/Ð6Ð6Ñ6ð ØØØØ Ø,1ð"Lð "Làð"Lð ð"Lð ð	"Lð
 ð"Lð ð"Lð &*ð"Lð ð"Lð 
ð"Lð "Lð "Lð "Lð "Lð "LðH?˜H SœMð ?°8¸HÀS¼MÔ3Jð ?Ètð ?ð ?ð ?ð ?ð$^˜˜v u¨V°V¨^Ô'<Ð<Ô=ð ^ð ^ð ^ð ^ð-Ð*<ð -ð -ð -ð -ðGÐ=Oð GÐTXð Gð Gð Gð Gð ð6˜Sð 6°ð 6¸ð 6ð 6ð 6ñ „\ð6ð ð8˜cð 8°ð 8¸ð 8Àð 8ð 8ð 8ñ „\ð8ðl¨8°E¸%ÀÀSÀ¼/È3Ð:NÔ4OÔ+Pð lð lð lð lð
 _cð*#ð *#Ø˜E &¨(°6Ô*:Ð":Ô;Ô<ð*#ØIQÐRZÔI[ð*#à	ð*#ð *#ð *#ð *#ð *#ð *#ð *#ð *#rJ   r   ) rh   Úcollections.abcr   r   Útypingr   r   r   r   rR   r	   r
   Útorchmetricsr   Ú!torchmetrics.functional.text.chrfr   r   r   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__rj   rk   rX   r[   rV   r:   rx   r   r6   rJ   rI   ú<module>r‚      sˆ  ðð( Ð Ð Ð Ø .Ð .Ð .Ð .Ð .Ð .Ð .Ð .Ø -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -Ð -à €€€Ø  Ð  Ð  Ð  Ð  Ð  Ð  Ð  à Ð Ð Ð Ð Ð Ø mÐ mÐ mÐ mÐ mÐ mÐ mÐ mÐ mÐ mØ @Ð @Ð @Ð @Ð @Ð @Ø @Ð @Ð @Ð @Ð @Ð @Ð @Ð @àð *Ø(Ð)Ðð "€Ø.€ðÐ ð ØˆˆfˆÔ�t˜C ˜KÔ(¨$¨s°F¨{Ô*;¸TÀ#ÀvÀ+Ô=NÐPTÐUXÐZ`ÐU`ÔPaÐcgÐhkÐmsÐhsÔctÐtôÐ ð
E#ð E#ð E#ð E#ð E#�ñ E#ô E#ð E#ð E#ð E#rJ   