o
    .wi                     @   s   d dl mZ d dlmZmZmZmZ d dlmZm	Z	 d dl
mZ d dlmZmZ d dlmZ d dlmZ d dlmZmZ esAd	gZG d
d deZdS )    )Sequence)AnyListOptionalUnion)Tensorstack)Literal)_eed_compute_eed_update)Metric)_MATPLOTLIB_AVAILABLE)_AX_TYPE_PLOT_OUT_TYPEExtendedEditDistance.plotc                       s  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 ed
< 						d%ded dede
de
de
de
dedd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	d&d!eeeee f  d"ee defd#d$Z  ZS )'ExtendedEditDistancea  Compute extended edit distance score (`ExtendedEditDistance`_) for strings or list of strings.

    The metric utilises the Levenshtein distance and extends it by adding a jump operation.

    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:

    - ``eed`` (:class:`~torch.Tensor`): A tensor with the extended edit distance score

    Args:
        language: Language used in sentences. Only supports English (en) and Japanese (ja) for now.
        return_sentence_level_score: An indication of whether sentence-level EED score is to be returned
        alpha: optimal jump penalty, penalty for jumps between characters
        rho: coverage cost, penalty for repetition of characters
        deletion: penalty for deletion of character
        insertion: penalty for insertion or substitution of character
        kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.

    Example:
        >>> from torchmetrics.text import ExtendedEditDistance
        >>> preds = ["this is the prediction", "here is an other sample"]
        >>> target = ["this is the reference", "here is another one"]
        >>> eed = ExtendedEditDistance()
        >>> eed(preds=preds, target=target)
        tensor(0.3078)

    Fhigher_is_betteris_differentiablefull_state_updateg        plot_lower_bound      ?plot_upper_boundsentence_eeden       @333333?皙?languager   jareturn_sentence_level_scorealpharhodeletion	insertionkwargsreturnNc           
         s   t  jd
i | |dvrtd| || _|| _tg d||||gD ]\}}	t|	tr7t|	tr?|	dk r?td| dq%|| _|| _	|| _
|| _| jdg dd	 d S )Nr   z?Expected argument `language` to either be `en` or `ja` but got )r!   r"   r#   r$   r   zParameter `z)` is expected to be a non-negative float.r   cat)dist_reduce_fx )super__init__
ValueErrorr   r    zip
isinstancefloatr!   r"   r#   r$   	add_state)
selfr   r    r!   r"   r#   r$   r%   
param_nameparam	__class__r)   R/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/text/eed.pyr+   F   s   
zExtendedEditDistance.__init__predstargetc              	   C   s(   t ||| j| j| j| j| j| j| _dS )z*Update state with predictions and targets.N)r   r   r!   r"   r#   r$   r   )r1   r7   r8   r)   r)   r6   updatec   s   
zExtendedEditDistance.updatec                 C   s"   t | j}| jr|t| jfS |S )z'Calculate extended edit distance score.)r
   r   r    r   )r1   averager)   r)   r6   computet   s   
zExtendedEditDistance.computevalaxc                 C   s   |  ||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 ExtendedEditDistance
            >>> metric = ExtendedEditDistance()
            >>> preds = ["this is the prediction", "there is an other sample"]
            >>> target = ["this is the reference", "there is another one"]
            >>> metric.update(preds, target)
            >>> fig_, ax_ = metric.plot()

        .. plot::
            :scale: 75

            >>> # Example plotting multiple values
            >>> from torchmetrics.text import ExtendedEditDistance
            >>> metric = ExtendedEditDistance()
            >>> preds = ["this is the prediction", "there is an other sample"]
            >>> target = ["this is the reference", "there is another one"]
            >>> values = [ ]
            >>> for _ in range(10):
            ...     values.append(metric(preds, target))
            >>> fig_, ax_ = metric.plot(values)

        )_plot)r1   r<   r=   r)   r)   r6   plot|   s   *r   )r   Fr   r   r   r   )NN)__name__
__module____qualname____doc__r   bool__annotations__r   r   r   r/   r   r   r   r	   r   r+   r   strr   r9   tupler;   r   r   r   r?   __classcell__r)   r)   r4   r6   r      s^   
  	
	r   N)collections.abcr   typingr   r   r   r   torchr   r   typing_extensionsr	    torchmetrics.functional.text.eedr
   r   torchmetrics.metricr   torchmetrics.utilities.importsr   torchmetrics.utilities.plotr   r   __doctest_skip__r   r)   r)   r)   r6   <module>   s   