




import argparse
import json
import re
import string
import sys
from collections import Counter
def 							__lowerCAmelCase			(UpperCAmelCase_		):


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

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

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

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

    return white_space_fix(remove_articles(remove_punc(lower(_SCREAMING_SNAKE_CASE		)		)		)		)
def 							__lowerCAmelCase			(UpperCAmelCase_ ,     UpperCAmelCase_		):
    a__  			=							normalize_answer(_SCREAMING_SNAKE_CASE		).split()
    a__  			=							normalize_answer(_SCREAMING_SNAKE_CASE		).split()
    a__  			=							Counter(_SCREAMING_SNAKE_CASE		) & Counter(_SCREAMING_SNAKE_CASE		)
    a__  			=							sum(common.values()		)
    if num_same == 0:
        return 0
    a__  			=							1.0 * num_same / len(_SCREAMING_SNAKE_CASE		)
    a__  			=							1.0 * num_same / len(_SCREAMING_SNAKE_CASE		)
    a__  			=							(2 * precision * recall) / (precision + recall)
    return fa
def 							__lowerCAmelCase			(UpperCAmelCase_ ,     UpperCAmelCase_		):
    return normalize_answer(_SCREAMING_SNAKE_CASE		) == normalize_answer(_SCREAMING_SNAKE_CASE		)
def 							__lowerCAmelCase			(UpperCAmelCase_ ,     UpperCAmelCase_ ,     UpperCAmelCase_		):
    a__  			=							[]
    for ground_truth in ground_truths:
        a__  			=							metric_fn(_SCREAMING_SNAKE_CASE ,     _SCREAMING_SNAKE_CASE		)
        scores_for_ground_truths.append(_SCREAMING_SNAKE_CASE		)
    return max(_SCREAMING_SNAKE_CASE		)
def 							__lowerCAmelCase			(UpperCAmelCase_ ,     UpperCAmelCase_		):
    a__  			=							0
    for article in dataset:
        for paragraph in article["paragraphs"]:
            for qa in paragraph["qas"]:
                total += 1
                if qa["id"] not in predictions:
                    a__  			=							'Unanswered question ' + qa['id'] + ' will receive score 0.'
                    print(_SCREAMING_SNAKE_CASE ,     file=sys.stderr		)
                    continue
                a__  			=							[x['text'] for x in qa['answers']]
                a__  			=							predictions[qa['id']]
                exact_match += metric_max_over_ground_truths(_SCREAMING_SNAKE_CASE ,     _SCREAMING_SNAKE_CASE ,     _SCREAMING_SNAKE_CASE		)
                fa += metric_max_over_ground_truths(_SCREAMING_SNAKE_CASE ,     _SCREAMING_SNAKE_CASE ,     _SCREAMING_SNAKE_CASE		)

    a__  			=							100.0 * exact_match / total
    a__  			=							100.0 * fa / total

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


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE_    :       Union[str, Any]       =							'''1.1'''
 SCREAMING_SNAKE_CASE_    :       Tuple       =							argparse.ArgumentParser(description='''Evaluation for SQuAD ''' + expected_version)
 parser.add_argument('''dataset_file''', help='''Dataset file''')
 parser.add_argument('''prediction_file''', help='''Prediction File''')
 SCREAMING_SNAKE_CASE_    :       Any       =							parser.parse_args()
 with open(args.dataset_file) as dataset_file:
  SCREAMING_SNAKE_CASE_    :       Tuple       =							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,
   )
  SCREAMING_SNAKE_CASE_    :       Dict       =							dataset_json['''data''']
 with open(args.prediction_file) as prediction_file:
  SCREAMING_SNAKE_CASE_    :       List[Any]       =							json.load(prediction_file)
 print(json.dumps(evaluate(dataset, predictions)))







"""simple docstring"""







def 						_lowercase				(  _SCREAMING_SNAKE_CASE				:				list[int]						)    ->     tuple[int, int]:







     '''simple docstring'''


     if len(_SCREAMING_SNAKE_CASE						) == 1:
          return a[0], a[0]
     else:
          # split A into half.
          __A					:					List[str]								=		a[: len(_SCREAMING_SNAKE_CASE						) // 2]
          __A					:					Optional[Any]								=		a[len(_SCREAMING_SNAKE_CASE						) // 2 :]

          # 2 sub problems, 1/2 of original size.
          __A						,		__A					:					int								=		max_difference(_SCREAMING_SNAKE_CASE						)
          __A						,		__A					:					str								=		max_difference(_SCREAMING_SNAKE_CASE						)

          # get min of first and max of second
          # linear time
          __A					:					str								=		min(_SCREAMING_SNAKE_CASE						)
          __A					:					Optional[int]								=		max(_SCREAMING_SNAKE_CASE						)

          # 3 cases, either (small1, big1),
          # (min_first, max_second), (small2, big2)
          # constant comparisons
          if biga - smalla > max_second - min_first and biga - smalla > biga - smalla:
               return smalla, biga
          elif biga - smalla > max_second - min_first:
               return smalla, biga
          else:
               return min_first, max_second


if __name__ == "__main__":
       import doctest

       doctest.testmod()



