a
    d                     @   s   d dl mZmZ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s~dgZG d	d
 d
eZdS )    )AnyListOptionalSequenceTuple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ed ee
e
e
e
edd fddZeeee f eeeee f  ddddZeeeeef f dddZdeeeee f  ee ed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?皙?r   jaN)languagereturn_sentence_level_scorealpharhodeletion	insertionkwargsreturnc           
         s   t  jf i | |dvr(td| || _|| _tg d||||gD ]4\}}	t|	trnt|	trJ|	dk rJtd| dqJ|| _|| _	|| _
|| _| 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)Zdist_reduce_fx)super__init__
ValueErrorr    r!   zip
isinstancefloatr"   r#   r$   r%   Z	add_state)
selfr    r!   r"   r#   r$   r%   r&   
param_nameparam	__class__ ^/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/torchmetrics/text/eed.pyr*   E   s    
zExtendedEditDistance.__init__)predstargetr'   c              	   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   )r/   r6   r7   r4   r4   r5   updateb   s    zExtendedEditDistance.update)r'   c                 C   s"   t | j}| jr|t| jfS |S )z'Calculate extended edit distance score.)r   r   r!   r	   )r/   Zaverager4   r4   r5   computes   s    
zExtendedEditDistance.compute)valaxr'   c                 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)

        )Z_plot)r/   r:   r;   r4   r4   r5   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   r8   r   r9   r   r   r   r<   __classcell__r4   r4   r2   r5   r      sB   
       	 r   N)typingr   r   r   r   r   r   Ztorchr   r	   Ztyping_extensionsr
   Z torchmetrics.functional.text.eedr   r   Ztorchmetrics.metricr   Ztorchmetrics.utilities.importsr   Ztorchmetrics.utilities.plotr   r   Z__doctest_skip__r   r4   r4   r4   r5   <module>   s    