Ë
    ýÿæim  ã                   óœ   — d dl 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mZmZ d dlmZ d dlmZ d dlmZmZ esd	gZ G d
„ de«      Zy)é    )ÚSequence)ÚAnyÚListÚOptionalÚUnionN)ÚTensorÚtensor)Ú_ter_computeÚ_ter_updateÚ_TercomTokenizer)ÚMetric)Ú_MATPLOTLIB_AVAILABLE)Ú_AX_TYPEÚ_PLOT_OUT_TYPEzTranslationEditRate.plotc                   óP  ‡ — 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	<   eed
<   eed<   dZeee      ed<   	 	 	 	 	 ddededededededdfˆ fd„Zdeeee   f   deeeee   f      ddfd„Zdeeeeef   f   fd„Z	 ddeeeee   f      dee   defd„Zˆ xZS )ÚTranslationEditRateau  Calculate Translation edit rate (`TER`_)  of machine translated text with one or more references.

    This implementation follows the one from `SacreBleu_ter`_, which is a
    near-exact reimplementation of the Tercom algorithm, produces identical results on all "sane" outputs.

    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:

    - ``ter`` (:class:`~torch.Tensor`): if ``return_sentence_level_score=True`` return a corpus-level translation
      edit rate with a list of sentence-level translation_edit_rate, else return a corpus-level translation edit rate

    Args:
        normalize: An indication whether a general tokenization to be applied.
        no_punctuation: An indication whteher a punctuation to be removed from the sentences.
        lowercase: An indication whether to enable case-insensitivity.
        asian_support: An indication whether asian characters to be processed.
        return_sentence_level_score: An indication whether a sentence-level TER to be returned.
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

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

    FÚis_differentiableÚhigher_is_betterÚfull_state_updateç        Úplot_lower_boundg      ð?Úplot_upper_boundÚtotal_num_editsÚtotal_tgt_lenNÚsentence_terÚ	normalizeÚno_punctuationÚ	lowercaseÚasian_supportÚreturn_sentence_level_scoreÚkwargsÚreturnc                 ó  •— t        ‰| �  di |¤Ž t        |t        «      st	        d|› d�«      ‚t        |t        «      st	        d|› d�«      ‚t        |t        «      st	        d|› d�«      ‚t        |t        «      st	        d|› d�«      ‚t        ||||«      | _        || _        | j                  dt        d«      d¬	«       | j                  d
t        d«      d¬	«       | j                  r| j                  dg d¬	«       y y )Nz<Expected argument `normalize` to be of type boolean but got Ú.zAExpected argument `no_punctuation` to be of type boolean but got z<Expected argument `lowercase` to be of type boolean but got z@Expected argument `asian_support` to be of type boolean but got r   r   Úsum)Údist_reduce_fxr   r   Úcat© )
ÚsuperÚ__init__Ú
isinstanceÚboolÚ
ValueErrorr   Ú	tokenizerr    Ú	add_stater	   )Úselfr   r   r   r   r    r!   Ú	__class__s          €új/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/torchmetrics/text/ter.pyr*   zTranslationEditRate.__init__J   s  ø€ ô 	‰ÑÑ"˜6Ò"Ü˜)¤TÔ*ÜÐ[Ð\eÐ[fÐfgÐhÓiÐiÜ˜.¬$Ô/ÜÐ`ÐaoÐ`pÐpqÐrÓsÐsÜ˜)¤TÔ*ÜÐ[Ð\eÐ[fÐfgÐhÓiÐiÜ˜-¬Ô.ÜÐ_Ð`mÐ_nÐnoÐpÓqÐqä)¨)°^ÀYÐP]Ó^ˆŒØ+FˆÔ(à�‰Ð(¬&°«+ÀeˆÔLØ�‰�¬¨s«ÀEˆÔJØ×+Ò+Ø�N‰N˜>¨2¸eˆNÕDð ,ó    ÚpredsÚtargetc                 óš   — t        ||| j                  | j                  | j                  | j                  «      \  | _        | _        | _        y)z*Update state with predictions and targets.N)r   r.   r   r   r   )r0   r4   r5   s      r2   ÚupdatezTranslationEditRate.updatee   sE   € äFQØØØ�N‰NØ× Ñ Ø×ÑØ×ÑóG
ÑCˆÔ˜dÔ0°$Õ2Cr3   c                 ó    — t        | j                  | j                  «      }| j                  �!|t	        j
                  | j                  «      fS |S )z)Calculate the translate error rate (TER).)r
   r   r   r   Útorchr'   )r0   Úters     r2   ÚcomputezTranslationEditRate.computep   sE   € ä˜4×/Ñ/°×1CÑ1CÓDˆØ×ÑÐ(ØœŸ	™	 $×"3Ñ"3Ó4Ð4Ð4Øˆ
r3   ÚvalÚaxc                 ó&   — | j                  ||«      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 TranslationEditRate
            >>> metric = TranslationEditRate()
            >>> 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 TranslationEditRate
            >>> metric = TranslationEditRate()
            >>> 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)r0   r<   r=   s      r2   ÚplotzTranslationEditRate.plotw   s   € ðT �z‰z˜#˜rÓ"Ð"r3   )FFTFF)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r,   Ú__annotations__r   r   r   Úfloatr   r   r   r   r   r   r*   r   Ústrr   r7   Útupler;   r   r   r@   Ú__classcell__)r1   s   @r2   r   r      s_  ø… ñðB $Ð�tÓ#Ø"Ð�dÓ"Ø#Ð�tÓ#Ø!Ð�eÓ!Ø!Ð�eÓ!àÓØÓØ+/€L�(˜4 ™<Ñ(Ó/ð  Ø$ØØ#Ø,1ñEàðEð ðEð ð	Eð
 ðEð &*ðEð ðEð 
õEð6	
˜E # x°¡}Ð"4Ñ5ð 	
¸xÈÈcÐS[Ð\_ÑS`ÐN`ÑHaÑ?bð 	
Ðgkó 	
ð˜˜v u¨V°V¨^Ñ'<Ð<Ñ=ó ð _cñ*#Ø˜E &¨(°6Ñ*:Ð":Ñ;Ñ<ð*#ØIQÐRZÑI[ð*#à	÷*#r3   r   )Úcollections.abcr   Útypingr   r   r   r   r9   r   r	   Ú torchmetrics.functional.text.terr
   r   r   Útorchmetrics.metricr   Útorchmetrics.utilities.importsr   Útorchmetrics.utilities.plotr   r   Ú__doctest_skip__r   r(   r3   r2   Ú<module>rQ      s?   ðõ %ß -Ó -ã ß  ç XÑ XÝ &Ý @ß @áØ2Ð3ÐôC#˜&õ C#r3   