o
    .wi                     @   s   d dl mZ d dlmZmZmZ d dlZd dlmZ d dlm	Z
 	ddeeee f deeee f d	ed
efddZ	ddedeeef deed  d
efddZ		ddeeee f deeee f d	edeed  d
ef
ddZdS )    )Sequence)LiteralOptionalUnionN)Tensor)_LevenshteinEditDistance   predstargetsubstitution_costreturnc                    s   t | tr| g} t |tr|g}tdd | D s td|  tdd |D s0td| t| t|krFtdt|  dt|  fdd	t| |D }tj|tjd
S )Nc                 s       | ]}t |tV  qd S N
isinstancestr.0x r   ^/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torchmetrics/functional/text/edit.py	<genexpr>        z(_edit_distance_update.<locals>.<genexpr>zCExpected all values in argument `preds` to be string type, but got c                 s   r   r   r   r   r   r   r   r   "   r   zDExpected all values in argument `target` to be string type, but got zDExpected argument `preds` and `target` to have same length, but got z and c                    s$   g | ]\}}t | d |d qS ))op_substituter   )_LE_distance)r   ptr   r   r   
<listcomp>)   s    z)_edit_distance_update.<locals>.<listcomp>dtype)	r   r   all
ValueErrorlenziptorchtensorint)r	   r
   r   distancer   r   r   _edit_distance_update   s    


r)   meanedit_scoresnum_elements	reduction)r*   sumnonec                 C   s\   |   dkrtjdtjdS |dkr|  | S |dkr |  S |du s(|dkr*| S td)z3Compute final edit distance reduced over the batch.r   r   r*   r.   Nr/   zHExpected argument `reduction` to either be 'sum', 'mean', 'none' or None)numelr%   r&   int32r.   r"   )r+   r,   r-   r   r   r   _edit_distance_compute0   s   r2   c                 C   s   t | ||}t|| |dS )u  Calculates the Levenshtein edit distance between two sequences.

    The edit distance is the number of characters that need to be substituted, inserted, or deleted, to transform the
    predicted text into the reference text. The lower the distance, the more accurate the model is considered to be.

    Implementation is similar to `nltk.edit_distance <https://www.nltk.org/_modules/nltk/metrics/distance.html>`_.

    Args:
        preds: An iterable of predicted texts (strings).
        target: An iterable of reference texts (strings).
        substitution_cost: The cost of substituting one character for another.
        reduction: a method to reduce metric score over samples.

            - ``'mean'``: takes the mean over samples
            - ``'sum'``: takes the sum over samples
            - ``None`` or ``'none'``: return the score per sample

    Raises:
        ValueError:
            If ``preds`` and ``target`` do not have the same length.
        ValueError:
            If ``preds`` or ``target`` contain non-string values.

    Example::
        Basic example with two strings. Going from “rain” -> “sain” -> “shin” -> “shine” takes 3 edits:

        >>> from torchmetrics.functional.text import edit_distance
        >>> edit_distance(["rain"], ["shine"])
        tensor(3.)

    Example::
        Basic example with two strings and substitution cost of 2. Going from “rain” -> “sain” -> “shin” -> “shine”
        takes 3 edits, where two of them are substitutions:

        >>> from torchmetrics.functional.text import edit_distance
        >>> edit_distance(["rain"], ["shine"], substitution_cost=2)
        tensor(5.)

    Example::
        Multiple strings example:

        >>> from torchmetrics.functional.text import edit_distance
        >>> edit_distance(["rain", "lnaguaeg"], ["shine", "language"], reduction=None)
        tensor([3, 4], dtype=torch.int32)
        >>> edit_distance(["rain", "lnaguaeg"], ["shine", "language"], reduction="mean")
        tensor(3.5000)

    )r,   r-   )r)   r2   r0   )r	   r
   r   r-   r(   r   r   r   edit_distanceA   s   6r3   )r   )r*   )r   r*   )collections.abcr   typingr   r   r   r%   r   #torchmetrics.functional.text.helperr   r   r   r'   r)   r2   r3   r   r   r   r   <module>   sL   




