




import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


__SCREAMING_SNAKE_CASE 						="""hf-internal-testing/tiny-random-bert"""
__SCREAMING_SNAKE_CASE 						=os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
__SCREAMING_SNAKE_CASE 						="""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""






class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				def     _A							(							self:   List[str]		):
											SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					_lowerCamelCase		)
											# Should have downloaded the file in here
											self.assertTrue(os.path.isdir(_lowerCamelCase		)		)
											# Cache should contain at least those three subfolders:
											for subfolder in ["blobs", "refs", "snapshots"]:
																		self.assertTrue(os.path.isdir(os.path.join(_lowerCamelCase		,					_lowerCamelCase		)		)		)
											with open(os.path.join(_lowerCamelCase		,					'''refs'''		,					'''main'''		)		) as f:
																		SCREAMING_SNAKE_CASE_   	=    f.read()
											self.assertEqual(_lowerCamelCase		,					os.path.join(_lowerCamelCase		,					'''snapshots'''		,					_lowerCamelCase		,					_lowerCamelCase		)		)
											self.assertTrue(os.path.isfile(_lowerCamelCase		)		)

											# File is cached at the same place the second time.
											SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					_lowerCamelCase		)
											self.assertEqual(_lowerCamelCase		,					_lowerCamelCase		)

											# Using a specific revision to test the full commit hash.
											SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					_lowerCamelCase		,					revision='''9b8c223'''		)
											self.assertEqual(_lowerCamelCase		,					os.path.join(_lowerCamelCase		,					'''snapshots'''		,					_lowerCamelCase		,					_lowerCamelCase		)		)


				def     _A							(							self:   List[Any]		):
											with self.assertRaisesRegex(_lowerCamelCase		,					'''is not a valid model identifier'''		):
																		SCREAMING_SNAKE_CASE_   	=    cached_file('''tiny-random-bert'''		,					_lowerCamelCase		)

											with self.assertRaisesRegex(_lowerCamelCase		,					'''is not a valid git identifier'''		):
																		SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					_lowerCamelCase		,					revision='''aaaa'''		)

											with self.assertRaisesRegex(_lowerCamelCase		,					'''does not appear to have a file named'''		):
																		SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					'''conf'''		)


				def     _A							(							self:   str		):
											with self.assertRaisesRegex(_lowerCamelCase		,					'''does not appear to have a file named'''		):
																		SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					'''conf'''		)

											with open(os.path.join(_lowerCamelCase		,					'''refs'''		,					'''main'''		)		) as f:
																		SCREAMING_SNAKE_CASE_   	=    f.read()
											self.assertTrue(os.path.isfile(os.path.join(_lowerCamelCase		,					'''.no_exist'''		,					_lowerCamelCase		,					'''conf'''		)		)		)

											SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					'''conf'''		,					_raise_exceptions_for_missing_entries=_lowerCamelCase		)
											self.assertIsNone(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					'''conf'''		,					local_files_only=_lowerCamelCase		,					_raise_exceptions_for_missing_entries=_lowerCamelCase		)
											self.assertIsNone(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    mock.Mock()
											SCREAMING_SNAKE_CASE_   	=    5_00
											SCREAMING_SNAKE_CASE_   	=    {}
											SCREAMING_SNAKE_CASE_   	=    HTTPError
											SCREAMING_SNAKE_CASE_   	=    {}

											# Under the mock environment we get a 500 error when trying to reach the tokenizer.
											with mock.patch('''requests.Session.request'''		,					return_value=_lowerCamelCase		) as mock_head:
																		SCREAMING_SNAKE_CASE_   	=    cached_file(_lowerCamelCase		,					'''conf'''		,					_raise_exceptions_for_connection_errors=_lowerCamelCase		)
																		self.assertIsNone(_lowerCamelCase		)
																		# This check we did call the fake head request
																		mock_head.assert_called()


				def     _A							(							self:   Optional[int]		):
											self.assertTrue(has_file('''hf-internal-testing/tiny-bert-pt-only'''		,					_lowerCamelCase		)		)
											self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''		,					_lowerCamelCase		)		)
											self.assertFalse(has_file('''hf-internal-testing/tiny-bert-pt-only'''		,					_lowerCamelCase		)		)


				def     _A							(							self:   Tuple		):
											# `get_file_from_repo` returns None if the file does not exist
											self.assertIsNone(get_file_from_repo('''bert-base-cased'''		,					'''ahah.txt'''		)		)

											# The function raises if the repository does not exist.
											with self.assertRaisesRegex(_lowerCamelCase		,					'''is not a valid model identifier'''		):
																		get_file_from_repo('''bert-base-case'''		,					_lowerCamelCase		)

											# The function raises if the revision does not exist.
											with self.assertRaisesRegex(_lowerCamelCase		,					'''is not a valid git identifier'''		):
																		get_file_from_repo('''bert-base-cased'''		,					_lowerCamelCase		,					revision='''ahaha'''		)

											SCREAMING_SNAKE_CASE_   	=    get_file_from_repo('''bert-base-cased'''		,					_lowerCamelCase		)
											# The name is the cached name which is not very easy to test, so instead we load the content.
											SCREAMING_SNAKE_CASE_   	=    json.loads(open(_lowerCamelCase		,					'''r'''		).read()		)
											self.assertEqual(config['''hidden_size''']		,					7_68		)





				def     _A							(							self:   List[str]		):
											with tempfile.TemporaryDirectory() as tmp_dir:
																		SCREAMING_SNAKE_CASE_   	=    Path(_lowerCamelCase		) / '''a.txt'''
																		filename.touch()
																		self.assertEqual(get_file_from_repo(_lowerCamelCase		,					'''a.txt'''		)		,					str(_lowerCamelCase		)		)

																		self.assertIsNone(get_file_from_repo(_lowerCamelCase		,					'''b.txt'''		)		)








import re
import string

import numpy as np

import datasets


__SCREAMING_SNAKE_CASE 						="""
Returns the rate at which the input predicted strings exactly match their references, ignoring any strings input as part of the regexes_to_ignore list.
"""

__SCREAMING_SNAKE_CASE 						="""
Args:
    predictions: List of predicted texts.
    references: List of reference texts.
    regexes_to_ignore: List, defaults to None. Regex expressions of characters to
        ignore when calculating the exact matches. Note: these regexes are removed
        from the input data before the changes based on the options below (e.g. ignore_case,
        ignore_punctuation, ignore_numbers) are applied.
    ignore_case: Boolean, defaults to False. If true, turns everything
        to lowercase so that capitalization differences are ignored.
    ignore_punctuation: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
    ignore_numbers: Boolean, defaults to False. If true, removes all punctuation before
        comparing predictions and references.
