





"""simple docstring"""





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


def       UpperCAmelCase__							(  SCREAMING_SNAKE_CASE					:						List[Any]      ):







      '''simple docstring'''




      def remove_articles(SCREAMING_SNAKE_CASE					:						int      ):
            return re.sub(R"""\b(a|an|the)\b"""							,						""" """							,						SCREAMING_SNAKE_CASE      )

      def white_space_fix(SCREAMING_SNAKE_CASE					:						int      ):
            return " ".join(text.split()      )

      def remove_punc(SCREAMING_SNAKE_CASE					:						Union[str, Any]      ):
            lowerCAmelCase  						=	set(string.punctuation      )
            return "".join(ch for ch in text if ch not in exclude      )

      def lower(SCREAMING_SNAKE_CASE					:						Optional[Any]      ):
            return text.lower()

      return white_space_fix(remove_articles(remove_punc(lower(SCREAMING_SNAKE_CASE      )      )      )      )


def       UpperCAmelCase__							(  SCREAMING_SNAKE_CASE					:						Union[str, Any]							,						SCREAMING_SNAKE_CASE					:						Optional[int]      ):







      '''simple docstring'''




      lowerCAmelCase  						=	normalize_answer(SCREAMING_SNAKE_CASE      ).split()
      lowerCAmelCase  						=	normalize_answer(SCREAMING_SNAKE_CASE      ).split()
      lowerCAmelCase  						=	Counter(SCREAMING_SNAKE_CASE      ) & Counter(SCREAMING_SNAKE_CASE      )
      lowerCAmelCase  						=	sum(common.values()      )
      if num_same == 0:
            return 0
      lowerCAmelCase  						=	1.0 * num_same / len(SCREAMING_SNAKE_CASE      )
      lowerCAmelCase  						=	1.0 * num_same / len(SCREAMING_SNAKE_CASE      )
      lowerCAmelCase  						=	(2 * precision * recall) / (precision + recall)
      return fa


def       UpperCAmelCase__							(  SCREAMING_SNAKE_CASE					:						Tuple							,						SCREAMING_SNAKE_CASE					:						Tuple      ):







      '''simple docstring'''




      return normalize_answer(SCREAMING_SNAKE_CASE      ) == normalize_answer(SCREAMING_SNAKE_CASE      )


def       UpperCAmelCase__							(  SCREAMING_SNAKE_CASE					:						List[Any]							,						SCREAMING_SNAKE_CASE					:						Any							,						SCREAMING_SNAKE_CASE					:						str      ):







      '''simple docstring'''




      lowerCAmelCase  						=	[]
      for ground_truth in ground_truths:
            lowerCAmelCase  						=	metric_fn(SCREAMING_SNAKE_CASE							,						SCREAMING_SNAKE_CASE      )
            scores_for_ground_truths.append(SCREAMING_SNAKE_CASE      )
      return max(SCREAMING_SNAKE_CASE      )


def       UpperCAmelCase__							(  SCREAMING_SNAKE_CASE					:						Optional[int]							,						SCREAMING_SNAKE_CASE					:						Dict      ):







      '''simple docstring'''




      lowerCAmelCase  						=	lowerCAmelCase  						=	lowerCAmelCase  						=	0
      for article in dataset:
            for paragraph in article["paragraphs"]:
                  for qa in paragraph["qas"]:
                        total += 1
                        if qa["id"] not in predictions:
                              lowerCAmelCase  						=	"""Unanswered question """ + qa["""id"""] + """ will receive score 0."""
                              print(SCREAMING_SNAKE_CASE							,						file=sys.stderr      )
                              continue
                        lowerCAmelCase  						=	[x["""text"""] for x in qa["""answers"""]]
                        lowerCAmelCase  						=	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      )

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

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


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













"""simple docstring"""





import unittest
from dataclasses import dataclass
from typing import Optional

from transformers.utils import ModelOutput

@dataclass
class 		lowercase ( _UpperCAmelCase ):
      _SCREAMING_SNAKE_CASE         =				42
      _SCREAMING_SNAKE_CASE         =				None
      _SCREAMING_SNAKE_CASE         =				None







