

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






def  UpperCamelCase_(							_snake_case     :						str			):




			"""simple docstring"""






			def remove_articles(_snake_case     :						int			):
						return re.sub(r'\b(a|an|the)\b' ,  ' ' ,  _snake_case			)

			def white_space_fix(_snake_case     :						Union[str, Any]			):
						return " ".join(text.split()			)

			def remove_punc(_snake_case     :						Dict			):
						__a						   =set(string.punctuation			)
						return "".join(ch for ch in text if ch not in exclude			)

			def lower(_snake_case     :						Any			):
						return text.lower()

			return white_space_fix(remove_articles(remove_punc(lower(_snake_case			)			)			)			)






def  UpperCamelCase_(							_snake_case     :						Dict ,  _snake_case     :						List[str]			):




			"""simple docstring"""






			__a						   =normalize_answer(_snake_case			).split()
			__a						   =normalize_answer(_snake_case			).split()
			__a						   =Counter(_snake_case			) & Counter(_snake_case			)
			__a						   =sum(common.values()			)
			if num_same == 0:
						return 0
			__a						   =1.0 * num_same / len(_snake_case			)
			__a						   =1.0 * num_same / len(_snake_case			)
			__a						   =(2 * precision * recall) / (precision + recall)
			return fa






def  UpperCamelCase_(							_snake_case     :						Optional[Any] ,  _snake_case     :						str			):




			"""simple docstring"""






			return normalize_answer(_snake_case			) == normalize_answer(_snake_case			)






def  UpperCamelCase_(							_snake_case     :						Tuple ,  _snake_case     :						Any ,  _snake_case     :						int			):




			"""simple docstring"""






			__a						   =[]
			for ground_truth in ground_truths:
						__a						   =metric_fn(_snake_case ,  _snake_case			)
						scores_for_ground_truths.append(_snake_case			)
			return max(_snake_case			)






def  UpperCamelCase_(							_snake_case     :						Union[str, Any] ,  _snake_case     :						Any			):




			"""simple docstring"""






			__a						   =__a						   =__a						   =0
			__a						   =[]
			for passage in dataset:
						for qa in passage["qas"]:
									total += 1
									if qa["id"] not in predictions:
												__a						   =F'Unanswered question {qa["id"]} will receive score 0.'
												print(_snake_case ,  file=sys.stderr			)
												continue

									__a						   =[x['text'] for x in qa['answers']]
									__a						   =predictions[qa['id']]

									__a						   =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			)

			__a						   =exact_match / total
			__a						   =fa / total

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


if __name__ == "__main__":
						_lowerCAmelCase      :    List[Any]						       =    "1.0"
						_lowerCAmelCase      :    List[Any]						       =    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.")
						_lowerCAmelCase      :    List[str]						       =    parser.parse_args()

						with open(args.data_file) as data_file:
												_lowerCAmelCase      :    Tuple						       =    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,
																		)
												_lowerCAmelCase      :    int						       =    dataset_json["data"]

						with open(args.pred_file) as pred_file:
												_lowerCAmelCase      :    List[str]						       =    json.load(pred_file)

						_lowerCAmelCase      :    str						       =    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)








# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
						import tensorflow as tf






class 			__magic_name__					(    TensorFormatter[Mapping, 'tf.Tensor', Mapping]   ):





			def __init__(	self      ,						__snake_case=None      ,						**__snake_case      )     ->				Union[str, Any]:




						'''simple docstring'''
						super().__init__(features=__snake_case      )
						__a						   =tf_tensor_kwargs
						import tensorflow as tf  # noqa: import tf at initialization





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				int:




						'''simple docstring'''
						import tensorflow as tf

						if isinstance(__snake_case      ,						__snake_case      ) and column:
									if all(
									    isinstance(__snake_case      ,						tf.Tensor      ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column      ):
												return tf.stack(__snake_case      )
									elif all(
									    isinstance(__snake_case      ,						(tf.Tensor, tf.RaggedTensor)      ) and x.ndim == 1 and x.dtype == column[0].dtype
									    for x in column      ):
												# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
												return tf.ragged.stack(__snake_case      )

						return column





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Any:




						'''simple docstring'''
						import tensorflow as tf

						if value is None:
									return value

						__a						   ={}

						if isinstance(__snake_case      ,						(np.number, np.ndarray)      ) and np.issubdtype(value.dtype      ,						np.integer      ):
									__a						   ={'dtype': tf.intaa}
						elif isinstance(__snake_case      ,						(np.number, np.ndarray)      ) and np.issubdtype(value.dtype      ,						np.floating      ):
									__a						   ={'dtype': tf.floataa}
						elif config.PIL_AVAILABLE and "PIL" in sys.modules:
									import PIL.Image

									if isinstance(__snake_case      ,						PIL.Image.Image      ):
												__a						   =np.asarray(__snake_case      )

						return tf.convert_to_tensor(__snake_case      ,						**{**default_dtype, **self.tf_tensor_kwargs}      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Dict:




						'''simple docstring'''
						import tensorflow as tf

						# support for torch, tf, jax etc.
						if config.TORCH_AVAILABLE and "torch" in sys.modules:
									import torch

									if isinstance(__snake_case      ,						torch.Tensor      ):
												return self._tensorize(data_struct.detach().cpu().numpy()[()]      )
						if hasattr(__snake_case      ,						'__array__'      ) and not isinstance(__snake_case      ,						tf.Tensor      ):
									__a						   =data_struct.__array__()
						# support for nested types like struct of list of struct
						if isinstance(__snake_case      ,						np.ndarray      ):
									if data_struct.dtype == object:  # tf tensors cannot be instantied from an array of objects
												return self._consolidate([self.recursive_tensorize(__snake_case      ) for substruct in data_struct]      )
						elif isinstance(__snake_case      ,						(list, tuple)      ):
									return self._consolidate([self.recursive_tensorize(__snake_case      ) for substruct in data_struct]      )
						return self._tensorize(__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Optional[Any]:




						'''simple docstring'''
						return map_nested(self._recursive_tensorize      ,						__snake_case      ,						map_list=__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Mapping:




						'''simple docstring'''
						__a						   =self.numpy_arrow_extractor().extract_row(__snake_case      )
						__a						   =self.python_features_decoder.decode_row(__snake_case      )
						return self.recursive_tensorize(__snake_case      )





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				"tf.Tensor":




						'''simple docstring'''
						__a						   =self.numpy_arrow_extractor().extract_column(__snake_case      )
						__a						   =self.python_features_decoder.decode_column(__snake_case      ,						pa_table.column_names[0]      )
						__a						   =self.recursive_tensorize(__snake_case      )
						__a						   =self._consolidate(__snake_case      )
						return column





			def 	__magic_name__					(	self      ,						__snake_case      )     ->				Mapping:




						'''simple docstring'''
						__a						   =self.numpy_arrow_extractor().extract_batch(__snake_case      )
						__a						   =self.python_features_decoder.decode_batch(__snake_case      )
						__a						   =self.recursive_tensorize(__snake_case      )
						for column_name in batch:
									__a						   =self._consolidate(batch[column_name]      )
						return batch






