





import json
import os
import pickle
import subprocess
from hashlib import mda
from pathlib import Path
from tempfile import gettempdir
from textwrap import dedent
from types import CodeType, FunctionType
from unittest import TestCase
from unittest.mock import patch

import pytest
from multiprocess import Pool
from packaging import version

import datasets
from datasets.fingerprint import Hasher, fingerprint_transform
from datasets.table import InMemoryTable

from .utils import (
    require_regex,
    require_spacy,
    require_spacy_model,
    require_tiktoken,
    require_torch,
    require_transformers,
)





class A_      :




   def __init__(self			:int					,     _UpperCamelCase			:Tuple				)->      List[str]:
         __A      =       foo






   def __call__(self			:Optional[Any]				)->      List[str]:
         return self.foo





class A_      (    datasets.Dataset     ):




   @fingerprint_transform(inplace=_UpperCamelCase				)
   def _lowerCAmelCase   (self			:Tuple					,     _UpperCamelCase			:Any					,     *_UpperCamelCase			:Dict					,     **_UpperCamelCase			:List[str]				)->      int:
         return DatasetChild(self.data					,     fingerprint=_UpperCamelCase				)






   @fingerprint_transform(inplace=_UpperCamelCase				)
   def _lowerCAmelCase   (self			:List[Any]					,     _UpperCamelCase			:Tuple					,     *_UpperCamelCase			:Optional[Any]					,     **_UpperCamelCase			:List[str]				)->      Optional[Any]:
         return DatasetChild(self.data					,     fingerprint=_UpperCamelCase				)





class A_      :




   def __init__(self			:Any					,     _UpperCamelCase			:List[str]				)->      Optional[Any]:
         __A      =       callable




   def __call__(self			:Tuple					,     *_UpperCamelCase			:Optional[int]					,     **_UpperCamelCase			:Union[str, Any]				)->      List[Any]:
         if self.callable is not None:
               return self.callable(*_UpperCamelCase					,     **_UpperCamelCase				)






   def __getstate__(self			:List[str]				)->      List[Any]:
         raise pickle.PicklingError()





