o
    7ti                  
   @   s   d dl Z d dlZzd dlZedZedZedZejddddZW n" e	e
fy2   e	d	 eyG Z z
ed
ee ddZ[ww dd ZdefddZdefddZde jfddZdd ZdS )    Nbleurouge	bertscorebleurtzbleurt-base-512metric)module_typezPlease install evaluation metrics via pip install evaluate bert-score rouge_score>=0.1.2 nltk absl-py git+https://github.com/google-research/bleurt.gitz"Error loading evaluation metrics: z!. Please check your installation.c              
   C   s  z	t j| |d}W n ty' } ztd|  dtji}W Y d }~nd }~ww z	tj| |d}W n" tyS } ztd|  tjtjtjd}W Y d }~nd }~ww ztj| |dd }W n ty| } ztd|  tjg}W Y d }~nd }~ww ztj| |dd	d
 }W n ty } ztd|  tjg}W Y d }~nd }~ww |d dkr|d  d7  < |d |d |d |d t	|t	|d}|S )N)predictions
referenceszBleu error: r   zRouge error: )rouge1rouge2rougeLscoreszBleurt error: en)r   r	   langf1zBert error: r   gh㈵>r
   r   r   r   r
   r   r   r   
bert_score)
r   compute	ExceptionprintnpNANr   r   r   mean)predrefsbleu_resultserouge_resultsbleurt_scoresbert_scoresresults r!   M/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/olaph/utils.pydoc_eval   sP   	r#   returnc                 C      | d S )NQuestionr!   docr!   r!   r"   doc_to_textD      r)   c                 C   r%   )NFree_form_answerr!   r'   r!   r!   r"   doc_to_targetH   r*   r,   datasetc                 C   s4   dd }t | }td| }| t|}||S )Nc                 S   s   | S )Nr!   r'   r!   r!   r"   _helperM   s   zprocess_docs.<locals>._helperg?)lenintselectrangemap)r-   r.   num_entriesone_percent_indexfiltered_datasetr!   r!   r"   process_docsL   s
   
r7   c                 C   s   |d gt | g}}t|d dk st|d dk r*tjtjtjtjtjtjdS t||}|d |d |d |d |d |d	 dS )
Nr   
   r   r   r
   r   r   r   r   )r,   r/   r   r   r#   )r(   r    r   r   r!   r!   r"   process_resultsY   s"    
	r9   )datasetsnumpyr   evaluateloadr   r   r   r   ModuleNotFoundErrorImportErrorr   r   RuntimeErrorstrr#   r)   r,   Datasetr7   r9   r!   r!   r!   r"   <module>   s.    


+