




import concurrent.futures
import json
import os
import shutil
import tempfile
import unittest

from transformers import AutoTokenizer, PreTrainedTokenizerFast
from transformers.testing_utils import require_tokenizers

from ..test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 							__SCREAMING_SNAKE_CASE				(						lowerCamelCase  ,	unittest.TestCase    ):
					_lowerCamelCase						    =					PreTrainedTokenizerFast
					_lowerCamelCase						    =					False
					_lowerCamelCase						    =					True
					_lowerCamelCase						    =					"""tokenizer_file"""

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Optional[int]				  =				False  # because we don't have pretrained_vocab_files_map
						super().setUp()
						lowerCamelCase_				:						List[str]				  =				True

						lowerCamelCase_				:						List[str]				  =				['robot-test/dummy-tokenizer-fast', 'robot-test/dummy-tokenizer-wordlevel']
						lowerCamelCase_				:						Optional[Any]				  =				'SaulLu/dummy-tokenizer-bytelevel-bpe'

						# Inclusion of 2 tokenizers to test different types of models (Unigram and WordLevel for the moment)
						lowerCamelCase_				:						Dict				  =				[(PreTrainedTokenizerFast, model_path, {}) for model_path in model_paths]

						lowerCamelCase_				:						Optional[Any]				  =				PreTrainedTokenizerFast.from_pretrained(model_paths[0]   )
						tokenizer.save_pretrained(self.tmpdirname   )

					def       lowercase_				(							self   ):







						'''simple docstring'''




						pass

					def       lowercase_				(							self   ):







						'''simple docstring'''




						pass

					def       lowercase_				(							self   ):







						'''simple docstring'''




						pass

					def       lowercase_				(							self   ):







						'''simple docstring'''




						pass

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Tuple				  =				self.tmpdirname
						# Here we want to test the 2 available tokenizers that use 2 different types of models: Unigram and WordLevel.
						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
							with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"   ):
								try:
									lowerCamelCase_				:						Any				  =				tempfile.mkdtemp()
									lowerCamelCase_				:						Optional[Any]				  =				self.rust_tokenizer_class.from_pretrained(__lowercase					,			**__lowercase   )

									tokenizer.save_pretrained(self.tmpdirname   )
									super().test_training_new_tokenizer()
								finally:
									# Even if the test fails, we must be sure that the folder is deleted and that the default tokenizer
									# is restored
									shutil.rmtree(self.tmpdirname   )
									lowerCamelCase_				:						Union[str, Any]				  =				tmpdirname_orig

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						List[str]				  =				self.tmpdirname
						# Here we want to test the 2 available tokenizers that use 2 different types of models: Unigram and WordLevel.
						for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
							with self.subTest(f"{tokenizer.__class__.__name__} ({pretrained_name})"   ):
								try:
									lowerCamelCase_				:						List[Any]				  =				tempfile.mkdtemp()
									lowerCamelCase_				:						Optional[Any]				  =				self.rust_tokenizer_class.from_pretrained(__lowercase					,			**__lowercase   )

									tokenizer.save_pretrained(self.tmpdirname   )
									super().test_training_new_tokenizer_with_special_tokens_change()
								finally:
									# Even if the test fails, we must be sure that the folder is deleted and that the default tokenizer
									# is restored
									shutil.rmtree(self.tmpdirname   )
									lowerCamelCase_				:						str				  =				tmpdirname_orig





					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Dict				  =				self.rust_tokenizer_class.from_pretrained(self.bytelevel_bpe_model_name   )

						lowerCamelCase_				:						str				  =				('a' for _ in range(1_000   ))
						lowerCamelCase_				:						int				  =				tokenizer.train_new_from_iterator(text_iterator=__lowercase					,			length=1_000					,			vocab_size=50   )

						lowerCamelCase_				:						Dict				  =				new_tokenizer.encode('a🤗'   )
						self.assertEqual(__lowercase					,			[64, 172, 253, 97, 245]   )


