
import datasets

from .evaluate import evaluate


lowercase_ 				=			"""\
@inproceedings{Rajpurkar2016SQuAD10,
  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},
  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},
  booktitle={EMNLP},
  year={2016}
}
"""

lowercase_ 				=			"""
This metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).

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.
"""

lowercase_ 				=			"""
Computes SQuAD scores (F1 and EM).
Args:
    predictions: List of question-answers dictionaries with the following key-values:
        - 'id': id of the question-answer pair as given in the references (see below)
        - 'prediction_text': the text of the answer
    references: List of question-answers dictionaries with the following key-values:
        - 'id': id of the question-answer pair (see above),
        - 'answers': a Dict in the SQuAD dataset format
            {
                'text': list of possible texts for the answer, as a list of strings
                'answer_start': list of start positions for the answer, as a list of ints
            }
            Note that answer_start values are not taken into account to compute the metric.
Returns:
    'exact_match': Exact match (the normalized answer exactly match the gold answer)
    'f1': The F-score of predicted tokens versus the gold answer
Examples:

    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]
    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]
    >>> squad_metric = datasets.load_metric(\"squad\")
    >>> results = squad_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {'exact_match': 100.0, 'f1': 100.0}
"""





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION ,						_KWARGS_DESCRIPTION       )
class     __UpperCamelCase (							datasets.Metric       ):






     """simple docstring"""







     def  UpperCAmelCase__							(				self							:							Union[str, Any] ):







       """simple docstring"""




       return datasets.MetricInfo(
           description=_DESCRIPTION     ,						citation=_CITATION     ,						inputs_description=_KWARGS_DESCRIPTION     ,						features=datasets.Features(
               {
                   '''predictions''': {'''id''': datasets.Value('''string''' ), '''prediction_text''': datasets.Value('''string''' )},
                   '''references''': {
                       '''id''': datasets.Value('''string''' ),
                       '''answers''': datasets.features.Sequence(
                           {
                               '''text''': datasets.Value('''string''' ),
                               '''answer_start''': datasets.Value('''int32''' ),
                           } ),
                   },
               } )     ,						codebase_urls=['''https://rajpurkar.github.io/SQuAD-explorer/''']     ,						reference_urls=['''https://rajpurkar.github.io/SQuAD-explorer/''']     ,						)




     def  UpperCAmelCase__							(				self							:							List[str]     ,						_A							:							Tuple     ,						_A							:							Optional[int] ):







       """simple docstring"""




       __SCREAMING_SNAKE_CASE      :	Tuple					=		{prediction['''id''']: prediction['''prediction_text'''] for prediction in predictions}
       __SCREAMING_SNAKE_CASE      :	int					=		[
           {
               '''paragraphs''': [
                   {
                       '''qas''': [
                           {
                               '''answers''': [{'''text''': answer_text} for answer_text in ref['''answers''']['''text''']],
                               '''id''': ref['''id'''],
                           }
                           for ref in references
                       ]
                   }
               ]
           }
       ]
       __SCREAMING_SNAKE_CASE      :	Optional[int]					=		evaluate(dataset=_A     ,						predictions=_A )
       return score






'''simple docstring'''






import json
from typing import List, Optional, Tuple

from tokenizers import pre_tokenizers, processors

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mvp import MvpTokenizer


A_			      =		logging.get_logger(__name__)


A_			      =		{"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}

# See all MVP models at https://huggingface.co/models?filter=mvp
A_			      =		{
    "vocab_file": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/vocab.json",
    },
    "added_tokens.json": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/added_tokens.json",
    },
    "merges_file": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/merges.txt",
    },
    "tokenizer_file": {
        "RUCAIBox/mvp": "https://huggingface.co/RUCAIBox/mvp/resolve/main/tokenizer.json",
    },
}

A_			      =		{
    "RUCAIBox/mvp": 1024,
}






