






'''simple docstring'''




from copy import deepcopy
from unittest.case import TestCase

import pytest

from datasets.arrow_dataset import Dataset
from datasets.features import Audio, ClassLabel, Features, Image, Sequence, Value
from datasets.info import DatasetInfo
from datasets.tasks import (
    AudioClassification,
    AutomaticSpeechRecognition,
    ImageClassification,
    LanguageModeling,
    QuestionAnsweringExtractive,
    Summarization,
    TextClassification,
    task_template_from_dict,
)
from datasets.utils.py_utils import asdict


lowerCamelCase_							      =      {
    '''id''': '''5733be284776f41900661182''',
    '''title''': '''University_of_Notre_Dame''',
    '''context''': '''Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.''',
    '''question''': '''To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?''',
    '''answers''': {'''text''': ['''Saint Bernadette Soubirous'''], '''answer_start''': [5_15]},
}

@pytest.mark.parametrize(
    "task_cls"     ,						[
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]     ,						)
def       __lowercase		(    __lowercase ) ->		Tuple:

      '''simple docstring'''


      _A      =			task_cls()
      _A      =			asdict(__lowercase )
      _A      =			task_template_from_dict(__lowercase )
      assert task == reloaded

class      _UpperCAmelCase    :




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			Dict	):





         '''simple docstring'''





         _A      =			LanguageModeling(text_column="input_text"	)
         assert {"input_text": "text"} == task.column_mapping

   def 		lowerCAmelCase				(	self				:			Optional[int]	):





         '''simple docstring'''





         _A      =			Features({"text": Value("string"	)}	)
         _A      =			{"text_column": "input_text"}
         _A      =			LanguageModeling.from_dict(__UpperCAmelCase	)
         assert "language-modeling" == task.task
         assert input_schema == task.input_schema





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			Dict	):





         '''simple docstring'''





         _A      =			sorted(["pos", "neg"]	)

   def 		lowerCAmelCase				(	self				:			Optional[Any]	):





         '''simple docstring'''





         _A      =			TextClassification(text_column="input_text"   ,   label_column="input_label"	)
         self.assertDictEqual({"input_text": "text", "input_label": "labels"}   ,   task.column_mapping	)

   def 		lowerCAmelCase				(	self				:			List[str]	):





         '''simple docstring'''





         _A      =			Features({"text": Value("string"	)}	)
         # Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
         _A      =			Features({"labels": ClassLabel}	)
         _A      =			{"text_column": "input_text", "label_column": "input_labels"}
         _A      =			TextClassification.from_dict(__UpperCAmelCase	)
         self.assertEqual("text-classification"   ,   task.task	)
         self.assertEqual(__UpperCAmelCase   ,   task.input_schema	)
         self.assertEqual(__UpperCAmelCase   ,   task.label_schema	)

   def 		lowerCAmelCase				(	self				:			Tuple	):





         '''simple docstring'''





         _A      =			TextClassification(text_column="input_text"   ,   label_column="input_label"	)
         self.assertEqual(task.label_schema["labels"]   ,   __UpperCAmelCase	)
         _A      =			task.align_with_features(Features({"input_label": ClassLabel(names=self.labels	)}	)	)
         self.assertEqual(task.label_schema["labels"]   ,   ClassLabel(names=self.labels	)	)





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			Dict	):





         '''simple docstring'''





         _A      =			QuestionAnsweringExtractive(
             context_column="input_context"   ,   question_column="input_question"   ,   answers_column="input_answers"	)
         self.assertDictEqual(
             {"input_context": "context", "input_question": "question", "input_answers": "answers"}   ,   task.column_mapping	)

   def 		lowerCAmelCase				(	self				:			Any	):





         '''simple docstring'''





         _A      =			Features({"question": Value("string"	), "context": Value("string"	)}	)
         _A      =			Features(
             {
                 "answers": Sequence(
                     {
                         "text": Value("string"	),
                         "answer_start": Value("int32"	),
                     }	)
             }	)
         _A      =			{
             "context_column": "input_input_context",
             "question_column": "input_question",
             "answers_column": "input_answers",
         }
         _A      =			QuestionAnsweringExtractive.from_dict(__UpperCAmelCase	)
         self.assertEqual("question-answering-extractive"   ,   task.task	)
         self.assertEqual(__UpperCAmelCase   ,   task.input_schema	)
         self.assertEqual(__UpperCAmelCase   ,   task.label_schema	)





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			List[str]	):





         '''simple docstring'''





         _A      =			Summarization(text_column="input_text"   ,   summary_column="input_summary"	)
         self.assertDictEqual({"input_text": "text", "input_summary": "summary"}   ,   task.column_mapping	)

   def 		lowerCAmelCase				(	self				:			Dict	):





         '''simple docstring'''





         _A      =			Features({"text": Value("string"	)}	)
         _A      =			Features({"summary": Value("string"	)}	)
         _A      =			{"text_column": "input_text", "summary_column": "input_summary"}
         _A      =			Summarization.from_dict(__UpperCAmelCase	)
         self.assertEqual("summarization"   ,   task.task	)
         self.assertEqual(__UpperCAmelCase   ,   task.input_schema	)
         self.assertEqual(__UpperCAmelCase   ,   task.label_schema	)





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			List[str]	):





         '''simple docstring'''





         _A      =			AutomaticSpeechRecognition(audio_column="input_audio"   ,   transcription_column="input_transcription"	)
         self.assertDictEqual({"input_audio": "audio", "input_transcription": "transcription"}   ,   task.column_mapping	)

   def 		lowerCAmelCase				(	self				:			Tuple	):





         '''simple docstring'''





         _A      =			Features({"audio": Audio()}	)
         _A      =			Features({"transcription": Value("string"	)}	)
         _A      =			{
             "audio_column": "input_audio",
             "transcription_column": "input_transcription",
         }
         _A      =			AutomaticSpeechRecognition.from_dict(__UpperCAmelCase	)
         self.assertEqual("automatic-speech-recognition"   ,   task.task	)
         self.assertEqual(__UpperCAmelCase   ,   task.input_schema	)
         self.assertEqual(__UpperCAmelCase   ,   task.label_schema	)





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			List[Any]	):





         '''simple docstring'''





         _A      =			sorted(["pos", "neg"]	)

   def 		lowerCAmelCase				(	self				:			List[str]	):





         '''simple docstring'''





         _A      =			AudioClassification(audio_column="input_audio"   ,   label_column="input_label"	)
         self.assertDictEqual({"input_audio": "audio", "input_label": "labels"}   ,   task.column_mapping	)

   def 		lowerCAmelCase				(	self				:			List[Any]	):





         '''simple docstring'''





         _A      =			Features({"audio": Audio()}	)
         _A      =			Features({"labels": ClassLabel}	)
         _A      =			{
             "audio_column": "input_image",
             "label_column": "input_label",
         }
         _A      =			AudioClassification.from_dict(__UpperCAmelCase	)
         self.assertEqual("audio-classification"   ,   task.task	)
         self.assertEqual(__UpperCAmelCase   ,   task.input_schema	)
         self.assertEqual(__UpperCAmelCase   ,   task.label_schema	)

   def 		lowerCAmelCase				(	self				:			str	):





         '''simple docstring'''





         _A      =			AudioClassification(audio_column="input_audio"   ,   label_column="input_label"	)
         self.assertEqual(task.label_schema["labels"]   ,   __UpperCAmelCase	)
         _A      =			task.align_with_features(Features({"input_label": ClassLabel(names=self.labels	)}	)	)
         self.assertEqual(task.label_schema["labels"]   ,   ClassLabel(names=self.labels	)	)





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			List[str]	):





         '''simple docstring'''





         _A      =			sorted(["pos", "neg"]	)

   def 		lowerCAmelCase				(	self				:			Union[str, Any]	):





         '''simple docstring'''





         _A      =			ImageClassification(image_column="input_image"   ,   label_column="input_label"	)
         self.assertDictEqual({"input_image": "image", "input_label": "labels"}   ,   task.column_mapping	)

   def 		lowerCAmelCase				(	self				:			Dict	):





         '''simple docstring'''





         _A      =			Features({"image": Image()}	)
         _A      =			Features({"labels": ClassLabel}	)
         _A      =			{
             "image_column": "input_image",
             "label_column": "input_label",
         }
         _A      =			ImageClassification.from_dict(__UpperCAmelCase	)
         self.assertEqual("image-classification"   ,   task.task	)
         self.assertEqual(__UpperCAmelCase   ,   task.input_schema	)
         self.assertEqual(__UpperCAmelCase   ,   task.label_schema	)

   def 		lowerCAmelCase				(	self				:			Union[str, Any]	):





         '''simple docstring'''





         _A      =			ImageClassification(image_column="input_image"   ,   label_column="input_label"	)
         self.assertEqual(task.label_schema["labels"]   ,   __UpperCAmelCase	)
         _A      =			task.align_with_features(Features({"input_label": ClassLabel(names=self.labels	)}	)	)
         self.assertEqual(task.label_schema["labels"]   ,   ClassLabel(names=self.labels	)	)





