






"""simple docstring"""





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




def 						_A						(						UpperCamelCase_  : str)			->		Optional[int]:
   '''simple docstring'''


   def remove_articles(UpperCamelCase_  : Optional[int]):
      return re.sub(r"\b(a|an|the)\b",    " ",    UpperCamelCase_)

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

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

   def lower(UpperCamelCase_  : List[str]):
      return text.lower()

   return white_space_fix(remove_articles(remove_punc(lower(UpperCamelCase_))))




def 						_A						(						UpperCamelCase_  : int,    UpperCamelCase_  : Optional[Any])			->		Optional[Any]:
   '''simple docstring'''


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




def 						_A						(						UpperCamelCase_  : List[str],    UpperCamelCase_  : Any)			->		Any:
   '''simple docstring'''


   return normalize_answer(UpperCamelCase_) == normalize_answer(UpperCamelCase_)




def 						_A						(						UpperCamelCase_  : List[str],    UpperCamelCase_  : Optional[int],    UpperCamelCase_  : List[Any])			->		str:
   '''simple docstring'''


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




def 						_A						(						UpperCamelCase_  : Optional[Any],    UpperCamelCase_  : List[str])			->		List[Any]:
   '''simple docstring'''


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

         __lowercase			=		[x["text"] for x in qa["answers"]]
         __lowercase			=		predictions[qa["id"]]

         __lowercase			=		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_)

   __lowercase			=		exact_match / total
   __lowercase			=		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 textwrap

import pyarrow as pa
import pytest

from datasets import Features, Value
from datasets.packaged_modules.json.json import Json




@pytest.fixture
def 						_A						(						UpperCamelCase_  : str)			->		Tuple:
   '''simple docstring'''


   __lowercase			=		tmp_path / "file.jsonl"
   __lowercase			=		textwrap.dedent(
       "\\n        {\"col_1\": -1}\n        {\"col_1\": 1, \"col_2\": 2}\n        {\"col_1\": 10, \"col_2\": 20}\n        ")
   with open(UpperCamelCase_,    "w") as f:
      f.write(UpperCamelCase_)
   return str(UpperCamelCase_)




@pytest.fixture
def 						_A						(						UpperCamelCase_  : str)			->		Any:
   '''simple docstring'''


   __lowercase			=		tmp_path / "file_utf16_encoded.jsonl"
   __lowercase			=		textwrap.dedent(
       "\\n        {\"col_1\": -1}\n        {\"col_1\": 1, \"col_2\": 2}\n        {\"col_1\": 10, \"col_2\": 20}\n        ")
   with open(UpperCamelCase_,    "w",    encoding="utf-16") as f:
      f.write(UpperCamelCase_)
   return str(UpperCamelCase_)




@pytest.fixture
def 						_A						(						UpperCamelCase_  : Optional[int])			->		Optional[Any]:
   '''simple docstring'''


   __lowercase			=		tmp_path / "file_with_list_of_dicts.json"
   __lowercase			=		textwrap.dedent(
       "\\n        [\n            {\"col_1\": -1},\n            {\"col_1\": 1, \"col_2\": 2},\n            {\"col_1\": 10, \"col_2\": 20}\n        ]\n        ")
   with open(UpperCamelCase_,    "w") as f:
      f.write(UpperCamelCase_)
   return str(UpperCamelCase_)




@pytest.fixture
def 						_A						(						UpperCamelCase_  : str)			->		int:
   '''simple docstring'''


   __lowercase			=		tmp_path / "file_with_list_of_dicts_field.json"
   __lowercase			=		textwrap.dedent(
       "\\n        {\n            \"field1\": 1,\n            \"field2\": \"aabb\",\n            \"field3\": [\n                {\"col_1\": -1},\n                {\"col_1\": 1, \"col_2\": 2},\n                {\"col_1\": 10, \"col_2\": 20}\n            ]\n        }\n        ")
   with open(UpperCamelCase_,    "w") as f:
      f.write(UpperCamelCase_)
   return str(UpperCamelCase_)




@pytest.mark.parametrize(
    "file_fixture, config_kwargs",    [
        ("jsonl_file", {}),
        ("jsonl_file_utf16_encoded", {"encoding": "utf-16"}),
        ("json_file_with_list_of_dicts", {}),
        ("json_file_with_list_of_dicts_field", {"field": "field3"}),
    ],    )
def 						_A						(						UpperCamelCase_  : Any,    UpperCamelCase_  : List[Any],    UpperCamelCase_  : Optional[Any])			->		List[Any]:
   '''simple docstring'''


   __lowercase			=		Json(**UpperCamelCase_)
   __lowercase			=		json._generate_tables([[request.getfixturevalue(UpperCamelCase_)]])
   __lowercase			=		pa.concat_tables([table for _, table in generator])
   assert pa_table.to_pydict() == {"col_1": [-1, 1, 10], "col_2": [None, 2, 20]}




@pytest.mark.parametrize(
    "file_fixture, config_kwargs",    [
        (
            "jsonl_file",
            {"features": Features({"col_1": Value("int64"), "col_2": Value("int64"), "missing_col": Value("string")})},
        ),
        (
            "json_file_with_list_of_dicts",
            {"features": Features({"col_1": Value("int64"), "col_2": Value("int64"), "missing_col": Value("string")})},
        ),
        (
            "json_file_with_list_of_dicts_field",
            {
                "field": "field3",
                "features": Features(
                    {"col_1": Value("int64"), "col_2": Value("int64"), "missing_col": Value("string")}),
            },
        ),
    ],    )
def 						_A						(						UpperCamelCase_  : Optional[int],    UpperCamelCase_  : int,    UpperCamelCase_  : str)			->		Optional[int]:
   '''simple docstring'''


   __lowercase			=		Json(**UpperCamelCase_)
   __lowercase			=		json._generate_tables([[request.getfixturevalue(UpperCamelCase_)]])
   __lowercase			=		pa.concat_tables([table for _, table in generator])
   assert pa_table.to_pydict() == {"col_1": [-1, 1, 10], "col_2": [None, 2, 20], "missing_col": [None, None, None]}




