o
    *j                     @   s   d Z ddlmZ ddlZddlZddlZddlmZmZ ddl	m
Z
 ddlmZ dd Zd	d
 Zdd Zdd Zde
e fddZde
e fddZejeedZejeedZdS )zl
Official evaluation script for ReCoRD v1.0.
(Some functions are adopted from the SQuAD evaluation script.)
    )print_functionN)Counterdefaultdict)List)InputExamplec                 C   s4   dd }dd }dd }dd }||||| S )	zALower text and remove punctuation, articles and extra whitespace.c                 S   s   t dd| S )Nz\b(a|an|the)\b )resubtext r   t/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/modelscope/models/nlp/mglm/tasks/superglue/evaluate.pyremove_articles      z)normalize_answer.<locals>.remove_articlesc                 S   s   d |  S )Nr   )joinsplitr
   r   r   r   white_space_fix   r   z)normalize_answer.<locals>.white_space_fixc                    s"   t tj d fdd| D S )N c                 3   s    | ]	}| vr|V  qd S Nr   ).0chexcluder   r   	<genexpr>   s    z8normalize_answer.<locals>.remove_punc.<locals>.<genexpr>)setstringpunctuationr   r
   r   r   r   remove_punc   s   
z%normalize_answer.<locals>.remove_puncc                 S   s   |   S r   )lowerr
   r   r   r   r      s   znormalize_answer.<locals>.lowerr   )sr   r   r   r   r   r   r   normalize_answer   s
   r    c           	      C   sx   t |  }t | }t|t|@ }t| }|dkr dS d| t| }d| t| }d| | ||  }|S )Nr   g      ?   )r    r   r   sumvalueslen)	
predictionground_truthZprediction_tokensZground_truth_tokenscommonZnum_same	precisionZrecallf1r   r   r   f1_score$   s   r*   c                 C   s   t | t |kS r   )r    )r%   r&   r   r   r   exact_match_score1   s   r+   c                 C   s2   |sdS g }|D ]}| ||}| | qt|S )N        )appendmax)Z	metric_fnr%   ground_truthsZscores_for_ground_truthsr&   scorer   r   r   metric_max_over_ground_truths5   s   
r1   examplesc                 C   sl   t |t | ks
J d}t|| D ]\}}|jd }|jd | }|r+|t|||7 }qd| t |  }|S )Nr,   Zanswers
candidatesg      Y@)r$   zipmetar1   )predictionslabelsr2   metricr0   exampler%   r/   r   r   r   qa_evaluate?   s   
r:   c                 C   s   dd |D }t |}tt||}tt|| }tt}tt}|D ]\}	}
||	 |
 q#|D ]\}	}
||	 |
 q1d}|D ]}	||	 ||	 krO|d7 }qA|t| }|S )zLCompute the exact match (EM) for a sequence of predictions and actual labelsc                 S   s   g | ]}|j d  qS )Zquestion_idx)r5   )r   r9   r   r   r   
<listcomp>N   s    zmultirc_em.<locals>.<listcomp>r      )r   listr4   r   r-   r$   )r6   r7   r2   Zquestion_idsZunique_questionsZ	q_actualsZq_predictionsZactuals_per_questionZpredictions_per_questionZqidvalZemr   r   r   
multirc_emL   s"   r?   )r8   )__doc__
__future__r   	functoolsr   r   collectionsr   r   typingr   Ztasks.data_utilsr   r    r*   r+   r1   r:   r?   partialZqa_exact_matchZqa_f1r   r   r   r   <module>   s    
