





"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


SCREAMING_SNAKE_CASE__ =       {
    "configuration_dpr": ["DPR_PRETRAINED_CONFIG_ARCHIVE_MAP", "DPRConfig"],
    "tokenization_dpr": [
        "DPRContextEncoderTokenizer",
        "DPRQuestionEncoderTokenizer",
        "DPRReaderOutput",
        "DPRReaderTokenizer",
    ],
}


try:
       if not is_tokenizers_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__ =       [
           "DPRContextEncoderTokenizerFast",
           "DPRQuestionEncoderTokenizerFast",
           "DPRReaderTokenizerFast",
       ]

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__ =       [
           "DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST",
           "DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST",
           "DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST",
           "DPRContextEncoder",
           "DPRPretrainedContextEncoder",
           "DPRPreTrainedModel",
           "DPRPretrainedQuestionEncoder",
           "DPRPretrainedReader",
           "DPRQuestionEncoder",
           "DPRReader",
       ]

try:
       if not is_tf_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       SCREAMING_SNAKE_CASE__ =       [
           "TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST",
           "TFDPRContextEncoder",
           "TFDPRPretrainedContextEncoder",
           "TFDPRPretrainedQuestionEncoder",
           "TFDPRPretrainedReader",
           "TFDPRQuestionEncoder",
           "TFDPRReader",
       ]


if TYPE_CHECKING:
       from .configuration_dpr import DPR_PRETRAINED_CONFIG_ARCHIVE_MAP, DPRConfig
       from .tokenization_dpr import (
           DPRContextEncoderTokenizer,
           DPRQuestionEncoderTokenizer,
           DPRReaderOutput,
           DPRReaderTokenizer,
       )

       try:
              if not is_tokenizers_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .tokenization_dpr_fast import (
                  DPRContextEncoderTokenizerFast,
                  DPRQuestionEncoderTokenizerFast,
                  DPRReaderTokenizerFast,
              )

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_dpr import (
                  DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  DPRContextEncoder,
                  DPRPretrainedContextEncoder,
                  DPRPreTrainedModel,
                  DPRPretrainedQuestionEncoder,
                  DPRPretrainedReader,
                  DPRQuestionEncoder,
                  DPRReader,
              )

       try:
              if not is_tf_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_tf_dpr import (
                  TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
                  TFDPRContextEncoder,
                  TFDPRPretrainedContextEncoder,
                  TFDPRPretrainedQuestionEncoder,
                  TFDPRPretrainedReader,
                  TFDPRQuestionEncoder,
                  TFDPRReader,
              )

else:
       import sys

       SCREAMING_SNAKE_CASE__ =       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)













"""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)))