class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   def 		lowerCAmelCase				(	self				:			Dict	):





         '''simple docstring'''





         _A      =			DatasetInfo(task_templates=QuestionAnsweringExtractive()	)
         _A      =			Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}   ,   info=__UpperCAmelCase	)
         assert isinstance(dataset.info.task_templates   ,   __UpperCAmelCase	)
         assert len(dataset.info.task_templates	) == 1

         def keep_task(__UpperCAmelCase				:			Optional[Any]	):
               return x

         def dont_keep_task(__UpperCAmelCase				:			Tuple	):
               _A      =			deepcopy(__UpperCAmelCase	)
               _A      =			0
               return out

         _A      =			dataset.map(__UpperCAmelCase	)
         assert mapped_dataset.info.task_templates == dataset.info.task_templates
         # reload from cache
         _A      =			dataset.map(__UpperCAmelCase	)
         assert mapped_dataset.info.task_templates == dataset.info.task_templates

         _A      =			dataset.map(__UpperCAmelCase	)
         assert mapped_dataset.info.task_templates == []
         # reload from cache
         _A      =			dataset.map(__UpperCAmelCase	)
         assert mapped_dataset.info.task_templates == []

   def 		lowerCAmelCase				(	self				:			Optional[Any]	):





         '''simple docstring'''





         _A      =			Features({"text": Value("string"	), "label": ClassLabel(names=("pos", "neg")	)}	)
         _A      =			TextClassification(text_column="text"   ,   label_column="label"	)
         _A      =			DatasetInfo(features=__UpperCAmelCase   ,   task_templates=__UpperCAmelCase	)
         _A      =			Dataset.from_dict({"text": ["A sentence."], "label": ["pos"]}   ,   info=__UpperCAmelCase	)

         def process(__UpperCAmelCase				:			Any	):
               return example

         _A      =			dataset.remove_columns("label"	)
         _A      =			modified_dataset.map(__UpperCAmelCase	)
         assert mapped_dataset.info.task_templates == []













