






"""simple docstring"""





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



def      __lowercase       (     a			:					Optional[Any]			)       ->       Optional[int]:






      def remove_articles(a			:					Union[str, Any]			):
            return re.sub(R'''\b(a|an|the)\b'''							,		''' '''							,		_UpperCAmelCase			)

      def white_space_fix(a			:					Dict			):
            return " ".join(text.split()			)

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

      def lower(a			:					Optional[int]			):
            return text.lower()

      return white_space_fix(remove_articles(remove_punc(lower(_UpperCAmelCase			)			)			)			)



def      __lowercase       (     a			:					List[str]							,		a			:					Union[str, Any]			)       ->       Tuple:
      __snake_case   :     str	      =normalize_answer(_UpperCAmelCase			).split()
      __snake_case   :     Optional[int]	      =normalize_answer(_UpperCAmelCase			).split()
      __snake_case   :     List[str]	      =Counter(_UpperCAmelCase			) & Counter(_UpperCAmelCase			)
      __snake_case   :     Any	      =sum(common.values()			)
      if num_same == 0:
            return 0
      __snake_case   :     str	      =1.0 * num_same / len(_UpperCAmelCase			)
      __snake_case   :     Dict	      =1.0 * num_same / len(_UpperCAmelCase			)
      __snake_case   :     Optional[int]	      =(2 * precision * recall) / (precision + recall)
      return fa



def      __lowercase       (     a			:					Tuple							,		a			:					Union[str, Any]			)       ->       List[Any]:
      return normalize_answer(_UpperCAmelCase			) == normalize_answer(_UpperCAmelCase			)



def      __lowercase       (     a			:					Optional[Any]							,		a			:					int							,		a			:					Optional[int]			)       ->       Union[str, Any]:
      __snake_case   :     Union[str, Any]	      =[]
      for ground_truth in ground_truths:
            __snake_case   :     Tuple	      =metric_fn(_UpperCAmelCase							,		_UpperCAmelCase			)
            scores_for_ground_truths.append(_UpperCAmelCase			)
      return max(_UpperCAmelCase			)



def      __lowercase       (     a			:					Any							,		a			:					Any			)       ->       Union[str, Any]:
      __snake_case   :     List[str]	      =0
      for article in dataset:
            for paragraph in article["paragraphs"]:
                  for qa in paragraph["qas"]:
                        total += 1
                        if qa["id"] not in predictions:
                              __snake_case   :     Union[str, Any]	      ='''Unanswered question ''' + qa['''id'''] + ''' will receive score 0.'''
                              print(_UpperCAmelCase							,		file=sys.stderr			)
                              continue
                        __snake_case   :     Dict	      =[x['''text'''] for x in qa['''answers''']]
                        __snake_case   :     Union[str, Any]	      =predictions[qa['''id''']]
                        exact_match += metric_max_over_ground_truths(_UpperCAmelCase							,		_UpperCAmelCase							,		_UpperCAmelCase			)
                        fa += metric_max_over_ground_truths(_UpperCAmelCase							,		_UpperCAmelCase							,		_UpperCAmelCase			)

      __snake_case   :     Optional[int]	      =1_0_0.0 * exact_match / total
      __snake_case   :     Union[str, Any]	      =1_0_0.0 * fa / total

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


if __name__ == "__main__":
    UpperCamelCase_						:					Union[str, Any]      				=			"""1.1"""
    UpperCamelCase_						:					List[Any]      				=			argparse.ArgumentParser(description="""Evaluation for SQuAD """ + expected_version)
    parser.add_argument("""dataset_file""", help="""Dataset file""")
    parser.add_argument("""prediction_file""", help="""Prediction File""")
    UpperCamelCase_						:					Dict      				=			parser.parse_args()
    with open(args.dataset_file) as dataset_file:
        UpperCamelCase_						:					Optional[Any]      				=			json.load(dataset_file)
        if dataset_json["version"] != expected_version:
            print(
                """Evaluation expects v-""" + expected_version + """, but got dataset with v-""" + dataset_json["""version"""],
                file=sys.stderr,
            )
        UpperCamelCase_						:					List[str]      				=			dataset_json["""data"""]
    with open(args.prediction_file) as prediction_file:
        UpperCamelCase_						:					int      				=			json.load(prediction_file)
    print(json.dumps(evaluate(dataset, predictions)))







from typing import List, Optional, Tuple, Union

from . import logging
from .import_utils import is_torch_available, is_torch_version


if is_torch_available():
		import torch

__magic_name__       =     logging.get_logger(__name__)  # pylint: disable=invalid-name

try:
		from torch._dynamo import allow_in_graph as maybe_allow_in_graph
except (ImportError, ModuleNotFoundError):





		def 		__snake_case						(    cls				):
							"""simple docstring"""






							return cls





def 		__snake_case						(    _UpperCAmelCase       ,			_UpperCAmelCase = None       ,			_UpperCAmelCase = None       ,			_UpperCAmelCase = None       ,			_UpperCAmelCase = None       ,			):
					"""simple docstring"""






					lowercase						   =       device
					lowercase						   =       shape[0]

					lowercase						   =       layout or torch.strided
					lowercase						   =       device or torch.device('cpu'				)

					if generator is not None:
										lowercase						   =       generator.device.type if not isinstance(_UpperCAmelCase       ,			_UpperCAmelCase				) else generator[0].device.type
										if gen_device_type != device.type and gen_device_type == "cpu":
															lowercase						   =       'cpu'
															if device != "mps":
																				logger.info(
																				    f"""The passed generator was created on 'cpu' even though a tensor on {device} was expected."""
																				    f""" Tensors will be created on 'cpu' and then moved to {device}. Note that one can probably"""
																				    f""" slighly speed up this function by passing a generator that was created on the {device} device."""				)
										elif gen_device_type != device.type and gen_device_type == "cuda":
															raise ValueError(f"""Cannot generate a {device} tensor from a generator of type {gen_device_type}."""				)

					if isinstance(_UpperCAmelCase       ,			_UpperCAmelCase				):
										lowercase						   =       (1,) + shape[1:]
										lowercase						   =       [
										    torch.randn(_UpperCAmelCase       ,			generator=generator[i]       ,			device=_UpperCAmelCase       ,			dtype=_UpperCAmelCase       ,			layout=_UpperCAmelCase				)
										    for i in range(_UpperCAmelCase				)
										]
										lowercase						   =       torch.cat(_UpperCAmelCase       ,			dim=0				).to(_UpperCAmelCase				)
					else:
										lowercase						   =       torch.randn(_UpperCAmelCase       ,			generator=_UpperCAmelCase       ,			device=_UpperCAmelCase       ,			dtype=_UpperCAmelCase       ,			layout=_UpperCAmelCase				).to(_UpperCAmelCase				)

					return latents





def 		__snake_case						(    _UpperCAmelCase				):
					"""simple docstring"""






					if is_torch_version('<'       ,			'2.0.0'				) or not hasattr(_UpperCAmelCase       ,			'_dynamo'				):
										return False
					return isinstance(_UpperCAmelCase       ,			torch._dynamo.eval_frame.OptimizedModule				)






