o
    yi                     @   sl   d dl mZmZ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 G dd deZdS )    )AnyListOptionalSequenceTupleUnionN)Tensortensor)_ter_compute_ter_update_TercomTokenizer)Metricc                       s   e Zd ZU dZ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f fddZdeeee f deeeee f  ddfddZdee	ee	e	f f fddZ  ZS )TranslationEditRatea5  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-insesitivity.
        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:
        >>> 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total_num_editstotal_tgt_lenNsentence_terT	normalizeno_punctuation	lowercaseasian_supportreturn_sentence_level_scorekwargsc                    s   t  jdi | t|tstd| dt|ts#td| dt|ts0td| dt|ts=td| dt||||| _|| _| jdt	ddd	 | jd
t	ddd	 | jri| jdg dd	 d S d S )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   g        sum)dist_reduce_fxr   r   cat )
super__init__
isinstancebool
ValueErrorr   	tokenizerr   	add_stater	   )selfr   r   r   r   r   r   	__class__r   I/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/text/ter.pyr!   @   s    	



zTranslationEditRate.__init__predstargetreturnc                 C   s*   t ||| j| j| j| j\| _| _| _dS )z*Update state with predictions and targets.N)r   r%   r   r   r   )r'   r+   r,   r   r   r*   update[   s   zTranslationEditRate.updatec                 C   s,   t | j| j}| jdur|t| jfS |S )z)Calculate the translate error rate (TER).N)r
   r   r   r   torchr   )r'   terr   r   r*   computef   s   
zTranslationEditRate.compute)FFTFF)__name__
__module____qualname____doc__r   r#   __annotations__r   r   r   r   r   r   r   r!   r   strr   r.   r   r1   __classcell__r   r   r(   r*   r      s8   
 2&r   )typingr   r   r   r   r   r   r/   r   r	    torchmetrics.functional.text.terr
   r   r   torchmetrics.metricr   r   r   r   r   r*   <module>   s    