o
    6ti                     @   sP   d dl mZ d dlm  m  mZ dd Zdd Zdd Zd	d
 Z	dd Z
dS )    zip_longestNc                 C   sl   | d d }t | d d | d d d d D ]\}}d| d}|d ur+d| dnd	}||| 7 }q|S )
Nstoryz

	questions
input_textanswerszQ: zA: zA:r   )docdoc_textqaquestionanswer r   L/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/coqa/utils.pydoc_to_text   s   r   c                 C   s   t | d d }g }| d d |d  }|| | d}|r>|D ]}|| d |d  }| ttj|vr=|| q"|S )Nr   r   r      additional_answers)lenappendgetlowermapstr)r	   turn_idr   answer_forturnr   keyadditional_answer_for_turnr   r   r   doc_to_target   s   



r   c                    s   d}t | dkr-tt | D ]}| d| | |d d   }|t fdd|D 7 }qn|t fdd| D 7 }|tdt |  S )N        r   r   c                 3       | ]	}t | V  qd S Nsquad_metricscompute_exact.0r   predr   r   	<genexpr>,       zem.<locals>.<genexpr>c                 3   r    r!   r"   r%   r'   r   r   r)   .   r*   r   rangemax)	gold_listr(   em_sumigold_answersr   r'   r   em%   s   r2   c                    s   d}d}t | dkr<tt | D ]*}| d| | |d d   }|t fdd|D 7 }|t fdd|D 7 }qn|t fdd| D 7 }|t fdd| D 7 }|tdt |  |tdt |  d	S )
Nr   r   r   c                 3   r    r!   r"   r%   r'   r   r   r)   <   r*   z!compute_scores.<locals>.<genexpr>c                 3   r    r!   r#   
compute_f1r%   r'   r   r   r)   =   r*   c                 3   r    r!   r"   r%   r'   r   r   r)   ?   r*   c                 3   r    r!   r3   r%   r'   r   r   r)   @   r*   )r2   f1r+   )r.   r(   f1_sumr/   r0   r1   r   r'   r   compute_scores3   s   r7   c                 C   s,   t | }|d  dd }t||}|S )Nr   
)r   stripsplitr7   )r	   resultsr.   r(   scoresr   r   r   process_resultsH   s   
r=   )	itertoolsr   'transformers.data.metrics.squad_metricsdatametricsr#   r   r   r2   r7   r=   r   r   r   r   <module>   s    