

import json
import os
import shutil
import tempfile
import unittest

from transformers import BatchEncoding, CanineTokenizer
from transformers.testing_utils import require_tokenizers, require_torch
from transformers.tokenization_utils import AddedToken
from transformers.utils import cached_property

from ...test_tokenization_common import TokenizerTesterMixin


class 			A						(							_UpperCAmelCase   ,							unittest.TestCase			):







    """simple docstring"""





    lowerCamelCase		     =							CanineTokenizer
    lowerCamelCase		     =							False


    def 						snake_case__					(				self			:	Dict		)->   int:





        '''simple docstring'''







        super().setUp()
        A__   				=       CanineTokenizer()
        tokenizer.save_pretrained(self.tmpdirname		)


    @cached_property
    def 						snake_case__					(				self			:	Any		)->   Union[str, Any]:





        '''simple docstring'''







        return CanineTokenizer.from_pretrained('google/canine-s'		)


    def 						snake_case__					(				self			:	Union[str, Any],**lowercase_			:	Union[str, Any]		)->   CanineTokenizer:





        '''simple docstring'''







        A__   				=       self.tokenizer_class.from_pretrained(self.tmpdirname,**lowercase_		)
        A__   				=       1_0_2_4
        return tokenizer


    @require_torch
    def 						snake_case__					(				self			:	str		)->   int:





        '''simple docstring'''







        A__   				=       self.canine_tokenizer
        A__   				=       ['Life is like a box of chocolates.', 'You never know what you\'re gonna get.']
        # fmt: off
        A__   				=       [5_7_3_4_4, 7_6, 1_0_5, 1_0_2, 1_0_1, 3_2, 1_0_5, 1_1_5, 3_2, 1_0_8, 1_0_5, 1_0_7, 1_0_1, 3_2, 9_7, 3_2, 9_8, 1_1_1, 1_2_0, 3_2, 1_1_1, 1_0_2, 3_2, 9_9, 1_0_4, 1_1_1, 9_9, 1_1_1, 1_0_8, 9_7, 1_1_6, 1_0_1, 1_1_5, 4_6, 5_7_3_4_5, 0, 0, 0, 0]
        # fmt: on
        A__   				=       tokenizer(lowercase_,padding=lowercase_,return_tensors='pt'		)
        self.assertIsInstance(lowercase_,lowercase_		)

        A__   				=       list(batch.input_ids.numpy()[0]		)

        self.assertListEqual(lowercase_,lowercase_		)

        self.assertEqual((2, 3_9),batch.input_ids.shape		)
        self.assertEqual((2, 3_9),batch.attention_mask.shape		)


    @require_torch
    def 						snake_case__					(				self			:	Tuple		)->   int:





        '''simple docstring'''







        A__   				=       self.canine_tokenizer
        A__   				=       ['Once there was a man.', 'He wrote a test in HuggingFace Tranformers.']
        A__   				=       tokenizer(lowercase_,padding=lowercase_,return_tensors='pt'		)
        # check if input_ids, attention_mask and token_type_ids are returned
        self.assertIn('input_ids',lowercase_		)
        self.assertIn('attention_mask',lowercase_		)
        self.assertIn('token_type_ids',lowercase_		)


    @require_torch
    def 						snake_case__					(				self			:	Optional[Any]		)->   Dict:





        '''simple docstring'''







        A__   				=       self.canine_tokenizer
        A__   				=       [
            'What\'s the weater?',
            'It\'s about 25 degrees.',
        ]
        A__   				=       tokenizer(
            text_target=lowercase_,max_length=3_2,padding='max_length',truncation=lowercase_,return_tensors='pt'		)
        self.assertEqual(3_2,targets['input_ids'].shape[1]		)


    def 						snake_case__					(				self			:	Any		)->   str:





        '''simple docstring'''







        A__   				=       self.get_tokenizers()
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                self.assertNotEqual(tokenizer.model_max_length,4_2		)

        # Now let's start the test
        A__   				=       self.get_tokenizers()
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                # Isolate this from the other tests because we save additional tokens/etc
                A__   				=       tempfile.mkdtemp()

                A__   				=       ' He is very happy, UNwant\u00E9d,running'
                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                tokenizer.save_pretrained(lowercase_		)

                A__   				=       tokenizer.__class__.from_pretrained(lowercase_		)
                A__   				=       after_tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                self.assertListEqual(lowercase_,lowercase_		)

                shutil.rmtree(lowercase_		)

        A__   				=       self.get_tokenizers(model_max_length=4_2		)
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                # Isolate this from the other tests because we save additional tokens/etc
                A__   				=       tempfile.mkdtemp()

                A__   				=       ' He is very happy, UNwant\u00E9d,running'

                A__   				=       tokenizer.additional_special_tokens

                # We can add a new special token for Canine as follows:
                A__   				=       chr(0Xe_0_0_7		)
                additional_special_tokens.append(lowercase_		)
                tokenizer.add_special_tokens({'additional_special_tokens': additional_special_tokens}		)
                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                tokenizer.save_pretrained(lowercase_		)

                A__   				=       tokenizer.__class__.from_pretrained(lowercase_		)
                A__   				=       after_tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                self.assertListEqual(lowercase_,lowercase_		)
                self.assertIn(lowercase_,after_tokenizer.additional_special_tokens		)
                self.assertEqual(after_tokenizer.model_max_length,4_2		)

                A__   				=       tokenizer.__class__.from_pretrained(lowercase_,model_max_length=4_3		)
                self.assertEqual(tokenizer.model_max_length,4_3		)

                shutil.rmtree(lowercase_		)


    def 						snake_case__					(				self			:	Union[str, Any]		)->   List[Any]:





        '''simple docstring'''







        A__   				=       self.get_tokenizers(do_lower_case=lowercase_		)
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                A__				,						A__   				=       self.get_clean_sequence(lowercase_		)

                # a special token for Canine can be defined as follows:
                A__   				=       0Xe_0_0_5
                A__   				=       chr(lowercase_		)

                tokenizer.add_special_tokens({'cls_token': special_token}		)
                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                self.assertEqual(len(lowercase_		),1		)

                A__   				=       tokenizer.decode(ids + encoded_special_token,clean_up_tokenization_spaces=lowercase_		)
                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)

                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                self.assertEqual(lowercase_,input_encoded + special_token_id		)

                A__   				=       tokenizer.decode(lowercase_,skip_special_tokens=lowercase_		)
                self.assertTrue(special_token not in decoded		)


    def 						snake_case__					(				self			:	Any		)->   Tuple:





        '''simple docstring'''







        A__   				=       self.get_tokenizers(do_lower_case=lowercase_		)
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                A__   				=       chr(0Xe_0_0_5		)
                A__   				=       chr(0Xe_0_0_6		)

                # `add_tokens` method stores special tokens only in `tokenizer.unique_no_split_tokens`. (in tokenization_utils.py)
                tokenizer.add_tokens([SPECIAL_TOKEN_1],special_tokens=lowercase_		)
                # `add_special_tokens` method stores special tokens in `tokenizer.additional_special_tokens`,
                # which also occur in `tokenizer.all_special_tokens`. (in tokenization_utils_base.py)
                tokenizer.add_special_tokens({'additional_special_tokens': [SPECIAL_TOKEN_2]}		)

                A__   				=       tokenizer.tokenize(lowercase_		)
                A__   				=       tokenizer.tokenize(lowercase_		)

                self.assertEqual(len(lowercase_		),1		)
                self.assertEqual(len(lowercase_		),1		)
                self.assertEqual(token_a[0],lowercase_		)
                self.assertEqual(token_a[0],lowercase_		)


    @require_tokenizers
    def 						snake_case__					(				self			:	List[str]		)->   int:





        '''simple docstring'''







        A__   				=       self.get_tokenizers(do_lower_case=lowercase_		)
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                # a special token for Canine can be defined as follows:
                A__   				=       0Xe_0_0_6
                A__   				=       chr(lowercase_		)

                A__   				=       AddedToken(lowercase_,lstrip=lowercase_		)
                tokenizer.add_special_tokens({'additional_special_tokens': [new_token]}		)

                with tempfile.TemporaryDirectory() as tmp_dir_name:
                    tokenizer.save_pretrained(lowercase_		)
                    tokenizer.from_pretrained(lowercase_		)


    def 						snake_case__					(				self			:	List[str]		)->   List[str]:





        '''simple docstring'''







        A__   				=       []
        if self.test_slow_tokenizer:
            tokenizer_list.append((self.tokenizer_class, self.get_tokenizer())		)

        if self.test_rust_tokenizer:
            tokenizer_list.append((self.rust_tokenizer_class, self.get_rust_tokenizer())		)

        for tokenizer_class, tokenizer_utils in tokenizer_list:
            with tempfile.TemporaryDirectory() as tmp_dir:
                tokenizer_utils.save_pretrained(lowercase_		)

                with open(os.path.join(lowercase_,'special_tokens_map.json'		),encoding='utf-8'		) as json_file:
                    A__   				=       json.load(lowercase_		)

                with open(os.path.join(lowercase_,'tokenizer_config.json'		),encoding='utf-8'		) as json_file:
                    A__   				=       json.load(lowercase_		)

                # a special token for Canine can be defined as follows:
                A__   				=       0Xe_0_0_6
                A__   				=       chr(lowercase_		)

                A__   				=       [new_token_a]
                A__   				=       [new_token_a]

                with open(os.path.join(lowercase_,'special_tokens_map.json'		),'w',encoding='utf-8'		) as outfile:
                    json.dump(lowercase_,lowercase_		)
                with open(os.path.join(lowercase_,'tokenizer_config.json'		),'w',encoding='utf-8'		) as outfile:
                    json.dump(lowercase_,lowercase_		)

                # the following checks allow us to verify that our test works as expected, i.e. that the tokenizer takes
                # into account the new value of additional_special_tokens given in the "tokenizer_config.json" and
                # "special_tokens_map.json" files
                A__   				=       tokenizer_class.from_pretrained(lowercase_,extra_ids=0		)
                self.assertIn(lowercase_,tokenizer_without_change_in_init.additional_special_tokens		)
                # self.assertIn("an_additional_special_token",tokenizer_without_change_in_init.get_vocab()) # ByT5Tokenization no vocab
                self.assertEqual(
                    [new_token_a],tokenizer_without_change_in_init.convert_ids_to_tokens(
                        tokenizer_without_change_in_init.convert_tokens_to_ids([new_token_a]		)		),)

                A__   				=       0Xe_0_0_7
                A__   				=       chr(lowercase_		)
                # Now we test that we can change the value of additional_special_tokens in the from_pretrained
                A__   				=       [AddedToken(lowercase_,lstrip=lowercase_		)]
                A__   				=       tokenizer_class.from_pretrained(
                    lowercase_,additional_special_tokens=lowercase_,extra_ids=0		)

                self.assertIn(lowercase_,tokenizer.additional_special_tokens		)
                # self.assertIn(new_token_2,tokenizer.get_vocab()) # ByT5Tokenization no vocab
                self.assertEqual(
                    [new_token_a],tokenizer.convert_ids_to_tokens(tokenizer.convert_tokens_to_ids([new_token_a]		)		)		)


    @require_tokenizers
    def 						snake_case__					(				self			:	List[Any]		)->   Any:





        '''simple docstring'''







        A__   				=       self.get_tokenizers(do_lower_case=lowercase_		)
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                A__   				=       'hello world'
                if self.space_between_special_tokens:
                    A__   				=       '[CLS] hello world [SEP]'
                else:
                    A__   				=       input
                A__   				=       tokenizer.encode(lowercase_,add_special_tokens=lowercase_		)
                A__   				=       tokenizer.decode(lowercase_,spaces_between_special_tokens=self.space_between_special_tokens		)
                self.assertIn(lowercase_,[output, output.lower()]		)


    def 						snake_case__					(				self			:	Union[str, Any]		)->   Optional[Any]:





        '''simple docstring'''







        A__   				=       self.get_tokenizers()
        for tokenizer in tokenizers:
            with self.subTest(F'{tokenizer.__class__.__name__}'		):
                A__   				=       [
                    'bos_token',
                    'eos_token',
                    'unk_token',
                    'sep_token',
                    'pad_token',
                    'cls_token',
                    'mask_token',
                ]

        A__   				=       'a'
        A__   				=       ord(lowercase_		)

        for attr in attributes_list:
            setattr(lowercase_,attr + '_id',lowercase_		)
            self.assertEqual(getattr(lowercase_,lowercase_		),lowercase_		)
            self.assertEqual(getattr(lowercase_,attr + '_id'		),lowercase_		)

            setattr(lowercase_,attr + '_id',lowercase_		)
            self.assertEqual(getattr(lowercase_,lowercase_		),lowercase_		)
            self.assertEqual(getattr(lowercase_,attr + '_id'		),lowercase_		)

        setattr(lowercase_,'additional_special_tokens_ids',[]		)
        self.assertListEqual(getattr(lowercase_,'additional_special_tokens'		),[]		)
        self.assertListEqual(getattr(lowercase_,'additional_special_tokens_ids'		),[]		)

        A__   				=       0Xe_0_0_6
        A__   				=       chr(lowercase_		)
        setattr(lowercase_,'additional_special_tokens_ids',[additional_special_token_id]		)
        self.assertListEqual(getattr(lowercase_,'additional_special_tokens'		),[additional_special_token]		)
        self.assertListEqual(getattr(lowercase_,'additional_special_tokens_ids'		),[additional_special_token_id]		)


    def 						snake_case__					(				self			:	Union[str, Any]		)->   str:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	Optional[int]		)->   List[str]:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	str		)->   int:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	str		)->   int:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	Optional[Any]		)->   List[Any]:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	Tuple		)->   Dict:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	Optional[int]		)->   Union[str, Any]:





        '''simple docstring'''







        pass


    def 						snake_case__					(				self			:	List[Any]		)->   Optional[Any]:





        '''simple docstring'''







        pass