class 		lowercase ( unittest.TestCase ):





      def    _snake_case      (	self			)		->					Optional[Any]:
            lowerCAmelCase  						=	ModelOutputTest(a=30			)
            self.assertEqual(x.a				,						30			)
            self.assertIsNone(x.b			)
            self.assertIsNone(x.c			)
            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	x.d





      def    _snake_case      (	self			)		->					Optional[Any]:
            lowerCAmelCase  						=	ModelOutputTest(a=30				,						b=10			)
            self.assertEqual(x[0]				,						30			)
            self.assertEqual(x[1]				,						10			)
            self.assertEqual(x[:2]				,						(30, 10)			)
            self.assertEqual(x[:]				,						(30, 10)			)

            lowerCAmelCase  						=	ModelOutputTest(a=30				,						c=10			)
            self.assertEqual(x[0]				,						30			)
            self.assertEqual(x[1]				,						10			)
            self.assertEqual(x[:2]				,						(30, 10)			)
            self.assertEqual(x[:]				,						(30, 10)			)





      def    _snake_case      (	self			)		->					Tuple:
            lowerCAmelCase  						=	ModelOutputTest(a=30				,						b=10			)
            self.assertEqual(x["""a"""]				,						30			)
            self.assertEqual(x["""b"""]				,						10			)
            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	x["""c"""]

            lowerCAmelCase  						=	ModelOutputTest(a=30				,						c=10			)
            self.assertEqual(x["""a"""]				,						30			)
            self.assertEqual(x["""c"""]				,						10			)
            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	x["""b"""]





      def    _snake_case      (	self			)		->					Union[str, Any]:
            lowerCAmelCase  						=	ModelOutputTest(a=30			)
            self.assertEqual(list(x.keys()			)				,						["""a"""]			)
            self.assertEqual(list(x.values()			)				,						[30]			)
            self.assertEqual(list(x.items()			)				,						[("""a""", 30)]			)
            self.assertEqual(list(lowercase			)				,						["""a"""]			)

            lowerCAmelCase  						=	ModelOutputTest(a=30				,						b=10			)
            self.assertEqual(list(x.keys()			)				,						["""a""", """b"""]			)
            self.assertEqual(list(x.values()			)				,						[30, 10]			)
            self.assertEqual(list(x.items()			)				,						[("""a""", 30), ("""b""", 10)]			)
            self.assertEqual(list(lowercase			)				,						["""a""", """b"""]			)

            lowerCAmelCase  						=	ModelOutputTest(a=30				,						c=10			)
            self.assertEqual(list(x.keys()			)				,						["""a""", """c"""]			)
            self.assertEqual(list(x.values()			)				,						[30, 10]			)
            self.assertEqual(list(x.items()			)				,						[("""a""", 30), ("""c""", 10)]			)
            self.assertEqual(list(lowercase			)				,						["""a""", """c"""]			)

            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	x.update({"""d""": 20}			)
            with self.assertRaises(lowercase			):
                  del x["a"]
            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	x.pop("""a"""			)
            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	x.setdefault("""d"""				,						32			)





      def    _snake_case      (	self			)		->					Dict:
            lowerCAmelCase  						=	ModelOutputTest(a=30			)
            lowerCAmelCase  						=	10
            self.assertEqual(x.a				,						10			)
            self.assertEqual(x["""a"""]				,						10			)





      def    _snake_case      (	self			)		->					List[str]:
            lowerCAmelCase  						=	ModelOutputTest(a=30			)
            lowerCAmelCase  						=	10
            self.assertEqual(x.a				,						10			)
            self.assertEqual(x["""a"""]				,						10			)





      def    _snake_case      (	self			)		->					str:
            lowerCAmelCase  						=	ModelOutputTest({"""a""": 30, """b""": 10}			)
            self.assertEqual(list(x.keys()			)				,						["""a""", """b"""]			)
            self.assertEqual(x.a				,						30			)
            self.assertEqual(x.b				,						10			)





      def    _snake_case      (	self			)		->					Dict:
            lowerCAmelCase  						=	ModelOutputTest([("""a""", 30), ("""b""", 10)]			)
            self.assertEqual(list(x.keys()			)				,						["""a""", """b"""]			)
            self.assertEqual(x.a				,						30			)
            self.assertEqual(x.b				,						10			)

            with self.assertRaises(lowercase			):
                  lowerCAmelCase  						=	ModelOutputTest([("""a""", 30), (10, 10)]			)

            lowerCAmelCase  						=	ModelOutputTest(a=(30, 30)			)
            self.assertEqual(list(x.keys()			)				,						["""a"""]			)
            self.assertEqual(x.a				,						(30, 30)			)