@require_tokenizers
class 							__SCREAMING_SNAKE_CASE				(						unittest.TestCase    ):

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Optional[Any]				  =				AutoTokenizer.from_pretrained('bert-base-cased'   )
						lowerCamelCase_				:						Dict				  =				json.loads(tokenizer._tokenizer.to_str()   )
						lowerCamelCase_				:						Tuple				  =				len(__lowercase   )

						with tempfile.TemporaryDirectory() as tmp_dir:
							# Hack to save this in the tokenizer_config.json
							lowerCamelCase_				:						Tuple				  =				['tokenizer.4.0.0.json']
							tokenizer.save_pretrained(__lowercase   )
							json.dump(__lowercase					,			open(os.path.join(__lowercase					,			'tokenizer.4.0.0.json'   )					,			'w'   )   )

							# This should pick the new tokenizer file as the version of Transformers is > 4.0.0
							lowerCamelCase_				:						List[str]				  =				AutoTokenizer.from_pretrained(__lowercase   )
							self.assertEqual(len(__lowercase   )					,			len(__lowercase   ) + 1   )
							lowerCamelCase_				:						Tuple				  =				json.loads(new_tokenizer._tokenizer.to_str()   )
							self.assertIn('huggingface'					,			json_tokenizer['model']['vocab']   )

							# Will need to be adjusted if we reach v42 and this test is still here.
							# Should pick the old tokenizer file as the version of Transformers is < 4.0.0
							shutil.move(os.path.join(__lowercase					,			'tokenizer.4.0.0.json'   )					,			os.path.join(__lowercase					,			'tokenizer.42.0.0.json'   )   )
							lowerCamelCase_				:						List[Any]				  =				['tokenizer.42.0.0.json']
							tokenizer.save_pretrained(__lowercase   )
							lowerCamelCase_				:						str				  =				AutoTokenizer.from_pretrained(__lowercase   )
							self.assertEqual(len(__lowercase   )					,			len(__lowercase   )   )
							lowerCamelCase_				:						int				  =				json.loads(new_tokenizer._tokenizer.to_str()   )
							self.assertNotIn('huggingface'					,			json_tokenizer['model']['vocab']   )





					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Optional[int]				  =				'hf-internal-testing/test-two-tokenizers'

						# This should pick the new tokenizer file as the version of Transformers is > 4.0.0
						lowerCamelCase_				:						int				  =				AutoTokenizer.from_pretrained(__lowercase   )
						self.assertEqual(len(__lowercase   )					,			28_997   )
						lowerCamelCase_				:						str				  =				json.loads(tokenizer._tokenizer.to_str()   )
						self.assertIn('huggingface'					,			json_tokenizer['model']['vocab']   )

						# Testing an older version by monkey-patching the version in the module it's used.
						import transformers as old_transformers

						lowerCamelCase_				:						Union[str, Any]				  =				'3.0.0'
						lowerCamelCase_				:						List[str]				  =				old_transformers.models.auto.AutoTokenizer.from_pretrained(__lowercase   )
						self.assertEqual(len(__lowercase   )					,			28_996   )
						lowerCamelCase_				:						Tuple				  =				json.loads(old_tokenizer._tokenizer.to_str()   )
						self.assertNotIn('huggingface'					,			json_tokenizer['model']['vocab']   )


@require_tokenizers
class 							__SCREAMING_SNAKE_CASE				(						unittest.TestCase    ):

					def       lowercase_				(							self   ):







						'''simple docstring'''




						lowerCamelCase_				:						Optional[int]				  =				PreTrainedTokenizerFast.from_pretrained('robot-test/dummy-tokenizer-wordlevel'   )
						lowerCamelCase_				:						int				  =				'The Matrix is a 1999 science fiction action film.'

						with concurrent.futures.ThreadPoolExecutor() as executor:
							lowerCamelCase_				:						List[str]				  =				[executor.submit(self.fetch					,			__lowercase					,			__lowercase   ) for i in range(10   )]
							lowerCamelCase_				:						List[str]				  =				[future.result() for future in futures]
							self.assertEqual(__lowercase					,			[[1, 10, 0, 8, 0, 18, 0, 0, 0, 2] for i in range(10   )]   )





					def       lowercase_				(							self					,			__lowercase					,			__lowercase   ):







						'''simple docstring'''




						return tokenizer.encode(__lowercase					,			truncation='longest_first'					,			padding='longest'   )