class A_      (    _lowerCamelCase     ):




   @require_transformers
   @pytest.mark.integration
   def _lowerCAmelCase   (self			:Optional[int]				)->      List[Any]:
         from transformers import AutoTokenizer

         def encode(_UpperCamelCase			:str				):
               return tokenizer(_UpperCamelCase				)

         # TODO: add hash consistency tests across sessions
         __A      =       AutoTokenizer.from_pretrained('''bert-base-uncased'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(lambda _UpperCamelCase				: tokenizer(_UpperCamelCase				)				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       AutoTokenizer.from_pretrained('''bert-base-cased'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(lambda _UpperCamelCase				: tokenizer(_UpperCamelCase				)				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       AutoTokenizer.from_pretrained('''bert-base-uncased'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(lambda _UpperCamelCase				: tokenizer(_UpperCamelCase				)				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   @require_transformers
   @pytest.mark.integration
   def _lowerCAmelCase   (self			:Optional[Any]				)->      int:
         from transformers import AutoTokenizer

         __A      =       AutoTokenizer.from_pretrained('''gpt2'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         tokenizer('''Hello world !'''				)  # call once to change the tokenizer's cache
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)






   @require_regex
   def _lowerCAmelCase   (self			:Any				)->      Any:
         import regex

         __A      =       regex.Regex('''foo'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       regex.Regex('''bar'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       regex.Regex('''foo'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)





class A_      (    _lowerCamelCase     ):




   def _lowerCAmelCase   (self			:Optional[int]				)->      Tuple:
         def func():
               return foo

         __A      =       [0]
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       [1]
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       [0]
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:int				)->      Dict:
         def func():
               pass

         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()

         def func():
               pass

         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:str				)->      Optional[Any]:
         __A      =       mda(datasets.utils.py_utils.dumps(Foo([0]				)				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(Foo([1]				)				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(Foo([0]				)				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:List[str]				)->      List[str]:
         __A      =       mda(datasets.utils.py_utils.dumps(Foo([0]				).__call__				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(Foo([1]				).__call__				)				).hexdigest()
         __A      =       mda(datasets.utils.py_utils.dumps(Foo([0]				).__call__				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:Optional[int]				)->      List[str]:
         __A      =       (
             '''co_argcount''',
             '''co_kwonlyargcount''',
             '''co_nlocals''',
             '''co_stacksize''',
             '''co_flags''',
             '''co_code''',
             '''co_consts''',
             '''co_names''',
             '''co_varnames''',
             '''co_filename''',
             '''co_name''',
             '''co_firstlineno''',
             '''co_lnotab''',
             '''co_freevars''',
             '''co_cellvars''',
         )

         def _create_code_pyaa(*_UpperCamelCase			:Dict				):
               if hasattr(args[-3]					,     '''encode'''				):
                     __A      =       list(_UpperCamelCase				)
                     __A      =       args[-3].encode()  # co_lnotab
                     __A      =       args[-10].encode()  # co_code
               if hasattr(_UpperCamelCase					,     '''co_posonlyargcount'''				):
                     if len(_UpperCamelCase				) == 16:
                           return CodeType(*_UpperCamelCase				)
                     elif len(_UpperCamelCase				) == 15:
                           return CodeType(args[0]					,     0					,     *args[1:]				)
                     return CodeType(args[0]					,     0					,     0					,     *args[1:]				)
               elif hasattr(_UpperCamelCase					,     '''co_kwonlyargcount'''				):
                     if len(_UpperCamelCase				) == 16:
                           return CodeType(args[0]					,     *args[2:]				)
                     elif len(_UpperCamelCase				) == 15:
                           return CodeType(*_UpperCamelCase				)
                     return CodeType(args[0]					,     0					,     *args[1:]				)
               if len(_UpperCamelCase				) == 16:
                     return CodeType(args[0]					,     *args[3:]				)
               elif len(_UpperCamelCase				) == 15:
                     return CodeType(args[0]					,     *args[2:]				)
               return CodeType(*_UpperCamelCase				)

         def create_ipython_func(_UpperCamelCase			:List[Any]					,     _UpperCamelCase			:Optional[Any]				):
               def func():
                     return returned_obj

               __A      =       func.__code__
               # Use _create_code from dill in order to make it work for different python versions
               if datasets.config.PY_VERSION < version.parse('''3.8'''				):
                     __A      =       _create_code_pyaa(
                         *[getattr(_UpperCamelCase					,     _UpperCamelCase				) if k != '''co_filename''' else co_filename for k in code_args_pyaa]				)
               else:
                     __A      =       code.replace(co_filename=_UpperCamelCase				)
               return FunctionType(_UpperCamelCase					,     func.__globals__					,     func.__name__					,     func.__defaults__					,     func.__closure__				)

         __A     ,		__A      =       '''<ipython-input-2-e0383a102aae>''', [0]
         __A      =       mda(datasets.utils.py_utils.dumps(create_ipython_func(_UpperCamelCase					,     _UpperCamelCase				)				)				).hexdigest()
         __A     ,		__A      =       '''<ipython-input-2-e0383a102aae>''', [1]
         __A      =       mda(datasets.utils.py_utils.dumps(create_ipython_func(_UpperCamelCase					,     _UpperCamelCase				)				)				).hexdigest()
         __A     ,		__A      =       '''<ipython-input-5-713f6613acf3>''', [0]
         __A      =       mda(datasets.utils.py_utils.dumps(create_ipython_func(_UpperCamelCase					,     _UpperCamelCase				)				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)

         __A     ,		__A      =       os.path.join(gettempdir()					,     '''ipykernel_12345'''					,     '''321456789.py'''				), [0]
         __A      =       mda(datasets.utils.py_utils.dumps(create_ipython_func(_UpperCamelCase					,     _UpperCamelCase				)				)				).hexdigest()
         __A     ,		__A      =       os.path.join(gettempdir()					,     '''ipykernel_12345'''					,     '''321456789.py'''				), [1]
         __A      =       mda(datasets.utils.py_utils.dumps(create_ipython_func(_UpperCamelCase					,     _UpperCamelCase				)				)				).hexdigest()
         __A     ,		__A      =       os.path.join(gettempdir()					,     '''ipykernel_12345'''					,     '''654123987.py'''				), [0]
         __A      =       mda(datasets.utils.py_utils.dumps(create_ipython_func(_UpperCamelCase					,     _UpperCamelCase				)				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)






   def _lowerCAmelCase   (self			:List[Any]				)->      Union[str, Any]:
         __A     ,		__A      =       [0], [1]

         def func():
               return foo, bar

         __A      =       '''__main__'''

         def globalvars_mocka_side_effect(_UpperCamelCase			:Optional[Any]					,     *_UpperCamelCase			:Dict					,     **_UpperCamelCase			:List[str]				):
               return {"foo": foo, "bar": bar}

         def globalvars_mocka_side_effect(_UpperCamelCase			:Optional[Any]					,     *_UpperCamelCase			:str					,     **_UpperCamelCase			:Any				):
               return {"bar": bar, "foo": foo}

         with patch('''dill.detect.globalvars'''					,     side_effect=_UpperCamelCase				) as globalvars_mocka:
               __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
               self.assertGreater(globalvars_mocka.call_count					,     0				)
         with patch('''dill.detect.globalvars'''					,     side_effect=_UpperCamelCase				) as globalvars_mocka:
               __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
               self.assertGreater(globalvars_mocka.call_count					,     0				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)





class A_      (    _lowerCamelCase     ):




   def _lowerCAmelCase   (self			:str				)->      int:
         __A      =       Hasher.hash('''hello'''				)
         __A      =       Hasher.hash('''hello'''				)
         __A      =       Hasher.hash('''there'''				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:str				)->      Union[str, Any]:
         __A      =       Hasher.hash(Foo('''hello'''				)				)
         __A      =       Hasher.hash(Foo('''hello'''				)				)
         __A      =       Hasher.hash(Foo('''there'''				)				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:List[Any]				)->      List[str]:
         __A      =       Hasher()
         for x in ["hello", Foo('''hello'''				)]:
               hasher.update(_UpperCamelCase				)
         __A      =       hasher.hexdigest()
         __A      =       Hasher()
         for x in ["hello", Foo('''hello'''				)]:
               hasher.update(_UpperCamelCase				)
         __A      =       hasher.hexdigest()
         __A      =       Hasher()
         for x in ["there", Foo('''there'''				)]:
               hasher.update(_UpperCamelCase				)
         __A      =       hasher.hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   def _lowerCAmelCase   (self			:Optional[Any]				)->      Tuple:
         with self.assertRaises(pickle.PicklingError				):
               Hasher.hash(UnpicklableCallable(Foo('''hello'''				)				)				)




   def _lowerCAmelCase   (self			:Any				)->      Optional[int]:
         __A      =       '''abc'''
         __A      =       [string, string]  # two strings have the same ids
         __A      =       [string, string]
         __A      =       json.loads(f"""[\"{string}\", \"{string}\"]"""				)  # two strings have different ids
         self.assertIs(obja[0]					,     _UpperCamelCase				)
         self.assertIs(obja[0]					,     obja[1]				)
         self.assertIs(obja[0]					,     _UpperCamelCase				)
         self.assertIs(obja[0]					,     obja[1]				)
         self.assertIsNot(obja[0]					,     _UpperCamelCase				)
         self.assertIsNot(obja[0]					,     obja[1]				)
         __A      =       Hasher.hash(_UpperCamelCase				)
         __A      =       Hasher.hash(_UpperCamelCase				)
         __A      =       Hasher.hash(_UpperCamelCase				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)




   @require_tiktoken
   def _lowerCAmelCase   (self			:Optional[Any]				)->      Any:
         import tiktoken

         __A      =       tiktoken.get_encoding('''gpt2'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       tiktoken.get_encoding('''r50k_base'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       tiktoken.get_encoding('''gpt2'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)




   @require_torch
   def _lowerCAmelCase   (self			:Union[str, Any]				)->      str:
         import torch

         __A      =       torch.tensor([1.0]				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       torch.tensor([2.0]				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       torch.tensor([1.0]				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)






   @require_spacy
   @require_spacy_model('''en_core_web_sm'''				)
   @require_spacy_model('''fr_core_news_sm'''				)
   @pytest.mark.integration
   def _lowerCAmelCase   (self			:Any				)->      str:
         import spacy

         __A      =       spacy.load('''en_core_web_sm'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       spacy.load('''fr_core_news_sm'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         __A      =       spacy.load('''en_core_web_sm'''				)
         __A      =       mda(datasets.utils.py_utils.dumps(_UpperCamelCase				)				).hexdigest()
         self.assertEqual(_UpperCamelCase					,     _UpperCamelCase				)
         self.assertNotEqual(_UpperCamelCase					,     _UpperCamelCase				)





@pytest.mark.integration
def _a       (			lowerCamelCase:     Path				)			->		Any:







      '''simple docstring'''






      __A      =       tmp_path / '''dir1'''
      __A      =       tmp_path / '''dir2'''
      dira.mkdir()
      dira.mkdir()
      __A      =       '''script.py'''
      __A      =       dedent(
          '''
    from datasets.fingerprint import Hasher
    def foo():
        pass
    print(Hasher.hash(foo))
    '''				)
      __A      =       dira / script_filename
      __A      =       dira / script_filename
      with script_patha.open('''w'''				) as f:
            f.write(lowerCamelCase				)
      with script_patha.open('''w'''				) as f:
            f.write(lowerCamelCase				)
      __A      =       subprocess.check_output(['''python''', str(lowerCamelCase				)]				)
      __A      =       subprocess.check_output(['''python''', str(lowerCamelCase				)]				)
      assert fingerprinta == fingerprinta



def _a       (			)			->		Dict:







      '''simple docstring'''






      __A      =       {'''a''': [0, 1, 2]}
      __A      =       DatasetChild(InMemoryTable.from_pydict(lowerCamelCase				)				)
      __A      =       dataset.funca()._fingerprint
      assert expected_fingerprint == dataset.funca()._fingerprint
      assert expected_fingerprint != dataset.funca()._fingerprint

      with Pool(2				) as p:
            assert expected_fingerprint == p.apply_async(dataset.funca				).get()._fingerprint
            assert expected_fingerprint != p.apply_async(dataset.funca				).get()._fingerprint



def _a       (			)			->		List[Any]:







      '''simple docstring'''






      __A      =       {'''a''': [0, 1, 2]}





      class A_      (    datasets.Dataset     ):




         @fingerprint_transform(inplace=_UpperCamelCase				)
         def _lowerCAmelCase   (self			:Tuple					,     _UpperCamelCase			:int				)->      int:
               return DummyDatasetChild(self.data					,     fingerprint=_UpperCamelCase				)





      __A      =       DummyDatasetChild(InMemoryTable.from_pydict(lowerCamelCase				)				).func()





      class A_      (    datasets.Dataset     ):




         @fingerprint_transform(inplace=_UpperCamelCase					,     version='''1.0.0'''				)
         def _lowerCAmelCase   (self			:Union[str, Any]					,     _UpperCamelCase			:int				)->      Any:
               return DummyDatasetChild(self.data					,     fingerprint=_UpperCamelCase				)





      __A      =       DummyDatasetChild(InMemoryTable.from_pydict(lowerCamelCase				)				).func()





      class A_      (    datasets.Dataset     ):




         @fingerprint_transform(inplace=_UpperCamelCase					,     version='''2.0.0'''				)
         def _lowerCAmelCase   (self			:Optional[Any]					,     _UpperCamelCase			:List[str]				)->      str:
               return DummyDatasetChild(self.data					,     fingerprint=_UpperCamelCase				)





      __A      =       DummyDatasetChild(InMemoryTable.from_pydict(lowerCamelCase				)				).func()

      assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}				) == 3







def _a       (			)			->		Any:







      '''simple docstring'''






      __A      =       Hasher()
      hasher.update(lambda lowerCamelCase				: x				)












import argparse
import json
import re
import string
import sys

import numpy as np


snake_case__      :							Dict									=  0.5



def _a       (			lowerCamelCase:     List[str]      ,       lowerCamelCase:     List[str]				)			->		List[Any]:







      '''simple docstring'''






      __A      =       ['''.''', ''',''', ''';''', ''':''']

      for token in remove_tokens:
            __A      =       ground_truth.replace(lowerCamelCase      ,       ''''''				)
            __A      =       prediction.replace(lowerCamelCase      ,       ''''''				)

      __A     ,		__A      =       ground_truth.lower(), prediction.lower()
      __A     ,		__A      =       ground_truth.replace('''/'''      ,       ''' '''				), prediction.replace('''/'''      ,       ''' '''				)
      __A     ,		__A      =       set(ground_truth.split(''' '''				)				), set(prediction.split(''' '''				)				)

      __A      =       ground_truth.intersection(lowerCamelCase				)
      __A      =       ground_truth.union(lowerCamelCase				)
      __A      =       len(lowerCamelCase				) / len(lowerCamelCase				)
      return jaccard



def _a       (			lowerCamelCase:     List[str]				)			->		Dict:







      '''simple docstring'''






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

      def white_space_fix(lowerCamelCase:     Tuple				):
            return " ".join(text.split()				)

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

      def lower(lowerCamelCase:     int				):
            return text.lower()

      return white_space_fix(remove_articles(remove_punc(lower(lowerCamelCase				)				)				)				)



def _a       (			lowerCamelCase:     Dict      ,       lowerCamelCase:     int      ,       lowerCamelCase:     List[str]				)			->		str:







      '''simple docstring'''






      __A     ,		__A     ,		__A      =       0, 0, 0

      __A      =       '''Parties''' in qa_id

      # first check if ground truth is empty
      if len(lowerCamelCase				) == 0:
            if len(lowerCamelCase				) > 0:
                  fp += len(lowerCamelCase				)  # false positive for each one
      else:
            for ground_truth in ground_truths:
                  assert len(lowerCamelCase				) > 0
                  # check if there is a match
                  __A      =       False
                  for pred in predictions:
                        if substr_ok:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH or ground_truth in pred
                        else:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH
                        if is_match:
                              __A      =       True

                  if match_found:
                        tp += 1
                  else:
                        fn += 1

        # now also get any fps by looping through preds
            for pred in predictions:
                  # Check if there's a match. if so, don't count (don't want to double count based on the above)
                  # but if there's no match, then this is a false positive.
                  # (Note: we get the true positives in the above loop instead of this loop so that we don't double count
                  # multiple predictions that are matched with the same answer.)
                  __A      =       False
                  for ground_truth in ground_truths:
                        assert len(lowerCamelCase				) > 0
                        if substr_ok:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH or ground_truth in pred
                        else:
                              __A      =       get_jaccard(lowerCamelCase      ,       lowerCamelCase				) >= IOU_THRESH
                        if is_match:
                              __A      =       True

                  if not match_found:
                        fp += 1

      __A      =       tp / (tp + fp) if tp + fp > 0 else np.nan
      __A      =       tp / (tp + fn) if tp + fn > 0 else np.nan

      return precision, recall



def _a       (			lowerCamelCase:     Dict				)			->		List[str]:







      '''simple docstring'''






      __A      =       precisions[::-1]
      for i in range(1      ,       len(lowerCamelCase				)				):
            __A      =       max(precision_best[i - 1]      ,       precision_best[i]				)
      __A      =       precision_best[::-1]
      return precisions



def _a       (			lowerCamelCase:     Optional[int]      ,       lowerCamelCase:     Dict				)			->		Tuple:







      '''simple docstring'''






      __A      =       process_precisions(lowerCamelCase				)
      __A      =       np.trapz(lowerCamelCase      ,       lowerCamelCase				)
      if np.isnan(lowerCamelCase				):
            return 0
      return aupr



def _a       (			lowerCamelCase:     Any      ,       lowerCamelCase:     List[str]      ,       lowerCamelCase:     Optional[int]				)			->		str:







      '''simple docstring'''






      __A      =       process_precisions(lowerCamelCase				)
      __A      =       0
      for prec, recall in zip(lowerCamelCase      ,       lowerCamelCase				):
            if recall >= recall_thresh:
                  __A      =       prec
                  break
      return prec_at_recall



def _a       (			lowerCamelCase:     Tuple      ,       lowerCamelCase:     Dict				)			->		Optional[Any]:







      '''simple docstring'''






      return normalize_answer(lowerCamelCase				) == normalize_answer(lowerCamelCase				)



def _a       (			lowerCamelCase:     Any      ,       lowerCamelCase:     List[Any]      ,       lowerCamelCase:     Optional[int]				)			->		List[str]:







      '''simple docstring'''






      __A      =       0
      for pred in predictions:
            for ground_truth in ground_truths:
                  __A      =       metric_fn(lowerCamelCase      ,       lowerCamelCase				)
                  if score == 1:  # break the loop when one prediction matches the ground truth
                        break
            if score == 1:
                  break
      return score



def _a       (			lowerCamelCase:     Union[str, Any]      ,       lowerCamelCase:     int				)			->		int:







      '''simple docstring'''






      __A      =       __A      =       __A      =       0
      __A      =       []
      __A      =       []
      for article in dataset:
            for paragraph in article["paragraphs"]:
                  for qa in paragraph["qas"]:
                        total += 1
                        if qa["id"] not in predictions:
                              __A      =       '''Unanswered question ''' + qa['''id'''] + ''' will receive score 0.'''
                              print(lowerCamelCase      ,       file=sys.stderr				)
                              continue
                        __A      =       [x['''text'''] for x in qa['''answers''']]
                        __A      =       predictions[qa['''id''']]
                        __A     ,		__A      =       compute_precision_recall(lowerCamelCase      ,       lowerCamelCase      ,       qa['''id''']				)

                        precisions.append(lowerCamelCase				)
                        recalls.append(lowerCamelCase				)

                        if precision == 0 and recall == 0:
                              fa += 0
                        else:
                              fa += 2 * (precision * recall) / (precision + recall)

                        exact_match += metric_max_over_ground_truths(lowerCamelCase      ,       lowerCamelCase      ,       lowerCamelCase				)

      __A      =       [x for _, x in sorted(zip(lowerCamelCase      ,       lowerCamelCase				)				)]
      recalls.sort()

      __A      =       100.0 * fa / total
      __A      =       100.0 * exact_match / total
      __A      =       get_aupr(lowerCamelCase      ,       lowerCamelCase				)

      __A      =       get_prec_at_recall(lowerCamelCase      ,       lowerCamelCase      ,       recall_thresh=0.9				)
      __A      =       get_prec_at_recall(lowerCamelCase      ,       lowerCamelCase      ,       recall_thresh=0.8				)

      return {
          "exact_match": exact_match,
          "f1": fa,
          "aupr": aupr,
          "prec_at_80_recall": prec_at_aa_recall,
          "prec_at_90_recall": prec_at_aa_recall,
      }


if __name__ == "__main__":
       snake_case__      :							Dict									=  argparse.ArgumentParser(description='Evaluation for CUAD')
       parser.add_argument('dataset_file', help='Dataset file')
       parser.add_argument('prediction_file', help='Prediction File')
       snake_case__      :							List[str]									=  parser.parse_args()
       with open(args.dataset_file) as dataset_file:
              snake_case__      :							List[Any]									=  json.load(dataset_file)
              snake_case__      :							int									=  dataset_json['data']
       with open(args.prediction_file) as prediction_file:
              snake_case__      :							Union[str, Any]									=  json.load(prediction_file)
       print(json.dumps(evaluate(dataset, predictions)))





