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 wip 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 prediction
    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/wip.pyÚ_wip_update   s    

r   r   r   r   c                 C   s   | | | |  S )aN  Compute the Word Information Perserved.

    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 Perserved score
    r   )r   r   r   r   r   r   Ú_wip_compute7   s   r   c                 C   s   t | |ƒ\}}}t|||ƒS )ab  Word Information Preserved 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 preserved 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 preserved rate

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