




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




def     __SCREAMING_SNAKE_CASE      (     lowercase_					)				->		Any:

    """simple docstring"""


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

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

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

    def lower(lowercase_					):
        return text.lower()

    return white_space_fix(remove_articles(remove_punc(lower(lowercase_					)					)					)					)




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		Dict:

    """simple docstring"""


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




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		Optional[int]:

    """simple docstring"""


    return normalize_answer(lowercase_					) == normalize_answer(lowercase_					)




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_    ,   lowercase_					)				->		Dict:

    """simple docstring"""


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




def     __SCREAMING_SNAKE_CASE      (     lowercase_    ,   lowercase_					)				->		Dict:

    """simple docstring"""


    __UpperCamelCase     						=  __UpperCamelCase     						=  __UpperCamelCase     						=  0
    __UpperCamelCase     						=  []
    for passage in dataset:
        for qa in passage["qas"]:
            total += 1
            if qa["id"] not in predictions:
                __UpperCamelCase     						=  F"Unanswered question {qa['id']} will receive score 0."
                print(lowercase_    ,   file=sys.stderr					)
                continue

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

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

            fa += metric_max_over_ground_truths(lowercase_    ,   lowercase_    ,   lowercase_					)

    __UpperCamelCase     						=  exact_match / total
    __UpperCamelCase     						=  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)








'''simple docstring'''



import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
     import sqlitea

     import sqlalchemy


UpperCamelCase__					:						Tuple											=     datasets.utils.logging.get_logger(__name__)

@dataclass
class       _a (datasets.BuilderConfig):





       """simple docstring"""




       SCREAMING_SNAKE_CASE					=     None
       SCREAMING_SNAKE_CASE					=     None
       SCREAMING_SNAKE_CASE					=     None
       SCREAMING_SNAKE_CASE					=     True
       SCREAMING_SNAKE_CASE					=     None
       SCREAMING_SNAKE_CASE					=     None
       SCREAMING_SNAKE_CASE					=     None
       SCREAMING_SNAKE_CASE					=     1_00_00
       SCREAMING_SNAKE_CASE					=     None






       def 				UpperCamelCase (							self					)			->	str:
          if self.sql is None:
             raise ValueError("""sql must be specified"""					)
          if self.con is None:
             raise ValueError("""con must be specified"""					)






       def 				UpperCamelCase (							self				,							A__				,							A__ = None				,							)			->	str:
          _SCREAMING_SNAKE_CASE          = config_kwargs.copy()
          # We need to stringify the Selectable object to make its hash deterministic

          # The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
          _SCREAMING_SNAKE_CASE          = config_kwargs["""sql"""]
          if not isinstance(A__				,							A__					):
             if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
                import sqlalchemy

                if isinstance(A__				,							sqlalchemy.sql.Selectable					):
                   _SCREAMING_SNAKE_CASE          = sqlalchemy.create_engine(config_kwargs["""con"""].split("""://"""					)[0] + """://"""					)
                   _SCREAMING_SNAKE_CASE          = str(sql.compile(dialect=engine.dialect					)					)
                   _SCREAMING_SNAKE_CASE          = sql_str
                else:
                   raise TypeError(
                       F"Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(A__					)}: {sql}"					)
             else:
                raise TypeError(
                    F"Supported types for 'sql' are string and sqlalchemy.sql.Selectable but got {type(A__					)}: {sql}"					)
          _SCREAMING_SNAKE_CASE          = config_kwargs["""con"""]
          if not isinstance(A__				,							A__					):
             _SCREAMING_SNAKE_CASE          = id(A__					)
             logger.info(
                 F"SQL connection 'con' of type {type(A__					)} couldn't be hashed properly. To enable hashing, specify 'con' as URI string instead."					)

          return super().create_config_id(A__				,							custom_features=A__					)






       @property
       def 				UpperCamelCase (							self					)			->	Any:
          _SCREAMING_SNAKE_CASE          = {
              """index_col""": self.index_col,
              """columns""": self.columns,
              """params""": self.params,
              """coerce_float""": self.coerce_float,
              """parse_dates""": self.parse_dates,
          }
          return pd_read_sql_kwargs






class       _a (datasets.ArrowBasedBuilder):





       """simple docstring"""




       SCREAMING_SNAKE_CASE					=     SqlConfig






       def 				UpperCamelCase (							self					)			->	Optional[Any]:
          return datasets.DatasetInfo(features=self.config.features					)






       def 				UpperCamelCase (							self				,							A__					)			->	Dict:
          return [datasets.SplitGenerator(name=datasets.Split.TRAIN				,							gen_kwargs={}					)]






       def 				UpperCamelCase (							self				,							A__					)			->	pa.Table:
          if self.config.features is not None:
             _SCREAMING_SNAKE_CASE          = self.config.features.arrow_schema
             if all(not require_storage_cast(A__					) for feature in self.config.features.values()					):
                # cheaper cast
                _SCREAMING_SNAKE_CASE          = pa.Table.from_arrays([pa_table[field.name] for field in schema]				,							schema=A__					)
             else:
                # more expensive cast; allows str <-> int/float or str to Audio for example
                _SCREAMING_SNAKE_CASE          = table_cast(A__				,							A__					)
          return pa_table






       def 				UpperCamelCase (							self					)			->	Optional[int]:
          _SCREAMING_SNAKE_CASE          = self.config.chunksize
          _SCREAMING_SNAKE_CASE          = pd.read_sql(
              self.config.sql				,							self.config.con				,							chunksize=A__				,							**self.config.pd_read_sql_kwargs					)
          _SCREAMING_SNAKE_CASE          = [sql_reader] if chunksize is None else sql_reader
          for chunk_idx, df in enumerate(A__					):
             _SCREAMING_SNAKE_CASE          = pa.Table.from_pandas(A__					)
             yield chunk_idx, self._cast_table(A__					)






