



from typing import TYPE_CHECKING

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


a_								=   {
    """configuration_mpnet""": ["""MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MPNetConfig"""],
    """tokenization_mpnet""": ["""MPNetTokenizer"""],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a_								=   ["""MPNetTokenizerFast"""]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a_								=   [
      """MPNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """MPNetForMaskedLM""",
      """MPNetForMultipleChoice""",
      """MPNetForQuestionAnswering""",
      """MPNetForSequenceClassification""",
      """MPNetForTokenClassification""",
      """MPNetLayer""",
      """MPNetModel""",
      """MPNetPreTrainedModel""",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  a_								=   [
      """TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST""",
      """TFMPNetEmbeddings""",
      """TFMPNetForMaskedLM""",
      """TFMPNetForMultipleChoice""",
      """TFMPNetForQuestionAnswering""",
      """TFMPNetForSequenceClassification""",
      """TFMPNetForTokenClassification""",
      """TFMPNetMainLayer""",
      """TFMPNetModel""",
      """TFMPNetPreTrainedModel""",
  ]


if TYPE_CHECKING:
  from .configuration_mpnet import MPNET_PRETRAINED_CONFIG_ARCHIVE_MAP, MPNetConfig
  from .tokenization_mpnet import MPNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_mpnet_fast import MPNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_mpnet import (
        MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        MPNetForMaskedLM,
        MPNetForMultipleChoice,
        MPNetForQuestionAnswering,
        MPNetForSequenceClassification,
        MPNetForTokenClassification,
        MPNetLayer,
        MPNetModel,
        MPNetPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_mpnet import (
        TF_MPNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFMPNetEmbeddings,
        TFMPNetForMaskedLM,
        TFMPNetForMultipleChoice,
        TFMPNetForQuestionAnswering,
        TFMPNetForSequenceClassification,
        TFMPNetForTokenClassification,
        TFMPNetMainLayer,
        TFMPNetModel,
        TFMPNetPreTrainedModel,
    )

else:
  import sys

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











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





def 						a__     (						_UpperCamelCase     :	Tuple							):

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

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

   def remove_punc(_UpperCamelCase     :	int							):
      __lowerCamelCase  							=      set(string.punctuation							)
      return "".join(ch for ch in text if ch not in exclude							)

   def lower(_UpperCamelCase     :	str							):
      return text.lower()

   return white_space_fix(remove_articles(remove_punc(lower(_UpperCamelCase							)							)							)							)





def 						a__     (						_UpperCamelCase     :	Union[str, Any]	,_UpperCamelCase     :	List[str]							):
   __lowerCamelCase  							=      normalize_answer(_UpperCamelCase							).split()
   __lowerCamelCase  							=      normalize_answer(_UpperCamelCase							).split()
   __lowerCamelCase  							=      Counter(_UpperCamelCase							) & Counter(_UpperCamelCase							)
   __lowerCamelCase  							=      sum(common.values()							)
   if num_same == 0:
      return 0
   __lowerCamelCase  							=      1.0 * num_same / len(_UpperCamelCase							)
   __lowerCamelCase  							=      1.0 * num_same / len(_UpperCamelCase							)
   __lowerCamelCase  							=      (2 * precision * recall) / (precision + recall)
   return fa





def 						a__     (						_UpperCamelCase     :	Optional[Any]	,_UpperCamelCase     :	str							):
   return normalize_answer(_UpperCamelCase							) == normalize_answer(_UpperCamelCase							)





def 						a__     (						_UpperCamelCase     :	int	,_UpperCamelCase     :	Dict	,_UpperCamelCase     :	Dict							):
   __lowerCamelCase  							=      []
   for ground_truth in ground_truths:
      __lowerCamelCase  							=      metric_fn(_UpperCamelCase	,_UpperCamelCase							)
      scores_for_ground_truths.append(_UpperCamelCase							)
   return max(_UpperCamelCase							)





def 						a__     (						_UpperCamelCase     :	Optional[int]	,_UpperCamelCase     :	Optional[Any]							):
   __lowerCamelCase  							=      __lowerCamelCase  							=      __lowerCamelCase  							=      0
   __lowerCamelCase  							=      []
   for passage in dataset:
      for qa in passage["qas"]:
         total += 1
         if qa["id"] not in predictions:
            __lowerCamelCase  							=      F"""Unanswered question {qa["id"]} will receive score 0."""
            print(_UpperCamelCase	,file=sys.stderr							)
            continue

         __lowerCamelCase  							=      [x['''text'''] for x in qa['''answers''']]
         __lowerCamelCase  							=      predictions[qa['''id''']]

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

         fa += metric_max_over_ground_truths(_UpperCamelCase	,_UpperCamelCase	,_UpperCamelCase							)

   __lowerCamelCase  							=      exact_match / total
   __lowerCamelCase  							=      fa / total

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


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

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

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

  a_						,  a_								=   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)