'''simple docstring'''



from sklearn.metrics import accuracy_score

import datasets


lowerCAmelCase						:		Optional[Any]						      = '''
Accuracy is the proportion of correct predictions among the total number of cases processed. It can be computed with:
Accuracy = (TP + TN) / (TP + TN + FP + FN)
 Where:
TP: True positive
TN: True negative
FP: False positive
FN: False negative
'''


lowerCAmelCase						:		str						      = '''
Args:
    predictions (`list` of `int`): Predicted labels.
    references (`list` of `int`): Ground truth labels.
    normalize (`boolean`): If set to False, returns the number of correctly classified samples. Otherwise, returns the fraction of correctly classified samples. Defaults to True.
    sample_weight (`list` of `float`): Sample weights Defaults to None.

Returns:
    accuracy (`float` or `int`): Accuracy score. Minimum possible value is 0. Maximum possible value is 1.0, or the number of examples input, if `normalize` is set to `True`.. A higher score means higher accuracy.

Examples:

    Example 1-A simple example
        >>> accuracy_metric = datasets.load_metric("accuracy")
        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0])
        >>> print(results)
        {\'accuracy\': 0.5}

    Example 2-The same as Example 1, except with `normalize` set to `False`.
        >>> accuracy_metric = datasets.load_metric("accuracy")
        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], normalize=False)
        >>> print(results)
        {\'accuracy\': 3.0}

    Example 3-The same as Example 1, except with `sample_weight` set.
        >>> accuracy_metric = datasets.load_metric("accuracy")
        >>> results = accuracy_metric.compute(references=[0, 1, 2, 0, 1, 2], predictions=[0, 1, 1, 2, 1, 0], sample_weight=[0.5, 2, 0.7, 0.5, 9, 0.4])
        >>> print(results)
        {\'accuracy\': 0.8778625954198473}
'''


lowerCAmelCase						:		int						      = '''
@article{scikit-learn,
  title={Scikit-learn: Machine Learning in {P}ython},
  author={Pedregosa, F. and Varoquaux, G. and Gramfort, A. and Michel, V.
         and Thirion, B. and Grisel, O. and Blondel, M. and Prettenhofer, P.
         and Weiss, R. and Dubourg, V. and Vanderplas, J. and Passos, A. and
         Cournapeau, D. and Brucher, M. and Perrot, M. and Duchesnay, E.},
  journal={Journal of Machine Learning Research},
  volume={12},
  pages={2825--2830},
  year={2011}
}
'''






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



					def 							UpperCAmelCase_			(     self  )  ->      Optional[Any]:
						return datasets.MetricInfo(
						    description=_DESCRIPTION      ,					citation=_CITATION      ,					inputs_description=_KWARGS_DESCRIPTION      ,					features=datasets.Features(
						        {
						            "predictions": datasets.Sequence(datasets.Value("int32"  )  ),
						            "references": datasets.Sequence(datasets.Value("int32"  )  ),
						        }
						        if self.config_name == "multilabel"
						        else {
						            "predictions": datasets.Value("int32"  ),
						            "references": datasets.Value("int32"  ),
						        }  )      ,					reference_urls=["https://scikit-learn.org/stable/modules/generated/sklearn.metrics.accuracy_score.html"]      ,					)



					def 							UpperCAmelCase_			(     self      ,					UpperCamelCase      ,					UpperCamelCase      ,					UpperCamelCase=True      ,					UpperCamelCase=None  )  ->      List[str]:
						return {
						    "accuracy": float(
						        accuracy_score(UpperCamelCase      ,					UpperCamelCase      ,					normalize=UpperCamelCase      ,					sample_weight=UpperCamelCase  )  )
						}