o
    yi                     @   s   d dl mZmZmZ d dl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f fdd	Z
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UnionN)Tensortensor)_edit_distancepredstargetreturnc                 C   s   t | tr| g} t |tr|g}tdtjd}tdtjd}t| |D ]\}}| }| }|t||7 }|t|7 }q#||fS )a  Update the wer 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
    r   )dtype)	
isinstancestrr   torchfloatzipsplitr   len)r   r	   errorstotalpredtgtpred_tokens
tgt_tokens r   T/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/text/wer.py_wer_update   s   

r   r   r   c                 C   s   | | S )zCompute the word error rate.

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

    Returns:
        Word error rate score
    r   )r   r   r   r   r   _wer_compute3   s   
r   c                 C   s   t | |\}}t||S )a  Word error rate (WordErrorRate_) is a common metric of the performance of an automatic speech recognition
    system. This value indicates the percentage of words that were incorrectly predicted. The lower the value, the
    better the performance of the ASR system with a WER 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 error rate score

    Examples:
        >>> preds = ["this is the prediction", "there is an other sample"]
        >>> target = ["this is the reference", "there is another one"]
        >>> word_error_rate(preds=preds, target=target)
        tensor(0.5000)
    )r   r   )r   r	   r   r   r   r   r   word_error_rate@   s   
r   )typingr   r   r   r   r   r   #torchmetrics.functional.text.helperr   r   r   r   r   r   r   r   r   <module>   s   

2