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t|t|7 }|t|7 }q#||fS )a  Update the cer 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 character overall references
    r   )dtype)	
isinstancestrr   torchfloatzipr   listlen)r   r	   errorstotalpredtgtpred_tokens
tgt_tokens r   T/home/ubuntu/.local/lib/python3.10/site-packages/torchmetrics/functional/text/cer.py_cer_update   s   

r   r   r   c                 C   s   | | S )a	  Compute the Character error rate.

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

    Returns:
        Character error rate score
    r   )r   r   r   r   r   _cer_compute3   s   
r   c                 C   s   t | |\}}t||S )a  character error rate is a common 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 CER 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:
        Character error rate score

    Examples:
        >>> preds = ["this is the prediction", "there is an other sample"]
        >>> target = ["this is the reference", "there is another one"]
        >>> char_error_rate(preds=preds, target=target)
        tensor(0.3415)
    )r   r   )r   r	   r   r   r   r   r   char_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