




'''simple docstring'''





import argparse
import json
import re
import string
import sys
from collections import Counter


def 				UpperCamelCase	(  snake_case   ):




   '''simple docstring'''

   def remove_articles(snake_case   ):
      return re.sub(R'''\b(a|an|the)\b'''	,     ''' '''	,     snake_case   )

   def white_space_fix(snake_case   ):
      return " ".join(text.split()   )

   def remove_punc(snake_case   ):
      SCREAMING_SNAKE_CASE__			:				Optional[int]       						= set(string.punctuation   )
      return "".join(ch for ch in text if ch not in exclude   )

   def lower(snake_case   ):
      return text.lower()

   return white_space_fix(remove_articles(remove_punc(lower(snake_case   )   )   )   )


def 				UpperCamelCase	(  snake_case	,     snake_case   ):




   '''simple docstring'''

   SCREAMING_SNAKE_CASE__			:				Tuple       						= normalize_answer(snake_case   ).split()
   SCREAMING_SNAKE_CASE__			:				List[Any]       						= normalize_answer(snake_case   ).split()
   SCREAMING_SNAKE_CASE__			:				Dict       						= Counter(snake_case   ) & Counter(snake_case   )
   SCREAMING_SNAKE_CASE__			:				int       						= sum(common.values()   )
   if num_same == 0:
      return 0
   SCREAMING_SNAKE_CASE__			:				Tuple       						= 1.0 * num_same / len(snake_case   )
   SCREAMING_SNAKE_CASE__			:				Tuple       						= 1.0 * num_same / len(snake_case   )
   SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= (2 * precision * recall) / (precision + recall)
   return fa


def 				UpperCamelCase	(  snake_case	,     snake_case   ):




   '''simple docstring'''

   return normalize_answer(snake_case   ) == normalize_answer(snake_case   )


def 				UpperCamelCase	(  snake_case	,     snake_case	,     snake_case   ):




   '''simple docstring'''

   SCREAMING_SNAKE_CASE__			:				Any       						= []
   for ground_truth in ground_truths:
      SCREAMING_SNAKE_CASE__			:				List[Any]       						= metric_fn(snake_case	,     snake_case   )
      scores_for_ground_truths.append(snake_case   )
   return max(snake_case   )


def 				UpperCamelCase	(  snake_case	,     snake_case   ):




   '''simple docstring'''

   SCREAMING_SNAKE_CASE__			:				str       						= 0
   SCREAMING_SNAKE_CASE__			:				Optional[int]       						= []
   for passage in dataset:
      for qa in passage["qas"]:
         total += 1
         if qa["id"] not in predictions:
            SCREAMING_SNAKE_CASE__			:				Any       						= f"""Unanswered question {qa["id"]} will receive score 0."""
            print(snake_case	,     file=sys.stderr   )
            continue

         SCREAMING_SNAKE_CASE__			:				Union[str, Any]       						= [x['''text'''] for x in qa['''answers''']]
         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= predictions[qa['''id''']]

         SCREAMING_SNAKE_CASE__			:				List[str]       						= metric_max_over_ground_truths(snake_case	,     snake_case	,     snake_case   )
         if int(_exact_match   ) == 1:
            correct_ids.append(qa['''id''']   )
         exact_match += _exact_match

         fa += metric_max_over_ground_truths(snake_case	,     snake_case	,     snake_case   )

   SCREAMING_SNAKE_CASE__			:				int       						= exact_match / total
   SCREAMING_SNAKE_CASE__			:				Optional[int]       						= fa / total

   return {"exact_match": exact_match, "f1": fa}, correct_ids


if __name__ == "__main__":
       lowercase__			=	'''1.0'''
       lowercase__			=	argparse.ArgumentParser('''Official evaluation script for ReCoRD v1.0.''')
       parser.add_argument('''data_file''', help='''The dataset file in JSON format.''')
       parser.add_argument('''pred_file''', help='''The model prediction file in JSON format.''')
       parser.add_argument('''--output_correct_ids''', action='''store_true''', help='''Output the correctly answered query IDs.''')
       lowercase__			=	parser.parse_args()

       with open(args.data_file) as data_file:
              lowercase__			=	json.load(data_file)
              if dataset_json["version"] != expected_version:
                     print(
                         F"Evaluation expects v-{expected_version}, but got dataset with v-{dataset_json['version']}",
                         file=sys.stderr,
                     )
              lowercase__			=	dataset_json['''data''']

       with open(args.pred_file) as pred_file:
              lowercase__			=	json.load(pred_file)

       lowercase__	,     lowercase__			=	evaluate(dataset, predictions)

       if args.output_correct_ids:
              print(F"Output {len(correct_ids)} correctly answered question IDs.")
              with open('''correct_ids.json''', '''w''') as f:
                     json.dump(correct_ids, f)







'''simple docstring'''





import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_splinter import SplinterTokenizer


lowercase__			=	logging.get_logger(__name__)

lowercase__			=	{'''vocab_file''': '''vocab.txt'''}