import datasets

from .evaluate import evaluate


lowercase_			=  "\\n@inproceedings{Rajpurkar2016SQuAD10,\n  title={SQuAD: 100, 000+ Questions for Machine Comprehension of Text},\n  author={Pranav Rajpurkar and Jian Zhang and Konstantin Lopyrev and Percy Liang},\n  booktitle={EMNLP},\n  year={2016}\n}\n"

lowercase_			=  "\nThis metric wrap the official scoring script for version 1 of the Stanford Question Answering Dataset (SQuAD).\n\nStanford Question Answering Dataset (SQuAD) is a reading comprehension dataset, consisting of questions posed by\ncrowdworkers on a set of Wikipedia articles, where the answer to every question is a segment of text, or span,\nfrom the corresponding reading passage, or the question might be unanswerable.\n"

lowercase_			=  "\nComputes SQuAD scores (F1 and EM).\nArgs:\n    predictions: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair as given in the references (see below)\n        - 'prediction_text': the text of the answer\n    references: List of question-answers dictionaries with the following key-values:\n        - 'id': id of the question-answer pair (see above),\n        - 'answers': a Dict in the SQuAD dataset format\n            {\n                'text': list of possible texts for the answer, as a list of strings\n                'answer_start': list of start positions for the answer, as a list of ints\n            }\n            Note that answer_start values are not taken into account to compute the metric.\nReturns:\n    'exact_match': Exact match (the normalized answer exactly match the gold answer)\n    'f1': The F-score of predicted tokens versus the gold answer\nExamples:\n\n    >>> predictions = [{'prediction_text': '1976', 'id': '56e10a3be3433e1400422b22'}]\n    >>> references = [{'answers': {'answer_start': [97], 'text': ['1976']}, 'id': '56e10a3be3433e1400422b22'}]\n    >>> squad_metric = datasets.load_metric(\"squad\")\n    >>> results = squad_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'exact_match': 100.0, 'f1': 100.0}\n"


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







    """simple docstring"""





    def 						snake_case__					(				self			:	List[Any]		)->   Tuple:





        '''simple docstring'''







        return datasets.MetricInfo(
            description=_DESCRIPTION,citation=_CITATION,inputs_description=_KWARGS_DESCRIPTION,features=datasets.Features(
                {
                    'predictions': {'id': datasets.Value('string'		), 'prediction_text': datasets.Value('string'		)},
                    'references': {
                        'id': datasets.Value('string'		),
                        'answers': datasets.features.Sequence(
                            {
                                'text': datasets.Value('string'		),
                                'answer_start': datasets.Value('int32'		),
                            }		),
                    },
                }		),codebase_urls=['https://rajpurkar.github.io/SQuAD-explorer/'],reference_urls=['https://rajpurkar.github.io/SQuAD-explorer/'],)


    def 						snake_case__					(				self			:	str,lowercase_			:	List[Any],lowercase_			:	Dict		)->   List[str]:





        '''simple docstring'''







        A__   				=       {prediction['id']: prediction['prediction_text'] for prediction in predictions}
        A__   				=       [
            {
                'paragraphs': [
                    {
                        'qas': [
                            {
                                'answers': [{'text': answer_text} for answer_text in ref['answers']['text']],
                                'id': ref['id'],
                            }
                            for ref in references
                        ]
                    }
                ]
            }
        ]
        A__   				=       evaluate(dataset=lowercase_,predictions=lowercase_		)
        return score