Returns:
    exact_match: Dictionary containing exact_match rate. Possible values are between 0.0 and 100.0, inclusive.
Examples:
    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results[\"exact_match\"], 1))
    25.0

    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results[\"exact_match\"], 1))
    50.0


    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True)
    >>> print(round(results[\"exact_match\"], 1))
    75.0

    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"the cat\", \"theater\", \"YELLING\", \"agent007\"]
    >>> preds = [\"cat?\", \"theater\", \"yelling\", \"agent\"]
    >>> results = exact_match.compute(references=refs, predictions=preds, regexes_to_ignore=[\"the \", \"yell\", \"YELL\"], ignore_case=True, ignore_punctuation=True, ignore_numbers=True)
    >>> print(round(results[\"exact_match\"], 1))
    100.0

    >>> exact_match = datasets.load_metric(\"exact_match\")
    >>> refs = [\"The cat sat on the mat.\", \"Theaters are great.\", \"It's like comparing oranges and apples.\"]
    >>> preds = [\"The cat sat on the mat?\", \"Theaters are great.\", \"It's like comparing apples and oranges.\"]
    >>> results = exact_match.compute(references=refs, predictions=preds)
    >>> print(round(results[\"exact_match\"], 1))
    33.3

"""

__SCREAMING_SNAKE_CASE 						="""
"""






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION    ,  _KWARGS_DESCRIPTION)
class  __magic_name__						(  datasets.Metric):
				'''simple docstring'''







				def     _A							(							self:   Optional[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     _A							(							self:   Tuple		,					_lowerCamelCase:   int		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Tuple=None		,					_lowerCamelCase:   str=False		,					_lowerCamelCase:   List[str]=False		,					_lowerCamelCase:   List[str]=False		,					):
											if regexes_to_ignore is not None:
																		for s in regexes_to_ignore:
																									SCREAMING_SNAKE_CASE_   	=    np.array([re.sub(_lowerCamelCase		,					''''''		,					_lowerCamelCase		) for x in predictions]		)
																									SCREAMING_SNAKE_CASE_   	=    np.array([re.sub(_lowerCamelCase		,					''''''		,					_lowerCamelCase		) for x in references]		)
											else:
																		SCREAMING_SNAKE_CASE_   	=    np.asarray(_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    np.asarray(_lowerCamelCase		)

											if ignore_case:
																		SCREAMING_SNAKE_CASE_   	=    np.char.lower(_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    np.char.lower(_lowerCamelCase		)

											if ignore_punctuation:
																		SCREAMING_SNAKE_CASE_   	=    string.punctuation.maketrans(''''''		,					''''''		,					string.punctuation		)
																		SCREAMING_SNAKE_CASE_   	=    np.char.translate(_lowerCamelCase		,					table=_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    np.char.translate(_lowerCamelCase		,					table=_lowerCamelCase		)

											if ignore_numbers:
																		SCREAMING_SNAKE_CASE_   	=    string.digits.maketrans(''''''		,					''''''		,					string.digits		)
																		SCREAMING_SNAKE_CASE_   	=    np.char.translate(_lowerCamelCase		,					table=_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    np.char.translate(_lowerCamelCase		,					table=_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    predictions == references

											return {"exact_match": np.mean(_lowerCamelCase		) * 1_00}


