





import unittest

from transformers import AlbertTokenizer, AlbertTokenizerFast
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


lowerCAmelCase_						   =    get_tests_dir("""fixtures/spiece.model""")



@require_sentencepiece
@require_tokenizers
class        _lowerCAmelCase    (			UpperCAmelCase_							,    unittest.TestCase							):


  '''simple docstring'''
  a_							:       int      					=AlbertTokenizer
  a_							:       str      					=AlbertTokenizerFast
  a_							:       List[Any]      					=True
  a_							:       Any      					=True
  a_							:       str      					=True







  def UpperCamelCase_ (      self      :					Optional[int]							):


    '''simple docstring'''





    super().setUp()

    # We have a SentencePiece fixture for testing
    _snake_case   :				Optional[int]														=	AlbertTokenizer(UpperCamelCase							)
    tokenizer.save_pretrained(self.tmpdirname							)







  def UpperCamelCase_ (      self      :					List[str]      ,				UpperCamelCase      :					Any							):


    '''simple docstring'''





    _snake_case   :				Tuple														=	'this is a test'
    _snake_case   :				List[str]														=	'this is a test'
    return input_text, output_text







  def UpperCamelCase_ (      self      :					Any							):


    '''simple docstring'''





    _snake_case   :				Dict														=	'<pad>'
    _snake_case   :				str														=	0

    self.assertEqual(self.get_tokenizer()._convert_token_to_id(UpperCamelCase							)      ,				UpperCamelCase							)
    self.assertEqual(self.get_tokenizer()._convert_id_to_token(UpperCamelCase							)      ,				UpperCamelCase							)







  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





    _snake_case   :				Union[str, Any]														=	list(self.get_tokenizer().get_vocab().keys()							)

    self.assertEqual(vocab_keys[0]      ,				'<pad>'							)
    self.assertEqual(vocab_keys[1]      ,				'<unk>'							)
    self.assertEqual(vocab_keys[-1]      ,				'▁eloquent'							)
    self.assertEqual(len(UpperCamelCase							)      ,				3_00_00							)







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    self.assertEqual(self.get_tokenizer().vocab_size      ,				3_00_00							)







  def UpperCamelCase_ (      self      :					List[str]							):


    '''simple docstring'''





    if not self.test_rust_tokenizer:
      return

    _snake_case   :				Dict														=	self.get_tokenizer()
    _snake_case   :				int														=	self.get_rust_tokenizer()

    _snake_case   :				Union[str, Any]														=	'I was born in 92000, and this is falsé.'

    _snake_case   :				Dict														=	tokenizer.tokenize(UpperCamelCase							)
    _snake_case   :				Union[str, Any]														=	rust_tokenizer.tokenize(UpperCamelCase							)
    self.assertListEqual(UpperCamelCase      ,				UpperCamelCase							)

    _snake_case   :				str														=	tokenizer.encode(UpperCamelCase      ,				add_special_tokens=UpperCamelCase							)
    _snake_case   :				int														=	rust_tokenizer.encode(UpperCamelCase      ,				add_special_tokens=UpperCamelCase							)
    self.assertListEqual(UpperCamelCase      ,				UpperCamelCase							)

    _snake_case   :				Union[str, Any]														=	self.get_rust_tokenizer()
    _snake_case   :				str														=	tokenizer.encode(UpperCamelCase							)
    _snake_case   :				List[Any]														=	rust_tokenizer.encode(UpperCamelCase							)
    self.assertListEqual(UpperCamelCase      ,				UpperCamelCase							)







  def UpperCamelCase_ (      self      :					str							):


    '''simple docstring'''





    _snake_case   :				Union[str, Any]														=	AlbertTokenizer(UpperCamelCase      ,				keep_accents=UpperCamelCase							)

    _snake_case   :				Optional[int]														=	tokenizer.tokenize('This is a test'							)
    self.assertListEqual(UpperCamelCase      ,				['▁this', '▁is', '▁a', '▁test']							)

    self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCamelCase							)      ,				[48, 25, 21, 12_89]							)

    _snake_case   :				Union[str, Any]														=	tokenizer.tokenize('I was born in 92000, and this is falsé.'							)
    self.assertListEqual(
        UpperCamelCase      ,				['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', 'é', '.']							)
    _snake_case   :				Dict														=	tokenizer.convert_tokens_to_ids(UpperCamelCase							)
    self.assertListEqual(UpperCamelCase      ,				[31, 23, 3_86, 19, 5_61, 30_50, 15, 17, 48, 25, 82_56, 18, 1, 9]							)

    _snake_case   :				str														=	tokenizer.convert_ids_to_tokens(UpperCamelCase							)
    self.assertListEqual(
        UpperCamelCase      ,				['▁i', '▁was', '▁born', '▁in', '▁9', '2000', ',', '▁and', '▁this', '▁is', '▁fal', 's', '<unk>', '.']      ,				)







  def UpperCamelCase_ (      self      :					int							):


    '''simple docstring'''





    _snake_case   :				Tuple														=	AlbertTokenizer(UpperCamelCase							)

    _snake_case   :				Optional[int]														=	tokenizer.encode('sequence builders'							)
    _snake_case   :				Dict														=	tokenizer.encode('multi-sequence build'							)

    _snake_case   :				int														=	tokenizer.build_inputs_with_special_tokens(UpperCamelCase							)
    _snake_case   :				List[str]														=	tokenizer.build_inputs_with_special_tokens(UpperCamelCase      ,				UpperCamelCase							)

    assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
    assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
        tokenizer.sep_token_id
    ]







  @slow
  def UpperCamelCase_ (      self      :					Optional[Any]							):


    '''simple docstring'''





    _snake_case   :				List[str]														=	{'attention_mask': [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], 'input_ids': [[2, 2_19_70, 13, 5, 60_92, 1_67, 28, 71_03, 21_53, 6_73, 8, 70_28, 1_20_51, 18, 17, 71_03, 21_53, 6_73, 8, 35_15, 1_86_84, 8, 44_61, 6, 19_27, 2_97, 8, 1_20_60, 26_07, 18, 13, 5, 44_61, 15, 1_05_38, 38, 8, 1_35, 15, 8_22, 58, 15, 9_93, 1_03_63, 15, 14_60, 80_05, 44_61, 15, 9_93, 2_55, 23_28, 9, 9, 9, 6, 26, 11_12, 8_16, 32_60, 13, 5, 1_03, 23_77, 6, 17, 11_12, 8_16, 27_82, 13, 5, 1_03, 1_06_41, 6, 29, 84, 25_12, 24_30, 7_82, 1_86_84, 27_61, 19, 8_08, 24_30, 25_56, 17, 8_55, 14_80, 94_77, 40_91, 1_28, 1_17_12, 15, 71_03, 21_53, 6_73, 17, 2_48_83, 99_90, 9, 3], [2, 1_15_02, 25, 10_06, 20, 7_82, 8, 1_18_09, 8_55, 17_32, 1_93_93, 1_86_67, 37, 3_67, 2_10_18, 69, 18_54, 34, 1_18_60, 1_91_24, 27, 1_56, 2_25, 17, 1_93, 41_41, 19, 65, 91_24, 9, 3, 0, 0, 0, 0, 0,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0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]}  # noqa: E501
    # fmt: on

    self.tokenizer_integration_test_util(
        expected_encoding=UpperCamelCase      ,				model_name='albert-base-v2'      ,				revision='6b6560eaf5ff2e250b00c50f380c5389a9c2d82e'      ,				)











import argparse
import os

import transformers

from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS
from .utils import logging


logging.set_verbosity_info()

lowerCAmelCase_						   =    logging.get_logger(__name__)


lowerCAmelCase_						   =    {name: getattr(transformers, name + """Fast""") for name in SLOW_TO_FAST_CONVERTERS}


def     lowerCamelCase_	(      lowerCAmelCase:	List[Any]    ,							lowerCAmelCase:	Optional[Any]    ,							lowerCAmelCase:	Dict    ,							lowerCAmelCase:	Union[str, Any]				)->		Optional[int]:
  if tokenizer_name is not None and tokenizer_name not in TOKENIZER_CLASSES:
    raise ValueError(F"""Unrecognized tokenizer name, should be one of {list(TOKENIZER_CLASSES.keys()				)}."""				)

  if tokenizer_name is None:
    _snake_case   :				Tuple														=	TOKENIZER_CLASSES
  else:
    _snake_case   :				Union[str, Any]														=	{tokenizer_name: getattr(lowerCAmelCase    ,							tokenizer_name + 'Fast'				)}

  logger.info(F"""Loading tokenizer classes: {tokenizer_names}"""				)

  for tokenizer_name in tokenizer_names:
    _snake_case   :				Dict														=	TOKENIZER_CLASSES[tokenizer_name]

    _snake_case   :				Optional[Any]														=	True
    if checkpoint_name is None:
      _snake_case   :				Union[str, Any]														=	list(tokenizer_class.max_model_input_sizes.keys()				)
    else:
      _snake_case   :				Optional[int]														=	[checkpoint_name]

    logger.info(F"""For tokenizer {tokenizer_class.__class__.__name__} loading checkpoints: {checkpoint_names}"""				)

    for checkpoint in checkpoint_names:
      logger.info(F"""Loading {tokenizer_class.__class__.__name__} {checkpoint}"""				)

      # Load tokenizer
      _snake_case   :				str														=	tokenizer_class.from_pretrained(lowerCAmelCase    ,							force_download=lowerCAmelCase				)

      # Save fast tokenizer
      logger.info(F"""Save fast tokenizer to {dump_path} with prefix {checkpoint} add_prefix {add_prefix}"""				)

      # For organization names we create sub-directories
      if "/" in checkpoint:
        _snake_case							,					_snake_case   :				Tuple														=	checkpoint.split('/'				)
        _snake_case   :				int														=	os.path.join(lowerCAmelCase    ,							lowerCAmelCase				)
      elif add_prefix:
        _snake_case   :				Dict														=	checkpoint
        _snake_case   :				Optional[Any]														=	dump_path
      else:
        _snake_case   :				str														=	None
        _snake_case   :				Union[str, Any]														=	dump_path

      logger.info(F"""=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}"""				)

      if checkpoint in list(tokenizer.pretrained_vocab_files_map.values()				)[0]:
        _snake_case   :				Optional[Any]														=	list(tokenizer.pretrained_vocab_files_map.values()				)[0][checkpoint]
        _snake_case   :				Optional[int]														=	file_path.split(lowerCAmelCase				)[-1][0]
        if next_char == "/":
          _snake_case   :				Union[str, Any]														=	os.path.join(lowerCAmelCase    ,							lowerCAmelCase				)
          _snake_case   :				str														=	None

        logger.info(F"""=> {dump_path_full} with prefix {checkpoint_prefix_name}, add_prefix {add_prefix}"""				)

      _snake_case   :				Optional[int]														=	tokenizer.save_pretrained(
          lowerCAmelCase    ,							legacy_format=lowerCAmelCase    ,							filename_prefix=lowerCAmelCase				)
      logger.info(F"""=> File names {file_names}"""				)

      for file_name in file_names:
        if not file_name.endswith('tokenizer.json'				):
          os.remove(lowerCAmelCase				)
          logger.info(F"""=> removing {file_name}"""				)


if __name__ == "__main__":
      lowerCAmelCase_						   =    argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          """--dump_path""", default=None, type=str, required=True, help="""Path to output generated fast tokenizer files."""
      )
      parser.add_argument(
          """--tokenizer_name""",
          default=None,
          type=str,
          help=(
              F"""Optional tokenizer type selected in the list of {list(TOKENIZER_CLASSES.keys())}. If not given, will """
              """download and convert all the checkpoints from AWS."""
          ),
      )
      parser.add_argument(
          """--checkpoint_name""",
          default=None,
          type=str,
          help="""Optional checkpoint name. If not given, will download and convert the canonical checkpoints from AWS.""",
      )
      parser.add_argument(
          """--force_download""",
          action="""store_true""",
          help="""Re-download checkpoints.""",
      )
      lowerCAmelCase_						   =    parser.parse_args()

      convert_slow_checkpoint_to_fast(args.tokenizer_name, args.checkpoint_name, args.dump_path, args.force_download)




