




"""simple docstring"""





from typing import Dict, List, Optional, Tuple

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


_lowercase		 =      logging.get_logger(__name__)

class  __lowercase    (     snake_case_		):


				'''simple docstring'''


				_lowerCAmelCase		     =["input_ids", "attention_mask"]

				def __init__(				self				:       str ,SCREAMING_SNAKE_CASE_				:       Optional[int]="[PAD]" ,SCREAMING_SNAKE_CASE_				:       Optional[int]="[BOS]" ,SCREAMING_SNAKE_CASE_				:       int="[EOS]" ,SCREAMING_SNAKE_CASE_				:       Dict="[MASK]" ,SCREAMING_SNAKE_CASE_				:       Any="[CLS]" ,SCREAMING_SNAKE_CASE_				:       Optional[int]="[SEP]" ,SCREAMING_SNAKE_CASE_				:       Tuple=2_0_4_8 ,**SCREAMING_SNAKE_CASE_				:       int ,):
										snake_case__    :   Tuple							     =   AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else pad_token
										snake_case__    :   Optional[int]							     =   AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else bos_token
										snake_case__    :   int							     =   AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else eos_token
										snake_case__    :   int							     =   AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else mask_token
										snake_case__    :   int							     =   AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else cls_token
										snake_case__    :   Optional[int]							     =   AddedToken(SCREAMING_SNAKE_CASE_ ,lstrip=SCREAMING_SNAKE_CASE_ ,rstrip=SCREAMING_SNAKE_CASE_ ) if isinstance(SCREAMING_SNAKE_CASE_ ,SCREAMING_SNAKE_CASE_ ) else sep_token

										super().__init__(
										    pad_token=SCREAMING_SNAKE_CASE_ ,bos_token=SCREAMING_SNAKE_CASE_ ,eos_token=SCREAMING_SNAKE_CASE_ ,mask_token=SCREAMING_SNAKE_CASE_ ,cls_token=SCREAMING_SNAKE_CASE_ ,sep_token=SCREAMING_SNAKE_CASE_ ,model_max_length=SCREAMING_SNAKE_CASE_ ,**SCREAMING_SNAKE_CASE_ ,)

										snake_case__    :   int							     =   2**8  # utf is 8 bits

										# define special tokens dict
										snake_case__    :   Dict[str, int]							     =   {
										    self.pad_token: 0,
										    self.bos_token: 1,
										    self.eos_token: 2,
										    self.mask_token: 3,
										    self.cls_token: 4,
										    self.sep_token: 5,
										}
										snake_case__    :   Dict							     =   len(self.special_tokens_encoder )
										snake_case__    :   Dict[int, str]							     =   {v: k for k, v in self.special_tokens_encoder.items()}

				def  _lowerCamelCase   (				self				:       Dict ):
										snake_case__    :   List[str]							     =   self.special_tokens_encoder.copy()
										vocab.update(self.added_tokens_encoder )
										for i in range(self._utf_vocab_size ):
																snake_case__    :   Union[str, Any]							     =   chr(SCREAMING_SNAKE_CASE_ )
																snake_case__    :   Optional[int]							     =   i + len(self.special_tokens_encoder )
										return vocab

				@property
				def  _lowerCamelCase   (				self				:       Tuple ):
										return self._utf_vocab_size + self._num_special_tokens

				def  _lowerCamelCase   (				self				:       int ,SCREAMING_SNAKE_CASE_				:       List[int] ,SCREAMING_SNAKE_CASE_				:       Optional[List[int]] = None ,SCREAMING_SNAKE_CASE_				:       bool = False ):
										if already_has_special_tokens:
																return super().get_special_tokens_mask(
																    token_ids_a=SCREAMING_SNAKE_CASE_ ,token_ids_a=SCREAMING_SNAKE_CASE_ ,already_has_special_tokens=SCREAMING_SNAKE_CASE_ )

										# normal case: some special tokens
										if token_ids_a is None:
																return [1] + [0] * len(SCREAMING_SNAKE_CASE_ ) + [1]
										return [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1] + ([0] * len(SCREAMING_SNAKE_CASE_ )) + [1]

				def  _lowerCamelCase   (				self				:       Dict ,SCREAMING_SNAKE_CASE_				:       List[int] ,SCREAMING_SNAKE_CASE_				:       Optional[List[int]] = None ):
										if token_ids_a is None:
																return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
										else:
																return [self.cls_token_id] + token_ids_a + [self.sep_token_id] + token_ids_a + [self.sep_token_id]

				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       str ):
										snake_case__    :   List[str]							     =   [chr(SCREAMING_SNAKE_CASE_ ) for i in text.encode('''utf-8''' )]
										return tokens

				def  _lowerCamelCase   (				self				:       Any ,SCREAMING_SNAKE_CASE_				:       str ):
										if token in self.special_tokens_encoder:
																snake_case__    :   Union[str, Any]							     =   self.special_tokens_encoder[token]
										elif token in self.added_tokens_encoder:
																snake_case__    :   str							     =   self.added_tokens_encoder[token]
										elif len(SCREAMING_SNAKE_CASE_ ) != 1:
																snake_case__    :   Optional[int]							     =   self.unk_token_id
										else:
																snake_case__    :   Optional[Any]							     =   ord(SCREAMING_SNAKE_CASE_ ) + self._num_special_tokens
										return token_id

				def  _lowerCamelCase   (				self				:       str ,SCREAMING_SNAKE_CASE_				:       Optional[int] ):
										if index in self.special_tokens_decoder:
																snake_case__    :   str							     =   self.special_tokens_decoder[index]
										elif index in self.added_tokens_decoder:
																snake_case__    :   Union[str, Any]							     =   self.added_tokens_decoder[index]
										else:
																snake_case__    :   Union[str, Any]							     =   chr(index - self._num_special_tokens )
										return token

				def  _lowerCamelCase   (				self				:       Dict ,SCREAMING_SNAKE_CASE_				:       Tuple ):
										snake_case__    :   Dict							     =   B''''''
										for token in tokens:
																if token in self.special_tokens_decoder:
																						snake_case__    :   Optional[Any]							     =   self.special_tokens_decoder[token].encode('''utf-8''' )
																elif token in self.added_tokens_decoder:
																						snake_case__    :   int							     =   self.special_tokens_decoder[token].encode('''utf-8''' )
																elif token in self.special_tokens_encoder:
																						snake_case__    :   Tuple							     =   token.encode('''utf-8''' )
																elif token in self.added_tokens_encoder:
																						snake_case__    :   Union[str, Any]							     =   token.encode('''utf-8''' )
																else:
																						snake_case__    :   Union[str, Any]							     =   bytes([ord(SCREAMING_SNAKE_CASE_ )] )
																bstring += tok_string
										snake_case__    :   Dict							     =   bstring.decode('''utf-8''' ,errors='''replace''' )
										return string





				def  _lowerCamelCase   (				self				:       Union[str, Any] ,SCREAMING_SNAKE_CASE_				:       str ,SCREAMING_SNAKE_CASE_				:       Optional[str] = None ):
										return ()






