"""simple docstring"""



import json
import os
import unittest

from transformers.models.gptsan_japanese.tokenization_gptsan_japanese import (
    VOCAB_FILES_NAMES,
    GPTSanJapaneseTokenizer,
)
from transformers.testing_utils import require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin






@require_tokenizers
class 				snake_case__     (   snake_case_,		unittest.TestCase   ):
 _snake_case   :			Tuple           =		GPTSanJapaneseTokenizer
 _snake_case   :			List[Any]           =		False
 _snake_case   :			List[Any]           =		{"""do_clean_text""": False, """add_prefix_space""": False}


 def 		a__ (				self		):
        super().setUp()

        # fmt: off
        __a     				=  ["こん", "こんに", "にちは", "ばんは", "世界,㔺界", "、", "。", "<BR>", "<SP>", "<TAB>", "<URL>", "<EMAIL>", "<TEL>", "<DATE>", "<PRICE>", "<BLOCK>", "<KIGOU>", "<U2000U2BFF>", "<|emoji1|>", "<unk>", "<|bagoftoken|>", "<|endoftext|>"]
        # fmt: on
        __a     				=  {"emoji": {"\ud83d\ude00": "<|emoji1|>"}, "emoji_inv": {"<|emoji1|>": "\ud83d\ude00"}}  # 😀
        __a     				=  {"unk_token": "<unk>"}

        __a     				=  os.path.join(self.tmpdirname  ,						VOCAB_FILES_NAMES["vocab_file"]		)
        __a     				=  os.path.join(self.tmpdirname  ,						VOCAB_FILES_NAMES["emoji_file"]		)
        with open(self.vocab_file  ,						"w"  ,						encoding="utf-8"		) as vocab_writer:
               vocab_writer.write("".join([x + "\n" for x in vocab_tokens]		)		)
        with open(self.emoji_file  ,						"w"		) as emoji_writer:
               emoji_writer.write(json.dumps(_lowercase		)		)


 def 		a__ (				self  ,						**lowerCamelCase		):
        kwargs.update(self.special_tokens_map		)
        return GPTSanJapaneseTokenizer.from_pretrained(self.tmpdirname  ,						**_lowercase		)


 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  "こんにちは、世界。 \nこんばんは、㔺界。😀"
        __a     				=  "こんにちは、世界。 \nこんばんは、世界。😀"
        return input_text, output_text


 def 		a__ (				self  ,						lowerCamelCase		):
        __a				, __a     				=  self.get_input_output_texts(_lowercase		)
        __a     				=  tokenizer.encode(_lowercase  ,						add_special_tokens=_lowercase		)
        __a     				=  tokenizer.decode(_lowercase  ,						clean_up_tokenization_spaces=_lowercase		)
        return text, ids


 def 		a__ (				self		):
        pass  # TODO add if relevant


 def 		a__ (				self		):
        pass  # TODO add if relevant


 def 		a__ (				self		):
        pass  # TODO add if relevant


 def 		a__ (				self		):
        __a     				=  self.get_tokenizer()

        # Testing tokenization
        __a     				=  "こんにちは、世界。　こんばんは、㔺界。"
        __a     				=  ["こん", "にちは", "、", "世界", "。", "<SP>", "こん", "ばんは", "、", "㔺界", "。"]
        __a     				=  tokenizer.tokenize(_lowercase		)
        self.assertListEqual(_lowercase  ,						_lowercase		)

        # Testing conversion to ids without special tokens
        __a     				=  [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6]
        __a     				=  tokenizer.convert_tokens_to_ids(_lowercase		)
        self.assertListEqual(_lowercase  ,						_lowercase		)

        # Testing conversion to ids with special tokens
        __a     				=  tokens + [tokenizer.unk_token]
        __a     				=  [0, 2, 5, 4, 6, 8, 0, 3, 5, 4, 6, 19]
        __a     				=  tokenizer.convert_tokens_to_ids(_lowercase		)
        self.assertListEqual(_lowercase  ,						_lowercase		)


 def 		a__ (				self		):
        __a     				=  self.get_tokenizer()

        # Testing tokenization
        __a     				=  "こんにちは、<|bagoftoken|>世界。こんばんは、<|bagoftoken|>㔺界。"
        __a     				=  "こんにちは、、、、世界。こんばんは、、、、世界。"
        __a     				=  tokenizer.encode(_lowercase		)
        __a     				=  tokenizer.decode(_lowercase		)
        self.assertEqual(_lowercase  ,						_lowercase		)


 @slow
 def 		a__ (				self		):
        __a     				=  self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"		)

        # Testing tokenization
        __a     				=  "こんにちは、世界。"
        __a     				=  "こんばんは、㔺界。😀"
        __a     				=  "こんにちは、世界。こんばんは、世界。😀"
        __a     				=  tokenizer.encode(prefix_text + input_text		)
        __a     				=  tokenizer.encode(""  ,						prefix_text=prefix_text + input_text		)
        __a     				=  tokenizer.encode(_lowercase  ,						prefix_text=_lowercase		)
        __a     				=  tokenizer.decode(_lowercase		)
        __a     				=  tokenizer.decode(_lowercase		)
        __a     				=  tokenizer.decode(_lowercase		)
        self.assertEqual(_lowercase  ,						_lowercase		)
        self.assertEqual(_lowercase  ,						_lowercase		)
        self.assertEqual(_lowercase  ,						_lowercase		)


 @slow
 def 		a__ (				self		):
        __a     				=  self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"		)

        # Testing tokenization
        __a     				=  "こんにちは、世界。"
        __a     				=  "こんばんは、㔺界。😀"

        __a     				=  len(tokenizer.encode(_lowercase		)		) - 2
        __a     				=  len(tokenizer.encode(_lowercase		)		) - 2

        __a     				=  [1] + [0] * (len_prefix + len_text + 1)
        __a     				=  [1] * (len_prefix + len_text + 1) + [0]
        __a     				=  [1] + [1] * (len_prefix) + [0] * (len_text + 1)

        __a     				=  tokenizer(prefix_text + input_text		).token_type_ids
        __a     				=  tokenizer(""  ,						prefix_text=prefix_text + input_text		).token_type_ids
        __a     				=  tokenizer(_lowercase  ,						prefix_text=_lowercase		).token_type_ids
        self.assertListEqual(_lowercase  ,						_lowercase		)
        self.assertListEqual(_lowercase  ,						_lowercase		)
        self.assertListEqual(_lowercase  ,						_lowercase		)


 @slow
 def 		a__ (				self		):
        __a     				=  self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"		)

        __a     				=  tokenizer.encode("あンいワ"		)
        __a     				=  tokenizer.encode(""  ,						prefix_text="あンいワ"		)
        __a     				=  tokenizer.encode("いワ"  ,						prefix_text="あン"		)

        self.assertEqual(tokenizer.decode(_lowercase		)  ,						tokenizer.decode(_lowercase		)		)
        self.assertEqual(tokenizer.decode(_lowercase		)  ,						tokenizer.decode(_lowercase		)		)
        self.assertNotEqual(_lowercase  ,						_lowercase		)
        self.assertNotEqual(_lowercase  ,						_lowercase		)
        self.assertEqual(x_token_a[1]  ,						x_token_a[-1]		)  # SEG token
        self.assertEqual(x_token_a[1]  ,						x_token_a[3]		)  # SEG token


 @slow
 def 		a__ (				self		):
        __a     				=  self.tokenizer_class.from_pretrained("Tanrei/GPTSAN-japanese"		)

        __a     				=  [["武田信玄", "は、"], ["織田信長", "の配下の、"]]
        __a     				=  tokenizer(_lowercase  ,						padding=_lowercase		)
        __a     				=  tokenizer.batch_encode_plus(_lowercase  ,						padding=_lowercase		)

        # fmt: off
        __a     				=  [[35993, 8640, 25948, 35998, 30647, 35675, 35999, 35999], [35993, 10382, 9868, 35998, 30646, 9459, 30646, 35675]]
        __a     				=  [[1, 1, 1, 0, 0, 0, 0, 0], [1, 1, 1, 0, 0, 0, 0, 0]]
        __a     				=  [[1, 1, 1, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1]]
        # fmt: on
        self.assertListEqual(x_token.input_ids  ,						_lowercase		)
        self.assertListEqual(x_token.token_type_ids  ,						_lowercase		)
        self.assertListEqual(x_token.attention_mask  ,						_lowercase		)
        self.assertListEqual(x_token_a.input_ids  ,						_lowercase		)
        self.assertListEqual(x_token_a.token_type_ids  ,						_lowercase		)
        self.assertListEqual(x_token_a.attention_mask  ,						_lowercase		)


 def 		a__ (				self		):
        # Intentionally convert some words to accommodate character fluctuations unique to Japanese
        pass






 def 		a__ (				self		):
        # tokenizer has no padding token
        pass

