





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




def 			lowerCAmelCase_			(				UpperCamelCase_	)  ->		str:

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

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

       def remove_punc(UpperCamelCase_	):
              UpperCamelCase_ 							=	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(UpperCamelCase_	)	)	)	)




def 			lowerCAmelCase_			(				UpperCamelCase_       ,		UpperCamelCase_	)  ->		Any:
       UpperCamelCase_ 							=	normalize_answer(UpperCamelCase_	).split()
       UpperCamelCase_ 							=	normalize_answer(UpperCamelCase_	).split()
       UpperCamelCase_ 							=	Counter(UpperCamelCase_	) & Counter(UpperCamelCase_	)
       UpperCamelCase_ 							=	sum(common.values()	)
       if num_same == 0:
              return 0
       UpperCamelCase_ 							=	1.0 * num_same / len(UpperCamelCase_	)
       UpperCamelCase_ 							=	1.0 * num_same / len(UpperCamelCase_	)
       UpperCamelCase_ 							=	(2 * precision * recall) / (precision + recall)
       return fa




def 			lowerCAmelCase_			(				UpperCamelCase_       ,		UpperCamelCase_	)  ->		Optional[Any]:
       return normalize_answer(UpperCamelCase_	) == normalize_answer(UpperCamelCase_	)




def 			lowerCAmelCase_			(				UpperCamelCase_       ,		UpperCamelCase_       ,		UpperCamelCase_	)  ->		Tuple:
       UpperCamelCase_ 							=	[]
       for ground_truth in ground_truths:
              UpperCamelCase_ 							=	metric_fn(UpperCamelCase_       ,		UpperCamelCase_	)
              scores_for_ground_truths.append(UpperCamelCase_	)
       return max(UpperCamelCase_	)




def 			lowerCAmelCase_			(				UpperCamelCase_       ,		UpperCamelCase_	)  ->		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(UpperCamelCase_       ,		file=sys.stderr	)
                                   continue
                            UpperCamelCase_ 							=	[x["text"] for x in qa["answers"]]
                            UpperCamelCase_ 							=	predictions[qa["id"]]
                            exact_match += metric_max_over_ground_truths(UpperCamelCase_       ,		UpperCamelCase_       ,		UpperCamelCase_	)
                            fa += metric_max_over_ground_truths(UpperCamelCase_       ,		UpperCamelCase_       ,		UpperCamelCase_	)

       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       _a   (							lowerCamelCase__				)  ->      list[int]:
 if len(lowerCamelCase__				) > 1:
  lowerCamelCase_							:    Optional[Any]							=     len(lowerCamelCase__				) // 2  # Finds the middle of the array
  lowerCamelCase_							:    int							=     arr[
      :middle_length
  ]  # Creates an array of the elements in the first half.
  lowerCamelCase_							:    List[Any]							=     arr[
      middle_length:
  ]  # Creates an array of the elements in the second half.
  lowerCamelCase_							:    Optional[Any]							=     len(lowerCamelCase__				)
  lowerCamelCase_							:    Optional[Any]							=     len(lowerCamelCase__				)
  merge(lowerCamelCase__				)  # Starts sorting the left.
  merge(lowerCamelCase__				)  # Starts sorting the right
  lowerCamelCase_							:    Dict							=     0  # Left Counter
  lowerCamelCase_							:    str							=     0  # Right Counter
  lowerCamelCase_							:    Tuple							=     0  # Position Counter
  while (
      left_index < left_size and right_index < right_size
  ):  # Runs until the lowers size of the left and right are sorted.
   if left_array[left_index] < right_array[right_index]:
    lowerCamelCase_							:    Optional[int]							=     left_array[left_index]
    left_index += 1
   else:
    lowerCamelCase_							:    Union[str, Any]							=     right_array[right_index]
    right_index += 1
   index += 1
  while (
      left_index < left_size
  ):  # Adds the left over elements in the left half of the array
   lowerCamelCase_							:    Tuple							=     left_array[left_index]
   left_index += 1
   index += 1
  while (
      right_index < right_size
  ):  # Adds the left over elements in the right half of the array
   lowerCamelCase_							:    Optional[int]							=     right_array[right_index]
   right_index += 1
   index += 1
 return arr


if __name__ == "__main__":
       import doctest

       doctest.testmod()
