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







def 	UpperCAmelCase       (		lowerCAmelCase_       )	->	Dict:

     def remove_articles(lowerCAmelCase_       ):
          return re.sub(R'''\b(a|an|the)\b'''						,      ''' '''						,      lowerCAmelCase_       )

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

     def remove_punc(lowerCAmelCase_       ):
          a  :    Optional[int]				=						set(string.punctuation       )
          return "".join(ch for ch in text if ch not in exclude       )

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

     return white_space_fix(remove_articles(remove_punc(lower(lowerCAmelCase_       )       )       )       )







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







def 	UpperCAmelCase       (		lowerCAmelCase_						,      lowerCAmelCase_       )	->	Dict:
     return normalize_answer(lowerCAmelCase_       ) == normalize_answer(lowerCAmelCase_       )







def 	UpperCAmelCase       (		lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_       )	->	int:
     a  :    List[str]				=						[]
     for ground_truth in ground_truths:
          a  :    Any				=						metric_fn(lowerCAmelCase_						,      lowerCAmelCase_       )
          scores_for_ground_truths.append(lowerCAmelCase_       )
     return max(lowerCAmelCase_       )







def 	UpperCAmelCase       (		lowerCAmelCase_						,      lowerCAmelCase_       )	->	Optional[int]:
     a  :    Any				=						0
     a  :    List[str]				=						[]
     for passage in dataset:
          for qa in passage["qas"]:
               total += 1
               if qa["id"] not in predictions:
                    a  :    Any				=						f"""Unanswered question {qa['id']} will receive score 0."""
                    print(lowerCAmelCase_						,      file=sys.stderr       )
                    continue

               a  :    str				=						[x['''text'''] for x in qa['''answers''']]
               a  :    str				=						predictions[qa['''id''']]

               a  :    Any				=						metric_max_over_ground_truths(lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_       )
               if int(_exact_match       ) == 1:
                    correct_ids.append(qa['''id''']       )
               exact_match += _exact_match

               fa += metric_max_over_ground_truths(lowerCAmelCase_						,      lowerCAmelCase_						,      lowerCAmelCase_       )

     a  :    str				=						exact_match / total
     a  :    Tuple				=						fa / total

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


if __name__ == "__main__":
  UpperCAmelCase							   =      """1.0"""
  UpperCAmelCase							   =      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.""")
  UpperCAmelCase							   =      parser.parse_args()

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

  with open(args.pred_file) as pred_file:
    UpperCAmelCase							   =      json.load(pred_file)

  UpperCAmelCase   ,  UpperCAmelCase							   =      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)
def 	UpperCAmelCase       (		lowerCAmelCase_       )	->	int:
     return 1 if input_a == 0 else 0







def 	UpperCAmelCase       (		)	->	None:
     assert not_gate(0       ) == 1
     assert not_gate(1       ) == 0


if __name__ == "__main__":
  print(not_gate(0))
  print(not_gate(1))