

'''simple docstring'''




import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


_a			:	Tuple								=  get_logger(__name__)


if TYPE_CHECKING:
		from .builder import DatasetBuilder


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Tuple    ,				SCREAMING_SNAKE_CASE :				Optional[Union[str, bool]] = None  ):
		UpperCAmelCase  							=		importlib.import_module(SCREAMING_SNAKE_CASE  )

		# TODO(QL): always update the module to add subsequent new authentication
		if hasattr(SCREAMING_SNAKE_CASE    ,				'_patched_for_streaming'  ) and module._patched_for_streaming:
				return

		def wrap_auth(SCREAMING_SNAKE_CASE :				List[Any]  ):
				@wraps(SCREAMING_SNAKE_CASE  )
				def wrapper(*SCREAMING_SNAKE_CASE :				Any    ,				**SCREAMING_SNAKE_CASE :				Tuple  ):
						return function(*SCREAMING_SNAKE_CASE    ,				use_auth_token=SCREAMING_SNAKE_CASE    ,				**SCREAMING_SNAKE_CASE  )

				UpperCAmelCase  							=		'wrap_auth'
				return wrapper

		# open files in a streaming fashion
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'open'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.listdir'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.walk'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'glob.glob'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		# allow to navigate in remote zip files
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.join'    ,				SCREAMING_SNAKE_CASE  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.dirname'    ,				SCREAMING_SNAKE_CASE  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.basename'    ,				SCREAMING_SNAKE_CASE  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.relpath'    ,				SCREAMING_SNAKE_CASE  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.split'    ,				SCREAMING_SNAKE_CASE  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.splitext'    ,				SCREAMING_SNAKE_CASE  ).start()
		# allow checks on paths
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.exists'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.isdir'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.isfile'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'os.path.getsize'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'pathlib.Path'    ,				SCREAMING_SNAKE_CASE  ).start()
		# file readers
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'gzip.open'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'numpy.load'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'pandas.read_csv'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )    ,				attrs=['__version__']  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'pandas.read_excel'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )    ,				attrs=['__version__']  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'scipy.io.loadmat'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )    ,				attrs=['__version__']  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'xml.etree.ElementTree.parse'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		patch_submodule(SCREAMING_SNAKE_CASE    ,				'xml.dom.minidom.parse'    ,				wrap_auth(SCREAMING_SNAKE_CASE  )  ).start()
		UpperCAmelCase  							=		True



def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				"DatasetBuilder"  ):
		extend_module_for_streaming(builder.__module__    ,				use_auth_token=builder.use_auth_token  )
		# if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
		if not builder.__module__.startswith('datasets.'  ):  # check that it's not a packaged builder like csv
				for imports in get_imports(inspect.getfile(builder.__class__  )  ):
						if imports[0] == "internal":
								UpperCAmelCase  							=		imports[1]
								UpperCAmelCase  							=		'.'.join(builder.__module__.split('.'  )[:-1] + [internal_import_name]  )
								extend_module_for_streaming(SCREAMING_SNAKE_CASE    ,				use_auth_token=builder.use_auth_token  )

				# builders can inherit from other builders that might use streaming functionality
				# (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
				# but these parents builders are not patched automatically as they are not instantiated, so we patch them here
		from .builder import DatasetBuilder

		UpperCAmelCase  							=		[
		    cls.__module__
		    for cls in type(SCREAMING_SNAKE_CASE  ).__mro__[1:]  # make sure it's not the same module we've already patched
		    if issubclass(cls    ,				SCREAMING_SNAKE_CASE  ) and cls.__module__ != DatasetBuilder.__module__
		]  # check it's not a standard builder from datasets.builder
		for module in parent_builder_modules:
				extend_module_for_streaming(SCREAMING_SNAKE_CASE    ,				use_auth_token=builder.use_auth_token  )





'''simple docstring'''




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


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				List[str]  ):

  def remove_articles(SCREAMING_SNAKE_CASE :				Union[str, Any]  ):
    return re.sub(R'\b(a|an|the)\b'    ,				' '    ,				SCREAMING_SNAKE_CASE  )

  def white_space_fix(SCREAMING_SNAKE_CASE :				Union[str, Any]  ):
    return " ".join(text.split()  )

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

  def lower(SCREAMING_SNAKE_CASE :				Any  ):
    return text.lower()

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


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Tuple    ,				SCREAMING_SNAKE_CASE :				Dict  ):
  UpperCAmelCase  							=		normalize_answer(SCREAMING_SNAKE_CASE  ).split()
  UpperCAmelCase  							=		normalize_answer(SCREAMING_SNAKE_CASE  ).split()
  UpperCAmelCase  							=		Counter(SCREAMING_SNAKE_CASE  ) & Counter(SCREAMING_SNAKE_CASE  )
  UpperCAmelCase  							=		sum(common.values()  )
  if num_same == 0:
    return 0
  UpperCAmelCase  							=		1.0 * num_same / len(SCREAMING_SNAKE_CASE  )
  UpperCAmelCase  							=		1.0 * num_same / len(SCREAMING_SNAKE_CASE  )
  UpperCAmelCase  							=		(2 * precision * recall) / (precision + recall)
  return fa


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				str    ,				SCREAMING_SNAKE_CASE :				int  ):
  return normalize_answer(SCREAMING_SNAKE_CASE  ) == normalize_answer(SCREAMING_SNAKE_CASE  )


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Dict    ,				SCREAMING_SNAKE_CASE :				Tuple    ,				SCREAMING_SNAKE_CASE :				Dict  ):
  UpperCAmelCase  							=		[]
  for ground_truth in ground_truths:
    UpperCAmelCase  							=		metric_fn(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  )
    scores_for_ground_truths.append(SCREAMING_SNAKE_CASE  )
  return max(SCREAMING_SNAKE_CASE  )


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Optional[Any]    ,				SCREAMING_SNAKE_CASE :				Optional[int]  ):
  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(SCREAMING_SNAKE_CASE    ,				file=sys.stderr  )
          continue
        UpperCAmelCase  							=		[x['text'] for x in qa['answers']]
        UpperCAmelCase  							=		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  )

  UpperCAmelCase  							=		100.0 * exact_match / total
  UpperCAmelCase  							=		100.0 * fa / total

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


if __name__ == "__main__":
  _a			:	Tuple								=  '1.1'
  _a			:	int								=  argparse.ArgumentParser(description='Evaluation for SQuAD ' + expected_version)
  parser.add_argument('dataset_file', help='Dataset file')
  parser.add_argument('prediction_file', help='Prediction File')
  _a			:	List[Any]								=  parser.parse_args()
  with open(args.dataset_file) as dataset_file:
    _a			:	Optional[int]								=  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,
      )
    _a			:	Optional[Any]								=  dataset_json['data']
  with open(args.prediction_file) as prediction_file:
    _a			:	Optional[Any]								=  json.load(prediction_file)
  print(json.dumps(evaluate(dataset, predictions)))