class        lowercase_      (						lowerCAmelCase_							):
     A_											=			VOCAB_FILES_NAMES
     A_											=			PRETRAINED_VOCAB_FILES_MAP
     A_											=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
     A_											=			["input_ids", "attention_mask"]
     A_											=			MvpTokenizer





     def __init__(     self     :					Optional[int]     ,							__lowerCamelCase     :					Tuple=None     ,							__lowerCamelCase     :					Tuple=None     ,							__lowerCamelCase     :					List[str]=None     ,							__lowerCamelCase     :					Optional[int]="replace"     ,							__lowerCamelCase     :					int="<s>"     ,							__lowerCamelCase     :					Dict="</s>"     ,							__lowerCamelCase     :					int="</s>"     ,							__lowerCamelCase     :					int="<s>"     ,							__lowerCamelCase     :					int="<unk>"     ,							__lowerCamelCase     :					List[str]="<pad>"     ,							__lowerCamelCase     :					int="<mask>"     ,							__lowerCamelCase     :					int=False     ,							__lowerCamelCase     :					List[str]=True     ,							**__lowerCamelCase     :					Dict     ,							):
           super().__init__(
               __lowerCamelCase     ,							__lowerCamelCase     ,							tokenizer_file=__lowerCamelCase     ,							errors=__lowerCamelCase     ,							bos_token=__lowerCamelCase     ,							eos_token=__lowerCamelCase     ,							sep_token=__lowerCamelCase     ,							cls_token=__lowerCamelCase     ,							unk_token=__lowerCamelCase     ,							pad_token=__lowerCamelCase     ,							mask_token=__lowerCamelCase     ,							add_prefix_space=__lowerCamelCase     ,							trim_offsets=__lowerCamelCase     ,							**__lowerCamelCase     ,							)

           snake_case__       :   Any	      =    json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()			)
           if pre_tok_state.get('add_prefix_space'     ,							__lowerCamelCase			) != add_prefix_space:
                 snake_case__       :   str	      =    getattr(__lowerCamelCase     ,							pre_tok_state.pop('type'			)			)
                 snake_case__       :   int	      =    add_prefix_space
                 snake_case__       :   List[str]	      =    pre_tok_class(**__lowerCamelCase			)

           snake_case__       :   Tuple	      =    add_prefix_space

           # the pre_tokenizer is already updated in the GPT2TokenizerFast `__init__`
           snake_case__       :   Dict	      =    'post_processor'
           snake_case__       :   Optional[int]	      =    getattr(self.backend_tokenizer     ,							__lowerCamelCase     ,							__lowerCamelCase			)
           if tokenizer_component_instance:
                 snake_case__       :   List[Any]	      =    json.loads(tokenizer_component_instance.__getstate__()			)

                 # The lists 'sep' and 'cls' must be cased in tuples for the object `post_processor_class`
                 if "sep" in state:
                       snake_case__       :   int	      =    tuple(state['sep']			)
                 if "cls" in state:
                       snake_case__       :   Union[str, Any]	      =    tuple(state['cls']			)

                 snake_case__       :   Dict	      =    False

                 if state.get('add_prefix_space'     ,							__lowerCamelCase			) != add_prefix_space:
                       snake_case__       :   List[Any]	      =    add_prefix_space
                       snake_case__       :   List[str]	      =    True

                 if state.get('trim_offsets'     ,							__lowerCamelCase			) != trim_offsets:
                       snake_case__       :   str	      =    trim_offsets
                       snake_case__       :   List[Any]	      =    True

                 if changes_to_apply:
                       snake_case__       :   Optional[int]	      =    getattr(__lowerCamelCase     ,							state.pop('type'			)			)
                       snake_case__       :   str	      =    component_class(**__lowerCamelCase			)
                       setattr(self.backend_tokenizer     ,							__lowerCamelCase     ,							__lowerCamelCase			)





     @property
     def 			_lowerCAmelCase      (     self     :					str			):
           if self._mask_token is None:
                 if self.verbose:
                       logger.error('Using mask_token, but it is not set yet.'			)
                 return None
           return str(self._mask_token			)





     @mask_token.setter
     def 			_lowerCAmelCase      (     self     :					Tuple     ,							__lowerCamelCase     :					Optional[Any]			):
           snake_case__       :   str	      =    AddedToken(__lowerCamelCase     ,							lstrip=__lowerCamelCase     ,							rstrip=__lowerCamelCase			) if isinstance(__lowerCamelCase     ,							__lowerCamelCase			) else value
           snake_case__       :   Optional[int]	      =    value





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							*__lowerCamelCase     :					List[str]     ,							**__lowerCamelCase     :					List[Any]			):
           snake_case__       :   Optional[int]	      =    kwargs.get('is_split_into_words'     ,							__lowerCamelCase			)

           if is_split_into_words and not self.add_prefix_space:
                 raise ValueError(
                     F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
                     'to use it with pretokenized inputs.'			)

           return super()._batch_encode_plus(*__lowerCamelCase     ,							**__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							*__lowerCamelCase     :					Optional[Any]     ,							**__lowerCamelCase     :					Dict			):
           snake_case__       :   Tuple	      =    kwargs.get('is_split_into_words'     ,							__lowerCamelCase			)

           if is_split_into_words and not self.add_prefix_space:
                 raise ValueError(
                     F"You need to instantiate {self.__class__.__name__} with add_prefix_space=True "
                     'to use it with pretokenized inputs.'			)

           return super()._encode_plus(*__lowerCamelCase     ,							**__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					Union[str, Any]     ,							__lowerCamelCase     :					str     ,							__lowerCamelCase     :					Optional[str] = None			):
           snake_case__       :   List[str]	      =    self._tokenizer.model.save(__lowerCamelCase     ,							name=__lowerCamelCase			)
           return tuple(__lowerCamelCase			)





     def 			_lowerCAmelCase      (     self     :					List[str]     ,							__lowerCamelCase     :					Tuple     ,							__lowerCamelCase     :					Optional[int]=None			):
           snake_case__       :   Optional[Any]	      =    [self.bos_token_id] + token_ids_a + [self.eos_token_id]
           if token_ids_a is None:
                 return output

           return output + [self.eos_token_id] + token_ids_a + [self.eos_token_id]





     def 			_lowerCAmelCase      (     self     :					Dict     ,							__lowerCamelCase     :					List[int]     ,							__lowerCamelCase     :					Optional[List[int]] = None			):
           snake_case__       :   Dict	      =    [self.sep_token_id]
           snake_case__       :   List[Any]	      =    [self.cls_token_id]

           if token_ids_a is None:
                 return len(cls + token_ids_a + sep			) * [0]
           return len(cls + token_ids_a + sep + sep + token_ids_a + sep			) * [0]