'''simple docstring'''




import os
from shutil import copyfile
from typing import Optional, Tuple

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
    from .tokenization_reformer import ReformerTokenizer
else:
    lowerCamelCase_							      =      None


lowerCamelCase_							      =      logging.get_logger(__name__)


lowerCamelCase_							      =      '''▁'''

lowerCamelCase_							      =      {'''vocab_file''': '''spiece.model''', '''tokenizer_file''': '''tokenizer.json'''}

lowerCamelCase_							      =      {
    '''vocab_file''': {
        '''google/reformer-crime-and-punishment''': (
            '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model'''
        )
    },
    '''tokenizer_file''': {
        '''google/reformer-crime-and-punishment''': (
            '''https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/tokenizer.json'''
        )
    },
}

lowerCamelCase_							      =      {
    '''google/reformer-crime-and-punishment''': 52_42_88,
}

class      _UpperCAmelCase    (  snake_case_		):




   """simple docstring"""

   snake_case					       =		VOCAB_FILES_NAMES
   snake_case					       =		PRETRAINED_VOCAB_FILES_MAP
   snake_case					       =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   snake_case					       =		['''input_ids''', '''attention_mask''']
   snake_case					       =		ReformerTokenizer

   def __init__(	self				:			List[str]   ,   __UpperCAmelCase				:			List[str]=None   ,   __UpperCAmelCase				:			Any=None   ,   __UpperCAmelCase				:			List[Any]="</s>"   ,   __UpperCAmelCase				:			Optional[Any]="<unk>"   ,   __UpperCAmelCase				:			List[Any]=[]   ,   **__UpperCAmelCase				:			int   ,   ):





         '''simple docstring'''





         super().__init__(
             __UpperCAmelCase   ,   tokenizer_file=__UpperCAmelCase   ,   eos_token=__UpperCAmelCase   ,   unk_token=__UpperCAmelCase   ,   additional_special_tokens=__UpperCAmelCase   ,   **__UpperCAmelCase   ,   )

         _A      =			vocab_file
         _A      =			False if not self.vocab_file else True

   def 		lowerCAmelCase				(	self				:			Any   ,   __UpperCAmelCase				:			str   ,   __UpperCAmelCase				:			Optional[str] = None	):





         '''simple docstring'''





         if not self.can_save_slow_tokenizer:
               raise ValueError(
                   "Your fast tokenizer does not have the necessary information to save the vocabulary for a slow "
                   "tokenizer."	)

         if not os.path.isdir(__UpperCAmelCase	):
               logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''	)
               return
         _A      =			os.path.join(
             __UpperCAmelCase   ,   (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]	)

         if os.path.abspath(self.vocab_file	) != os.path.abspath(__UpperCAmelCase	):
               copyfile(self.vocab_file   ,   __UpperCAmelCase	)

         return (out_vocab_file,)