"""simple docstring"""





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

def 						lowerCAmelCase							(	lowerCamelCase						):
						'''simple docstring'''


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

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

						def remove_punc(lowerCamelCase						):
												snake_case__    :   Tuple							     =   set(string.punctuation						)
												return "".join(ch for ch in text if ch not in exclude						)

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

						return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase						)						)						)						)

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


						snake_case__    :   Dict							     =   normalize_answer(lowerCamelCase						).split()
						snake_case__    :   Dict							     =   normalize_answer(lowerCamelCase						).split()
						snake_case__    :   List[Any]							     =   Counter(lowerCamelCase						) & Counter(lowerCamelCase						)
						snake_case__    :   List[str]							     =   sum(common.values()						)
						if num_same == 0:
												return 0
						snake_case__    :   int							     =   1.0 * num_same / len(lowerCamelCase						)
						snake_case__    :   str							     =   1.0 * num_same / len(lowerCamelCase						)
						snake_case__    :   Union[str, Any]							     =   (2 * precision * recall) / (precision + recall)
						return fa

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


						return normalize_answer(lowerCamelCase						) == normalize_answer(lowerCamelCase						)

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


						snake_case__    :   int							     =   []
						for ground_truth in ground_truths:
												snake_case__    :   Optional[Any]							     =   metric_fn(lowerCamelCase ,     lowerCamelCase						)
												scores_for_ground_truths.append(lowerCamelCase						)
						return max(lowerCamelCase						)

def 						lowerCAmelCase							(	lowerCamelCase ,     lowerCamelCase						):
						'''simple docstring'''


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

																		snake_case__    :   Any							     =   [x['''text'''] for x in qa['''answers''']]
																		snake_case__    :   Dict							     =   predictions[qa['''id''']]

																		snake_case__    :   Union[str, Any]							     =   metric_max_over_ground_truths(lowerCamelCase ,     lowerCamelCase ,     lowerCamelCase						)
																		if int(_exact_match						) == 1:
																								correct_ids.append(qa['''id''']						)
																		exact_match += _exact_match

																		fa += metric_max_over_ground_truths(lowerCamelCase ,     lowerCamelCase ,     lowerCamelCase						)

						snake_case__    :   str							     =   exact_match / total
						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)