"""simple docstring"""



import collections
import os
from typing import List, Optional, Tuple

from transformers.utils import is_jieba_available, requires_backends


if is_jieba_available():
   import jieba

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


SCREAMING_SNAKE_CASE__:Tuple			=	logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__:List[Any]			=	{"""vocab_file""": """vocab.txt"""}

SCREAMING_SNAKE_CASE__:Optional[int]			=	{
    """vocab_file""": {
        """openbmb/cpm-ant-10b""": """https://huggingface.co/openbmb/cpm-ant-10b/blob/main/vocab.txt""",
    },
}

SCREAMING_SNAKE_CASE__:Tuple			=	{
    """openbmb/cpm-ant-10b""": 1024,
}



def 					_lowerCamelCase(					a							):
       __a     				=  collections.OrderedDict()
       with open(a ,  "r" ,  encoding="utf-8"							) as reader:
              __a     				=  reader.readlines()
       for index, token in enumerate(a							):
              __a     				=  token.rstrip("\n"							)
              __a     				=  index
       return vocab






class 				snake_case__     (   snake_case_   ):


 def __init__(				self  ,						lowerCamelCase  ,						lowerCamelCase="<unk>"  ,						lowerCamelCase=200		):
        __a     				=  vocab
        __a     				=  unk_token
        __a     				=  max_input_chars_per_word






 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  list(lowerCamelCase		)
        if len(lowerCamelCase		) > self.max_input_chars_per_word:
               return [self.unk_token]

        __a     				=  0
        __a     				=  []
        while start < len(lowerCamelCase		):
               __a     				=  len(lowerCamelCase		)
               __a     				=  None
               while start < end:
                      __a     				=  "".join(chars[start:end]		)
                      if substr in self.vocab:
                             __a     				=  substr
                             break
                      end -= 1
               if cur_substr is None:
                      sub_tokens.append(self.unk_token		)
                      start += 1
               else:
                      sub_tokens.append(lowerCamelCase		)
                      __a     				=  end

        return sub_tokens