lowercase__			=	{
    '''vocab_file''': {
        '''tau/splinter-base''': '''https://huggingface.co/tau/splinter-base/resolve/main/vocab.txt''',
        '''tau/splinter-base-qass''': '''https://huggingface.co/tau/splinter-base-qass/resolve/main/vocab.txt''',
        '''tau/splinter-large''': '''https://huggingface.co/tau/splinter-large/resolve/main/vocab.txt''',
        '''tau/splinter-large-qass''': '''https://huggingface.co/tau/splinter-large-qass/resolve/main/vocab.txt''',
    }
}

lowercase__			=	{
    '''tau/splinter-base''': 5_1_2,
    '''tau/splinter-base-qass''': 5_1_2,
    '''tau/splinter-large''': 5_1_2,
    '''tau/splinter-large-qass''': 5_1_2,
}

lowercase__			=	{
    '''tau/splinter-base''': {'''do_lower_case''': False},
    '''tau/splinter-base-qass''': {'''do_lower_case''': False},
    '''tau/splinter-large''': {'''do_lower_case''': False},
    '''tau/splinter-large-qass''': {'''do_lower_case''': False},
}






class 						lowercase     (   UpperCAmelCase_):
      __lowercase 		=  VOCAB_FILES_NAMES
      __lowercase 		=  PRETRAINED_VOCAB_FILES_MAP
      __lowercase 		=  PRETRAINED_INIT_CONFIGURATION
      __lowercase 		=  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __lowercase 		=  SplinterTokenizer






      def __init__(self ,  a__=None ,  a__=None ,  a__=True ,  a__="[UNK]" ,  a__="[SEP]" ,  a__="[PAD]" ,  a__="[CLS]" ,  a__="[MASK]" ,  a__="[QUESTION]" ,  a__=True ,  a__=None ,  **a__ ,  )       ->   Any:
         super().__init__(
             a__ ,  tokenizer_file=a__ ,  do_lower_case=a__ ,  unk_token=a__ ,  sep_token=a__ ,  pad_token=a__ ,  cls_token=a__ ,  mask_token=a__ ,  tokenize_chinese_chars=a__ ,  strip_accents=a__ ,  additional_special_tokens=(question_token,) ,  **a__ ,  )

         SCREAMING_SNAKE_CASE__			:				Tuple       						= json.loads(self.backend_tokenizer.normalizer.__getstate__()					)
         if (
             pre_tok_state.get('''lowercase''' ,  a__					) != do_lower_case
             or pre_tok_state.get('''strip_accents''' ,  a__					) != strip_accents
         ):
            SCREAMING_SNAKE_CASE__			:				List[Any]       						= getattr(a__ ,  pre_tok_state.pop('''type'''					)					)
            SCREAMING_SNAKE_CASE__			:				Optional[int]       						= do_lower_case
            SCREAMING_SNAKE_CASE__			:				Optional[int]       						= strip_accents
            SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= pre_tok_class(**a__					)

         SCREAMING_SNAKE_CASE__			:				Optional[int]       						= do_lower_case






      @property
      def    A__(self					)       ->   List[str]:
         return self.convert_tokens_to_ids(self.question_token					)






      def    A__(self ,  a__ ,  a__ = None					)       ->   List[int]:
         if token_ids_a is None:
            return [self.cls_token_id] + token_ids_a + [self.sep_token_id]

         SCREAMING_SNAKE_CASE__			:				Dict       						= [self.cls_token_id]
         SCREAMING_SNAKE_CASE__			:				Tuple       						= [self.sep_token_id]
         SCREAMING_SNAKE_CASE__			:				List[Any]       						= [self.question_token_id] + [self.convert_tokens_to_ids('''.'''					)]
         if self.padding_side == "right":
            # Input is question-then-context
            return cls + token_ids_a + question_suffix + sep + token_ids_a + sep
         else:
            # Input is context-then-question
            return cls + token_ids_a + sep + token_ids_a + question_suffix + sep






      def    A__(self ,  a__ ,  a__ = None					)       ->   List[int]:
         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= [self.sep_token_id]
         SCREAMING_SNAKE_CASE__			:				Any       						= [self.cls_token_id]
         SCREAMING_SNAKE_CASE__			:				Optional[Any]       						= [self.question_token_id] + [self.convert_tokens_to_ids('''.'''					)]
         if token_ids_a is None:
            return len(cls + token_ids_a + sep					) * [0]

         if self.padding_side == "right":
            # Input is question-then-context
            return len(cls + token_ids_a + question_suffix + sep					) * [0] + len(token_ids_a + sep					) * [1]
         else:
            # Input is context-then-question
            return len(cls + token_ids_a + sep					) * [0] + len(token_ids_a + question_suffix + sep					) * [1]






      def    A__(self ,  a__ ,  a__ = None					)       ->   Tuple[str]:
         SCREAMING_SNAKE_CASE__			:				int       						= self._tokenizer.model.save(a__ ,  name=a__					)
         return tuple(a__					)

