o
    7ti`!                     @   sp   d Z ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
mZ dZd	d
 Zdd ZG dd deZdS )u  
Know What You Don’t Know: Unanswerable Questions for SQuAD
https://arxiv.org/pdf/1806.03822.pdf

Stanford Question Answering Dataset (SQuAD) is a reading comprehension dataset,
consisting of questions posed by crowdworkers on a set of Wikipedia articles,
where the answer to every question is a segment of text, or span, from the
corresponding reading passage, or the question might be unanswerable.
SQuAD2.0 combines the 100,000 questions in SQuAD1.1 with over 50,000 unanswerable
questions written adversarially by crowdworkers to look similar to answerable ones.
To do well on SQuAD2.0, systems must not only answer questions when possible, but
also determine when no answer is supported by the paragraph and abstain from answering.

Homepage: https://rajpurkar.github.io/SQuAD-explorer/
    )partialexpN)versionInstance)ConfigurableTaskz
@misc{rajpurkar2018know,
    title={Know What You Don't Know: Unanswerable Questions for SQuAD},
    author={Pranav Rajpurkar and Robin Jia and Percy Liang},
    year={2018},
    eprint={1806.03822},
    archivePrefix={arXiv},
    primaryClass={cs.CL}
}
c                 C   s    dd l }|d}|j| |dS )Nr   squad_v2predictions
references)evaluateloadcompute)r   r   r   squad_metric r   N/home/ubuntu/.local/lib/python3.10/site-packages/lm_eval/tasks/squadv2/task.py_squad_metric'   s   
r   c                 C   s    t | \}}t||d| dS )Nr
   r   )zipr   get)keyitemsr   r   r   r   r   
_squad_agg.   s   r   c                       s   e Zd ZdZdZdZd# fdd	Zee	j
edks J ddd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z	d$ddZdd Zdd  Zd!d" Z  ZS )%SQuAD2   zlighteval/squad_v2Nc                    s   t  jdd| jiid d S )Nmetadatar   )config)super__init__VERSION)selfr   	__class__r   r   r   9   s   zSQuAD2.__init__z1.11.0z,datasets v1.11.0 or later required for SQuADc                 C      dS NTr   r    r   r   r   has_training_docsA      zSQuAD2.has_training_docsc                 C   r#   r$   r   r%   r   r   r   has_validation_docsD   r'   zSQuAD2.has_validation_docsc                 C   r#   NFr   r%   r   r   r   has_test_docsG   r'   zSQuAD2.has_test_docsc                 C   
   | j d S )Ntraindatasetr%   r   r   r   training_docsJ      
zSQuAD2.training_docsc                 C   r+   )N
validationr-   r%   r   r   r   validation_docsM   r0   zSQuAD2.validation_docsc                 C   s4   d|d  d d |d  d d |d  d d S )	NzTitle: titlez

zBackground: contextz
Question: questionzAnswer:r   r    docr   r   r   doc_to_textP   s(   	zSQuAD2.doc_to_textc                 C   r#   r$   r   r%   r   r   r   should_decontaminate^   r'   zSQuAD2.should_decontaminatec                 C   s   |d S )Nr4   r   r6   r   r   r   doc_to_decontamination_querya   s   z#SQuAD2.doc_to_decontamination_queryc                 C   s4   |d d }t |dkr|d }d| S d}d| S )Nanswerstextr   unanswerable )len)r    r7   answer_listanswerr   r   r   doc_to_targetd   s   zSQuAD2.doc_to_targetFc                 K   s>   t d	d||ddgifdd|t d	d||dfdd|gS )
a  Uses RequestFactory to construct Requests and returns an iterable of
        Requests which will be sent to the LM.

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param ctx: str
            The context string, generated by fewshot_context. This includes the natural
            language description, as well as the few shot examples, and the question
            part of the document for `doc`.
        generate_untiluntil
r   )request_typer7   	argumentsidxloglikelihoodz unanswerableNr   r   )r    r7   ctxchat_templateapply_chat_templatekwargsr   r   r   construct_requestsl   s"   zSQuAD2.construct_requestsc           	   	   C   sl   |\}\}}t |}|d ||d}|d |d d}||f||f||f||f||f||f||f||fdS )a  Take a single document and the LM results and evaluates, returning a
        dict where keys are the names of submetrics and values are the values of
        the metric for that one document

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param results:
            The results of the requests created in construct_requests.
        id)rO   prediction_textno_answer_probabilityr;   )rO   r;   exactf1HasAns_exact	HasAns_f1NoAns_exactNoAns_f1
best_exactbest_f1r   )	r    r7   resultscontinuationlogprob_unanswerable_rQ   r   r   r   r   r   process_results   s@   zSQuAD2.process_resultsc              
   C   sF   t tdt tdt tdt tdt tdt tdt tdt tdd	S )
z
        :returns: {str: [float] -> float}
            A dictionary where keys are the names of submetrics and values are
            functions that aggregate a list of metrics
        rS   rT   rU   rV   rW   rX   rY   rZ   rR   )r   r   r%   r   r   r   aggregation   s2   zSQuAD2.aggregationc              	   C   s   dddddddddS )z
        :returns: {str: bool}
            A dictionary where keys are the names of submetrics and values are
            whether a higher value of the submetric is better
        TrR   r   r%   r   r   r   higher_is_better   s   zSQuAD2.higher_is_better)Nr)   )__name__
__module____qualname__r   DATASET_PATHDATASET_NAMEr   r   parsedatasets__version__r&   r(   r*   r/   r2   r8   r9   r:   rB   rN   r_   r`   ra   __classcell__r   r   r!   r   r   4   s,    	
:!r   )__doc__	functoolsr   mathr   rh   	packagingr   lm_eval.api.instancer   lm_eval.api.taskr   	_CITATIONr   r   r   r   r   r   r   <module>   s    