

import argparse
import json
import re
import string
import sys
from collections import Counter
def 		UpperCamelCase_(    snake_case__:						List[str]     ) ->				str:

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

 def white_space_fix(snake_case__:						Tuple     ):
  return " ".join(text.split()     )

 def remove_punc(snake_case__:						List[Any]     ):
  UpperCAmelCase__           =       set(string.punctuation     )
  return "".join(ch for ch in text if ch not in exclude     )

 def lower(snake_case__:						List[str]     ):
  return text.lower()

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

 UpperCAmelCase__           =       1_00.0 * exact_match / total
 UpperCAmelCase__           =       1_00.0 * fa / total

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


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






def 		UpperCamelCase_(    snake_case__:						int     ) ->				int:
 if not isinstance(snake_case__						,					snake_case__     ):
  UpperCAmelCase__           =       f"Input value of [number={number}] must be an integer"
  raise TypeError(snake_case__     )
 if number < 1:
  raise ValueError('Input must be a positive integer'     )
 return number * (2 * number - 1)


if __name__ == "__main__":
 import doctest

 doctest.testmod()



