






from importlib import import_module

from .logging import get_logger


__lowerCAmelCase       		=      get_logger(__name__)





class 	SCREAMING_SNAKE_CASE					:






			def __init__( self      :			int  ,						__SCREAMING_SNAKE_CASE      :			Any  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]=None					)			->			Any:
							a_		:  Any											=      attrs or []
							if module is not None:
											for key in module.__dict__:
															if key in attrs or not key.startswith('''__'''					):
																			setattr(self  ,						__SCREAMING_SNAKE_CASE  ,						getattr(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)					)
							a_		:  Tuple											=      module._original_module if isinstance(__SCREAMING_SNAKE_CASE  ,						_PatchedModuleObj					) else module





class 	SCREAMING_SNAKE_CASE					:
			snake_case__  =				[]






			def __init__( self      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[Any]  ,						__SCREAMING_SNAKE_CASE      :			str  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=None					)			->			Dict:
							a_		:  List[str]											=      obj
							a_		:  int											=      target
							a_		:  List[Any]											=      new
							a_		:  Union[str, Any]											=      target.split('''.'''					)[0]
							a_		:  List[Any]											=      {}
							a_		:  str											=      attrs or []






			def __enter__( self      :			Any					)			->			Tuple:
							a_		:  Dict											=      self.target.split('''.'''					)

							# Patch modules:
							# it's used to patch attributes of submodules like "os.path.join";
							# in this case we need to patch "os" and "os.path"

							for i in range(len(__SCREAMING_SNAKE_CASE					)					):
											try:
															a_		:  Optional[Any]											=      import_module('''.'''.join(submodules[: i + 1]					)					)
											except ModuleNotFoundError:
															continue
											# We iterate over all the globals in self.obj in case we find "os" or "os.path"
											for attr in self.obj.__dir__():
															a_		:  List[str]											=      getattr(self.obj  ,						__SCREAMING_SNAKE_CASE					)
															# We don't check for the name of the global, but rather if its value *is* "os" or "os.path".
															# This allows to patch renamed modules like "from os import path as ospath".
															if obj_attr is submodule or (
															    (isinstance(__SCREAMING_SNAKE_CASE  ,						_PatchedModuleObj					) and obj_attr._original_module is submodule)
															):
																			a_		:  Tuple											=      obj_attr
																			# patch at top level
																			setattr(self.obj  ,						__SCREAMING_SNAKE_CASE  ,						_PatchedModuleObj(__SCREAMING_SNAKE_CASE  ,						attrs=self.attrs					)					)
																			a_		:  Union[str, Any]											=      getattr(self.obj  ,						__SCREAMING_SNAKE_CASE					)
																			# construct lower levels patches
																			for key in submodules[i + 1 :]:
																							setattr(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						_PatchedModuleObj(getattr(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)  ,						attrs=self.attrs					)					)
																							a_		:  Any											=      getattr(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE					)
																			# finally set the target attribute
																			setattr(__SCREAMING_SNAKE_CASE  ,						__SCREAMING_SNAKE_CASE  ,						self.new					)

        # Patch attribute itself:
        # it's used for builtins like "open",
        # and also to patch "os.path.join" we may also need to patch "join"
        # itself if it was imported as "from os.path import join".

							if submodules:  # if it's an attribute of a submodule like "os.path.join"
											try:
															a_		:  List[Any]											=      getattr(import_module('''.'''.join(__SCREAMING_SNAKE_CASE					)					)  ,						__SCREAMING_SNAKE_CASE					)
											except (AttributeError, ModuleNotFoundError):
															return
											# We iterate over all the globals in self.obj in case we find "os.path.join"
											for attr in self.obj.__dir__():
															# We don't check for the name of the global, but rather if its value *is* "os.path.join".
															# This allows to patch renamed attributes like "from os.path import join as pjoin".
															if getattr(self.obj  ,						__SCREAMING_SNAKE_CASE					) is attr_value:
																			a_		:  Optional[int]											=      getattr(self.obj  ,						__SCREAMING_SNAKE_CASE					)
																			setattr(self.obj  ,						__SCREAMING_SNAKE_CASE  ,						self.new					)
							elif target_attr in globals()["__builtins__"]:  # if it'a s builtin like "open"
											a_		:  Any											=      globals()["__builtins__"][target_attr]
											setattr(self.obj  ,						__SCREAMING_SNAKE_CASE  ,						self.new					)
							else:
											raise RuntimeError(f'Tried to patch attribute {target_attr} instead of a submodule.'					)






			def __exit__( self      :			Union[str, Any]  ,						*__SCREAMING_SNAKE_CASE      :			List[Any]					)			->			Optional[int]:
							for attr in list(self.original					):
											setattr(self.obj  ,						__SCREAMING_SNAKE_CASE  ,						self.original.pop(__SCREAMING_SNAKE_CASE					)					)






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			Optional[int]:
							self.__enter__()
							self._active_patches.append(self					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			int:
							try:
											self._active_patches.remove(self					)
							except ValueError:
											# If the patch hasn't been started this will fail
											return None

							return self.__exit__()












'''simple docstring'''







import re
import string

import numpy as np

import datasets


__lowerCAmelCase       		=      '\nReturns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.\n'

__lowerCAmelCase       		=      '\nArgs:\n    predictions: List of predicted texts.\n    references: List of reference texts.\n    regexes_to_ignore: List, defaults to None. Regex expressions of characters to\n        ignore when calculating the exact matches. Note: these regexes are removed\n        from the input data before the changes based on the options below (e.g. ignore_case,\n        ignore_punctuation, ignore_numbers) are applied.\n    ignore_case: Boolean, defaults to False. If true, turns everything\n        to lowercase so that capitalization differences are ignored.\n    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\n    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before\n        comparing predictions and references.\nReturns:\n    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.\nExamples:\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    25.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    50.0\n\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True)\n    >>> print(round(results["exact_match"], 1))\n    75.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["the cat", "theater", "YELLING", "agent007"]\n    >>> preds = ["cat?", "theater", "yelling", "agent"]\n    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=["the ", "yell", "YELL"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)\n    >>> print(round(results["exact_match"], 1))\n    100.0\n\n    >>> exact_match = datasets.load_metric("exact_match")\n    >>> refs = ["The cat sat on the mat.", "Theaters are great.", "It\'s like comparing oranges and apples."]\n    >>> preds = ["The cat sat on the mat?", "Theaters are great.", "It\'s like comparing apples and oranges."]\n    >>> results = exact_match.compute(references=refs, predictions=preds)\n    >>> print(round(results["exact_match"], 1))\n    33.3\n\n'

__lowerCAmelCase       		=      '\n'





@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION	,   _KWARGS_DESCRIPTION       )
class 	SCREAMING_SNAKE_CASE					(					datasets.Metric       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[Any]					)			->			int:
							return datasets.MetricInfo(
							    description=_DESCRIPTION  ,						citation=_CITATION  ,						inputs_description=_KWARGS_DESCRIPTION  ,						features=datasets.Features(
							        {
							            '''predictions''': datasets.Value('''string'''  ,						id='''sequence'''					),
							            '''references''': datasets.Value('''string'''  ,						id='''sequence'''					),
							        }					)  ,						reference_urls=[]  ,						)







			def 			SCREAMING_SNAKE_CASE      ( self      :			str  ,						__SCREAMING_SNAKE_CASE      :			List[Any]  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]  ,						__SCREAMING_SNAKE_CASE      :			Dict=None  ,						__SCREAMING_SNAKE_CASE      :			int=False  ,						__SCREAMING_SNAKE_CASE      :			Optional[int]=False  ,						__SCREAMING_SNAKE_CASE      :			Dict=False  ,						)			->			str:
							if regexes_to_ignore is not None:
											for s in regexes_to_ignore:
															a_		:  Optional[Any]											=      np.array([re.sub(__SCREAMING_SNAKE_CASE  ,						''''''  ,						__SCREAMING_SNAKE_CASE					) for x in predictions]					)
															a_		:  int											=      np.array([re.sub(__SCREAMING_SNAKE_CASE  ,						''''''  ,						__SCREAMING_SNAKE_CASE					) for x in references]					)
							else:
											a_		:  List[str]											=      np.asarray(__SCREAMING_SNAKE_CASE					)
											a_		:  Any											=      np.asarray(__SCREAMING_SNAKE_CASE					)

							if ignore_case:
											a_		:  List[str]											=      np.char.lower(__SCREAMING_SNAKE_CASE					)
											a_		:  List[Any]											=      np.char.lower(__SCREAMING_SNAKE_CASE					)

							if ignore_punctuation:
											a_		:  Any											=      string.punctuation.maketrans(''''''  ,						''''''  ,						string.punctuation					)
											a_		:  Union[str, Any]											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)
											a_		:  int											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)

							if ignore_numbers:
											a_		:  int											=      string.digits.maketrans(''''''  ,						''''''  ,						string.digits					)
											a_		:  Optional[int]											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)
											a_		:  Dict											=      np.char.translate(__SCREAMING_SNAKE_CASE  ,						table=__SCREAMING_SNAKE_CASE					)

							a_		:  Optional[Any]											=      predictions == references

							return {"exact_match": np.mean(__SCREAMING_SNAKE_CASE					) * 100}