class 				snake_case__     (   snake_case_   ):
 _snake_case   :			Optional[int]           =		VOCAB_FILES_NAMES
 _snake_case   :			Optional[int]           =		PRETRAINED_VOCAB_FILES_MAP
 _snake_case   :			Optional[Any]           =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _snake_case   :			int           =		["""input_ids""", """attention_mask"""]
 _snake_case   :			int           =		False


 def __init__(				self  ,						lowerCamelCase  ,						lowerCamelCase="<d>"  ,						lowerCamelCase="</d>"  ,						lowerCamelCase="<s>"  ,						lowerCamelCase="</s>"  ,						lowerCamelCase="<pad>"  ,						lowerCamelCase="<unk>"  ,						lowerCamelCase="</n>"  ,						lowerCamelCase="</_>"  ,						lowerCamelCase="left"  ,						**lowerCamelCase  ,						):
        requires_backends(self  ,						["jieba"]		)
        super().__init__(
            bod_token=lowerCamelCase  ,						eod_token=lowerCamelCase  ,						bos_token=lowerCamelCase  ,						eos_token=lowerCamelCase  ,						pad_token=lowerCamelCase  ,						unk_token=lowerCamelCase  ,						line_token=lowerCamelCase  ,						space_token=lowerCamelCase  ,						padding_side=lowerCamelCase  ,						**lowerCamelCase  ,						)
        __a     				=  bod_token
        __a     				=  eod_token
        __a     				=  load_vocab(lowerCamelCase		)
        __a     				=  self.encoder[space_token]
        __a     				=  self.encoder[line_token]

        del self.encoder[space_token]
        del self.encoder[line_token]

        __a     				=  collections.OrderedDict(sorted(self.encoder.items()  ,						key=lambda lowerCamelCase		: x[1]		)		)
        __a     				=  {v: k for k, v in self.encoder.items()}

        __a     				=  WordpieceTokenizer(vocab=self.encoder  ,						unk_token=self.unk_token		)


 @property
 def 		a__ (				self		):
        return self.encoder[self.bod_token]


 @property
 def 		a__ (				self		):
        return self.encoder[self.eod_token]


 @property
 def 		a__ (				self		):
        return self.encoder["\n"]


 @property
 def 		a__ (				self		):
        return len(self.encoder		)


 def 		a__ (				self		):
        return dict(self.encoder  ,						**self.added_tokens_encoder		)


 def 		a__ (				self  ,						lowerCamelCase		):
        __a     				=  []
        for x in jieba.cut(lowerCamelCase  ,						cut_all=lowerCamelCase		):
               output_tokens.extend(self.wordpiece_tokenizer.tokenize(lowerCamelCase		)		)
        return output_tokens


 def 		a__ (				self  ,						lowerCamelCase  ,						**lowerCamelCase		):
        __a     				=  [i for i in token_ids if i >= 0]
        __a     				=  [
            x for x in token_ids if x != self.pad_token_id and x != self.eos_token_id and x != self.bos_token_id
        ]
        return super()._decode(lowerCamelCase  ,						**lowerCamelCase		)


 def 		a__ (				self  ,						lowerCamelCase		):
        return token in self.encoder


 def 		a__ (				self  ,						lowerCamelCase		):
        return "".join(lowerCamelCase		)


 def 		a__ (				self  ,						lowerCamelCase		):
        return self.encoder.get(lowerCamelCase  ,						self.encoder.get(self.unk_token		)		)


 def 		a__ (				self  ,						lowerCamelCase		):
        return self.decoder.get(lowerCamelCase  ,						self.unk_token		)


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None		):
        if os.path.isdir(lowerCamelCase		):
               __a     				=  os.path.join(
                   lowerCamelCase  ,						(filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]		)
        else:
               __a     				=  (filename_prefix + "-" if filename_prefix else "") + save_directory
        __a     				=  0
        if " " in self.encoder:
               __a     				=  self.encoder[" "]
               del self.encoder[" "]
        if "\n" in self.encoder:
               __a     				=  self.encoder["\n"]
               del self.encoder["\n"]
        __a     				=  collections.OrderedDict(sorted(self.encoder.items()  ,						key=lambda lowerCamelCase		: x[1]		)		)
        with open(lowerCamelCase  ,						"w"  ,						encoding="utf-8"		) as writer:
               for token, token_index in self.encoder.items():
                      if index != token_index:
                             logger.warning(
                                 F"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
                                 " Please check that the vocabulary is not corrupted!"		)
                             __a     				=  token_index
                      writer.write(token + "\n"		)
                      index += 1
        return (vocab_file,)


 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None		):
        if token_ids_a is None:
               return [self.bos_token_id] + token_ids_a
        return [self.bos_token_id] + token_ids_a + [self.bos_token_id] + token_ids_a






 def 		a__ (				self  ,						lowerCamelCase  ,						lowerCamelCase = None  ,						lowerCamelCase = False		):

        if already_has_special_tokens:
               return super().get_special_tokens_mask(
                   token_ids_a=lowerCamelCase  ,						token_ids_a=lowerCamelCase  ,						already_has_special_tokens=lowerCamelCase		)

        if token_ids_a is not None:
               return [1] + ([0] * len(lowerCamelCase		)) + [1] + ([0] * len(lowerCamelCase		))
        return [1] + ([0] * len(lowerCamelCase		))
