





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

def      __snake_case						(					_lowerCAmelCase : int	) ->						str:







   def remove_articles(_lowerCAmelCase : Any	):
      return re.sub(r"\b(a|an|the)\b"				,				" "				,				_lowerCAmelCase	)

   def white_space_fix(_lowerCAmelCase : Any	):
      return " ".join(text.split()	)

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

   def lower(_lowerCAmelCase : str	):
      return text.lower()

   return white_space_fix(remove_articles(remove_punc(lower(_lowerCAmelCase	)	)	)	)

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

def      __snake_case						(					_lowerCAmelCase : List[str]				,				_lowerCAmelCase : int	) ->						Any:
   return normalize_answer(_lowerCAmelCase	) == normalize_answer(_lowerCAmelCase	)

def      __snake_case						(					_lowerCAmelCase : int				,				_lowerCAmelCase : Tuple				,				_lowerCAmelCase : Tuple	) ->						Dict:
   A_					:				List[Any]          =			[]
   for ground_truth in ground_truths:
      A_					:				Tuple          =			metric_fn(_lowerCAmelCase				,				_lowerCAmelCase	)
      scores_for_ground_truths.append(_lowerCAmelCase	)
   return max(_lowerCAmelCase	)

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

   A_					:				Tuple          =			1_00.0 * exact_match / total
   A_					:				int          =			1_00.0 * fa / total

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


if __name__ == "__main__":
 _lowerCAmelCase							:      Optional[Any]						=	'1.1'
 _lowerCAmelCase							:      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''')
 _lowerCAmelCase							:      int						=	parser.parse_args()
 with open(args.dataset_file) as dataset_file:
  _lowerCAmelCase							:      List[str]						=	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,
   )
  _lowerCAmelCase							:      List[str]						=	dataset_json['data']
 with open(args.prediction_file) as prediction_file:
  _lowerCAmelCase							:      str						=	json.load(prediction_file)
 print(json.dumps(evaluate(dataset, predictions)))






def      __snake_case						(					_lowerCAmelCase : int				,				_lowerCAmelCase : int = 1000	) ->						str:
   if (not isinstance(_lowerCAmelCase				,				_lowerCAmelCase	)) or (digit_position <= 0):
      raise ValueError("Digit position must be a positive integer"	)
   elif (not isinstance(_lowerCAmelCase				,				_lowerCAmelCase	)) or (precision < 0):
      raise ValueError("Precision must be a nonnegative integer"	)

   # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
   # accurate
   A_					:				List[str]          =			(
       4 * _subsum(_lowerCAmelCase				,				1				,				_lowerCAmelCase	)
       - 2 * _subsum(_lowerCAmelCase				,				4				,				_lowerCAmelCase	)
       - _subsum(_lowerCAmelCase				,				5				,				_lowerCAmelCase	)
       - _subsum(_lowerCAmelCase				,				6				,				_lowerCAmelCase	)
   )

   # return the first hex digit of the fractional part of the result
   return hex(int((sum_result % 1) * 16	)	)[2:]

def      __snake_case						(					_lowerCAmelCase : int				,				_lowerCAmelCase : int				,				_lowerCAmelCase : int	) ->						float:
   A_					:				int          =			0.0
   for sum_index in range(digit_pos_to_extract + precision	):
      A_					:				Optional[int]          =			8 * sum_index + denominator_addend
      if sum_index < digit_pos_to_extract:
         # if the exponential term is an integer and we mod it by the denominator
         # before dividing, only the integer part of the sum will change;
         # the fractional part will not
         A_					:				Optional[Any]          =			pow(
             16				,				digit_pos_to_extract - 1 - sum_index				,				_lowerCAmelCase	)
      else:
         A_					:				Tuple          =			pow(16				,				digit_pos_to_extract - 1 - sum_index	)
      total += exponential_term / denominator
   return total


if __name__ == "__main__":
 import doctest

 doctest.testmod()
