
"""simple docstring"""




import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs


class       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       def __init__(  self			,					A_			,					A_=None			,					A_=True			,					A_=None			,					**A_							)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      str   			=  parent
         _lowercase:      int   			=  config_class
         _lowercase:      List[Any]   			=  has_text_modality
         _lowercase:      Optional[int]   			=  kwargs
         _lowercase:      Optional[int]   			=  common_properties
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Union[str, Any]   			=  self.config_class(**self.inputs_dict							)
         _lowercase:      Union[str, Any]   			=  (
             ['''hidden_size''', '''num_attention_heads''', '''num_hidden_layers''']
             if self.common_properties is None
             else self.common_properties
         )

         # Add common fields for text models
         if self.has_text_modality:
           common_properties.extend(['''vocab_size''']							)

         # Test that config has the common properties as getters
         for prop in common_properties:
           self.parent.assertTrue(hasattr(_lowerCAmelCase			,					_lowerCAmelCase							)			,					msg=f'''`{prop}` does not exist'''							)

         # Test that config has the common properties as setter
         for idx, name in enumerate(_lowerCAmelCase							):
           try:
             setattr(_lowerCAmelCase			,					_lowerCAmelCase			,					_lowerCAmelCase							)
             self.parent.assertEqual(
                 getattr(_lowerCAmelCase			,					_lowerCAmelCase							)			,					_lowerCAmelCase			,					msg=f'''`{name} value {idx} expected, but was {getattr(_lowerCAmelCase			,					_lowerCAmelCase							)}'''							)
           except NotImplementedError:
             # Some models might not be able to implement setters for common_properties
             # In that case, a NotImplementedError is raised
             pass

        # Test if config class can be called with Config(prop_name=..)
         for idx, name in enumerate(_lowerCAmelCase							):
           try:
             _lowercase:      int   			=  self.config_class(**{name: idx}							)
             self.parent.assertEqual(
                 getattr(_lowerCAmelCase			,					_lowerCAmelCase							)			,					_lowerCAmelCase			,					msg=f'''`{name} value {idx} expected, but was {getattr(_lowerCAmelCase			,					_lowerCAmelCase							)}'''							)
           except NotImplementedError:
             # Some models might not be able to implement setters for common_properties
             # In that case, a NotImplementedError is raised
             pass
       def 				lowercase_				(  self							)  ->						Tuple:





         """simple docstring"""




         _lowercase:      Union[str, Any]   			=  self.config_class(**self.inputs_dict							)
         _lowercase:      str   			=  json.loads(config.to_json_string()							)
         for key, value in self.inputs_dict.items():
           self.parent.assertEqual(obj[key]			,					_lowerCAmelCase							)
       def 				lowercase_				(  self							)  ->						List[str]:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  self.config_class(**self.inputs_dict							)

         with tempfile.TemporaryDirectory() as tmpdirname:
           _lowercase:      Optional[Any]   			=  os.path.join(_lowerCAmelCase			,					'''config.json'''							)
           config_first.to_json_file(_lowerCAmelCase							)
           _lowercase:      Union[str, Any]   			=  self.config_class.from_json_file(_lowerCAmelCase							)

         self.parent.assertEqual(config_second.to_dict()			,					config_first.to_dict()							)
       def 				lowercase_				(  self							)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      str   			=  self.config_class(**self.inputs_dict							)

         with tempfile.TemporaryDirectory() as tmpdirname:
           config_first.save_pretrained(_lowerCAmelCase							)
           _lowercase:      str   			=  self.config_class.from_pretrained(_lowerCAmelCase							)

         self.parent.assertEqual(config_second.to_dict()			,					config_first.to_dict()							)
       def 				lowercase_				(  self							)  ->						Optional[int]:





         """simple docstring"""




         _lowercase:      Any   			=  self.config_class(**self.inputs_dict							)

         _lowercase:      Tuple   			=  '''test'''
         with tempfile.TemporaryDirectory() as tmpdirname:
           _lowercase:      List[str]   			=  os.path.join(_lowerCAmelCase			,					_lowerCAmelCase							)
           config_first.save_pretrained(_lowerCAmelCase							)
           _lowercase:      Any   			=  self.config_class.from_pretrained(_lowerCAmelCase			,					subfolder=_lowerCAmelCase							)

         self.parent.assertEqual(config_second.to_dict()			,					config_first.to_dict()							)
       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         _lowercase:      Optional[int]   			=  self.config_class(**self.inputs_dict			,					num_labels=5							)
         self.parent.assertEqual(len(config.idalabel							)			,					5							)
         self.parent.assertEqual(len(config.labelaid							)			,					5							)

         _lowercase:      Optional[Any]   			=  3
         self.parent.assertEqual(len(config.idalabel							)			,					3							)
         self.parent.assertEqual(len(config.labelaid							)			,					3							)
       def 				lowercase_				(  self							)  ->						str:





         """simple docstring"""




         if self.config_class.is_composition:
           return
         _lowercase:      Union[str, Any]   			=  self.config_class()
         self.parent.assertIsNotNone(_lowerCAmelCase							)
       def 				lowercase_				(  self							)  ->						List[str]:





         """simple docstring"""




         _lowercase:      int   			=  copy.deepcopy(_lowerCAmelCase							)
         _lowercase:      Optional[Any]   			=  self.config_class(**_lowerCAmelCase							)
         _lowercase:      Any   			=  []
         for key, value in config_common_kwargs.items():
           if key == "torch_dtype":
             if not is_torch_available():
               continue
             else:
               import torch

               if config.torch_dtype != torch.floataa:
                 wrong_values.append(('''torch_dtype''', config.torch_dtype, torch.floataa)							)
           elif getattr(_lowerCAmelCase			,					_lowerCAmelCase							) != value:
             wrong_values.append((key, getattr(_lowerCAmelCase			,					_lowerCAmelCase							), value)							)

         if len(_lowerCAmelCase							) > 0:
           _lowercase:      Dict   			=  '''\n'''.join([f'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]							)
           raise ValueError(f'''The following keys were not properly set in the config:\n{errors}'''							)



       def 				lowercase_				(  self							)  ->						Dict:





         """simple docstring"""




         self.create_and_test_config_common_properties()
         self.create_and_test_config_to_json_string()
         self.create_and_test_config_to_json_file()
         self.create_and_test_config_from_and_save_pretrained()
         self.create_and_test_config_from_and_save_pretrained_subfolder()
         self.create_and_test_config_with_num_labels()
         self.check_config_can_be_init_without_params()
         self.check_config_arguments_init()







'''simple docstring'''


import re
import string

import numpy as np

import datasets


_SCREAMING_SNAKE_CASE									=				"\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"

_SCREAMING_SNAKE_CASE									=				"\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"

_SCREAMING_SNAKE_CASE									=				"\n"

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




 def 				_snake_case (						self		)     ->				List[str]:
     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 				_snake_case (						self      ,  _lowerCAmelCase      ,  _lowerCAmelCase      ,  _lowerCAmelCase=None      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=False      ,  _lowerCAmelCase=False      ,  )     ->				str:
     if regexes_to_ignore is not None:
         for s in regexes_to_ignore:
             _lowerCAmelCase						      =   np.array([re.sub(_lowerCAmelCase      ,  ""      ,  _lowerCAmelCase		) for x in predictions]		)
             _lowerCAmelCase						      =   np.array([re.sub(_lowerCAmelCase      ,  ""      ,  _lowerCAmelCase		) for x in references]		)
     else:
         _lowerCAmelCase						      =   np.asarray(_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.asarray(_lowerCAmelCase		)

     if ignore_case:
         _lowerCAmelCase						      =   np.char.lower(_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.char.lower(_lowerCAmelCase		)

     if ignore_punctuation:
         _lowerCAmelCase						      =   string.punctuation.maketrans(""      ,  ""      ,  string.punctuation		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)

     if ignore_numbers:
         _lowerCAmelCase						      =   string.digits.maketrans(""      ,  ""      ,  string.digits		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)
         _lowerCAmelCase						      =   np.char.translate(_lowerCAmelCase      ,  table=_lowerCAmelCase		)

     _lowerCAmelCase						      =   predictions == references

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


