o
    y“©i“  ã                	   @   s´   d dl mZmZmZ d dlmZmZ d dlmZ dee	ee	 f dee	ee	 f deeeef fdd„Z
d	ed
ededefdd„Zdee	ee	 f dee	ee	 f defdd„ZdS )é    )ÚListÚTupleÚUnion)ÚTensorÚtensor)Ú_edit_distanceÚpredsÚtargetÚreturnc           
      C   s®   t | tƒr| g} t |tƒr|g}tdƒ}tdƒ}tdƒ}tdƒ}t| |ƒD ]*\}}| ¡ }| ¡ }	|t||	ƒ7 }|t|	ƒ7 }|t|ƒ7 }|tt|	ƒt|ƒƒ7 }q%|| ||fS )a¾  Update the wil score with the current set of references and predictions.

    Args:
        preds: Transcription(s) to score as a string or list of strings
        target: Reference(s) for each speech input as a string or list of strings

    Returns:
        Number of edit operations to get from the reference to the prediction, summed over all samples
        Number of words overall references
        Number of words overall predictions
    g        )Ú
isinstanceÚstrr   ÚzipÚsplitr   ÚlenÚmax)
r   r	   ÚtotalÚerrorsÚtarget_totalÚpreds_totalÚpredÚtgtÚpred_tokensÚtarget_tokens© r   úT/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/text/wil.pyÚ_wil_update   s    

r   r   r   r   c                 C   s   d| | | |   S )aD  Compute the Word Information Lost.

    Args:
        errors: Number of edit operations to get from the reference to the prediction, summed over all samples
        target_total: Number of words overall references
        preds_total: Number of words overall prediction

    Returns:
        Word Information Lost score
    é   r   )r   r   r   r   r   r   Ú_wil_compute8   s   r   c                 C   s   t | |ƒ\}}}t|||ƒS )aI  Word Information Lost rate is a metric of the performance of an automatic speech recognition system. This
    value indicates the percentage of characters that were incorrectly predicted. The lower the value, the better
    the performance of the ASR system with a Word Information Lost rate of 0 being a perfect score.

    Args:
        preds: Transcription(s) to score as a string or list of strings
        target: Reference(s) for each speech input as a string or list of strings

    Returns:
        Word Information Lost rate

    Examples:
        >>> from torchmetrics.functional import word_information_lost
        >>> preds = ["this is the prediction", "there is an other sample"]
        >>> target = ["this is the reference", "there is another one"]
        >>> word_information_lost(preds, target)
        tensor(0.6528)
    )r   r   )r   r	   r   r   r   r   r   r   Úword_information_lostF   s   r   N)Útypingr   r   r   Útorchr   r   Ú#torchmetrics.functional.text.helperr   r   r   r   r   r   r   r   r   Ú<module>   s$   ÿþ
ý"ÿþý