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t	|t	|7 }q#||fS )a  Update the mer 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   maxlen)r   r	   errorstotalpredtgtpred_tokens
tgt_tokens r   T/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/text/mer.py_mer_update   s   

r   r   r   c                 C   s   | | S )zCompute the match 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:
        Match error rate score
    r   )r   r   r   r   r   _mer_compute4   s   
r   c                 C   s   t | |\}}t||S )a  Match error rate is a metric of the performance of an automatic speech recognition system. This value
    indicates the percentage of words that were incorrectly predicted and inserted. The lower the value, the better
    the performance of the ASR system with a MatchErrorRate 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:
        Match error rate score

    Examples:
        >>> preds = ["this is the prediction", "there is an other sample"]
        >>> target = ["this is the reference", "there is another one"]
        >>> match_error_rate(preds=preds, target=target)
        tensor(0.4444)
    )r   r   )r   r	   r   r   r   r   r   match_error_rateA   s
   
r   )typingr   r   r   r   r   r   #torchmetrics.functional.text.helperr   r   r   r   r   r   r   r   r   <module>   s&   

