




import unittest

import numpy as np

from transformers.testing_utils import require_flax, require_tf, require_torch
from transformers.utils import (
    expand_dims,
    flatten_dict,
    is_flax_available,
    is_tf_available,
    is_torch_available,
    reshape,
    squeeze,
    transpose,
)


if is_flax_available():
 import jax.numpy as jnp

if is_tf_available():
 import tensorflow as tf

if is_torch_available():
 import torch
class 						_A					(  unittest.TestCase      ):






  def 					__a			(  self		:	List[Any]      )		->    str:






       """simple docstring"""







       lowercase					:   List[str]								= {
           '''task_specific_params''': {
               '''summarization''': {'''length_penalty''': 1.0, '''max_length''': 128, '''min_length''': 12, '''num_beams''': 4},
               '''summarization_cnn''': {'''length_penalty''': 2.0, '''max_length''': 142, '''min_length''': 56, '''num_beams''': 4},
               '''summarization_xsum''': {'''length_penalty''': 1.0, '''max_length''': 62, '''min_length''': 11, '''num_beams''': 6},
           }
       }
       lowercase					:   List[str]								= {
           '''task_specific_params.summarization.length_penalty''': 1.0,
           '''task_specific_params.summarization.max_length''': 128,
           '''task_specific_params.summarization.min_length''': 12,
           '''task_specific_params.summarization.num_beams''': 4,
           '''task_specific_params.summarization_cnn.length_penalty''': 2.0,
           '''task_specific_params.summarization_cnn.max_length''': 142,
           '''task_specific_params.summarization_cnn.min_length''': 56,
           '''task_specific_params.summarization_cnn.num_beams''': 4,
           '''task_specific_params.summarization_xsum.length_penalty''': 1.0,
           '''task_specific_params.summarization_xsum.max_length''': 62,
           '''task_specific_params.summarization_xsum.min_length''': 11,
           '''task_specific_params.summarization_xsum.num_beams''': 6,
       }

       self.assertEqual(flatten_dict(a__      )      ,						a__      )






  def 					__a			(  self		:	Dict      )		->    Dict:






       """simple docstring"""







       lowercase					:   str								= np.random.randn(3      ,						4      )
       self.assertTrue(np.allclose(transpose(a__      )      ,						x.transpose()      )      )

       lowercase					:   Union[str, Any]								= np.random.randn(3      ,						4      ,						5      )
       self.assertTrue(np.allclose(transpose(a__      ,						axes=(1, 2, 0)      )      ,						x.transpose((1, 2, 0)      )      )      )






  @require_torch
  def 					__a			(  self		:	Optional[Any]      )		->    int:






       """simple docstring"""







       lowercase					:   List[str]								= np.random.randn(3      ,						4      )
       lowercase					:   Dict								= torch.tensor(a__      )
       self.assertTrue(np.allclose(transpose(a__      )      ,						transpose(a__      ).numpy()      )      )

       lowercase					:   int								= np.random.randn(3      ,						4      ,						5      )
       lowercase					:   Union[str, Any]								= torch.tensor(a__      )
       self.assertTrue(np.allclose(transpose(a__      ,						axes=(1, 2, 0)      )      ,						transpose(a__      ,						axes=(1, 2, 0)      ).numpy()      )      )






  @require_tf
  def 					__a			(  self		:	Any      )		->    Union[str, Any]:






       """simple docstring"""







       lowercase					:   List[Any]								= np.random.randn(3      ,						4      )
       lowercase					:   List[str]								= tf.constant(a__      )
       self.assertTrue(np.allclose(transpose(a__      )      ,						transpose(a__      ).numpy()      )      )

       lowercase					:   Dict								= np.random.randn(3      ,						4      ,						5      )
       lowercase					:   Dict								= tf.constant(a__      )
       self.assertTrue(np.allclose(transpose(a__      ,						axes=(1, 2, 0)      )      ,						transpose(a__      ,						axes=(1, 2, 0)      ).numpy()      )      )






  @require_flax
  def 					__a			(  self		:	Any      )		->    List[Any]:






       """simple docstring"""







       lowercase					:   str								= np.random.randn(3      ,						4      )
       lowercase					:   List[str]								= jnp.array(a__      )
       self.assertTrue(np.allclose(transpose(a__      )      ,						np.asarray(transpose(a__      )      )      )      )

       lowercase					:   Optional[Any]								= np.random.randn(3      ,						4      ,						5      )
       lowercase					:   str								= jnp.array(a__      )
       self.assertTrue(np.allclose(transpose(a__      ,						axes=(1, 2, 0)      )      ,						np.asarray(transpose(a__      ,						axes=(1, 2, 0)      )      )      )      )






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






       """simple docstring"""







       lowercase					:   Dict								= np.random.randn(3      ,						4      )
       self.assertTrue(np.allclose(reshape(a__      ,						(4, 3)      )      ,						np.reshape(a__      ,						(4, 3)      )      )      )

       lowercase					:   List[Any]								= np.random.randn(3      ,						4      ,						5      )
       self.assertTrue(np.allclose(reshape(a__      ,						(12, 5)      )      ,						np.reshape(a__      ,						(12, 5)      )      )      )






  @require_torch
  def 					__a			(  self		:	Dict      )		->    Tuple:






       """simple docstring"""







       lowercase					:   Any								= np.random.randn(3      ,						4      )
       lowercase					:   Any								= torch.tensor(a__      )
       self.assertTrue(np.allclose(reshape(a__      ,						(4, 3)      )      ,						reshape(a__      ,						(4, 3)      ).numpy()      )      )

       lowercase					:   int								= np.random.randn(3      ,						4      ,						5      )
       lowercase					:   int								= torch.tensor(a__      )
       self.assertTrue(np.allclose(reshape(a__      ,						(12, 5)      )      ,						reshape(a__      ,						(12, 5)      ).numpy()      )      )






  @require_tf
  def 					__a			(  self		:	Optional[int]      )		->    Union[str, Any]:






       """simple docstring"""







       lowercase					:   List[Any]								= np.random.randn(3      ,						4      )
       lowercase					:   int								= tf.constant(a__      )
       self.assertTrue(np.allclose(reshape(a__      ,						(4, 3)      )      ,						reshape(a__      ,						(4, 3)      ).numpy()      )      )

       lowercase					:   int								= np.random.randn(3      ,						4      ,						5      )
       lowercase					:   Dict								= tf.constant(a__      )
       self.assertTrue(np.allclose(reshape(a__      ,						(12, 5)      )      ,						reshape(a__      ,						(12, 5)      ).numpy()      )      )






  @require_flax
  def 					__a			(  self		:	Optional[int]      )		->    int:






       """simple docstring"""







       lowercase					:   Union[str, Any]								= np.random.randn(3      ,						4      )
       lowercase					:   Tuple								= jnp.array(a__      )
       self.assertTrue(np.allclose(reshape(a__      ,						(4, 3)      )      ,						np.asarray(reshape(a__      ,						(4, 3)      )      )      )      )

       lowercase					:   List[Any]								= np.random.randn(3      ,						4      ,						5      )
       lowercase					:   Dict								= jnp.array(a__      )
       self.assertTrue(np.allclose(reshape(a__      ,						(12, 5)      )      ,						np.asarray(reshape(a__      ,						(12, 5)      )      )      )      )






  def 					__a			(  self		:	Any      )		->    Optional[int]:






       """simple docstring"""







       lowercase					:   int								= np.random.randn(1      ,						3      ,						4      )
       self.assertTrue(np.allclose(squeeze(a__      )      ,						np.squeeze(a__      )      )      )

       lowercase					:   List[Any]								= np.random.randn(1      ,						4      ,						1      ,						5      )
       self.assertTrue(np.allclose(squeeze(a__      ,						axis=2      )      ,						np.squeeze(a__      ,						axis=2      )      )      )






  @require_torch
  def 					__a			(  self		:	Tuple      )		->    List[Any]:






       """simple docstring"""







       lowercase					:   Dict								= np.random.randn(1      ,						3      ,						4      )
       lowercase					:   Any								= torch.tensor(a__      )
       self.assertTrue(np.allclose(squeeze(a__      )      ,						squeeze(a__      ).numpy()      )      )

       lowercase					:   List[Any]								= np.random.randn(1      ,						4      ,						1      ,						5      )
       lowercase					:   List[Any]								= torch.tensor(a__      )
       self.assertTrue(np.allclose(squeeze(a__      ,						axis=2      )      ,						squeeze(a__      ,						axis=2      ).numpy()      )      )






  @require_tf
  def 					__a			(  self		:	Union[str, Any]      )		->    Tuple:






       """simple docstring"""







       lowercase					:   Tuple								= np.random.randn(1      ,						3      ,						4      )
       lowercase					:   Optional[Any]								= tf.constant(a__      )
       self.assertTrue(np.allclose(squeeze(a__      )      ,						squeeze(a__      ).numpy()      )      )

       lowercase					:   List[Any]								= np.random.randn(1      ,						4      ,						1      ,						5      )
       lowercase					:   int								= tf.constant(a__      )
       self.assertTrue(np.allclose(squeeze(a__      ,						axis=2      )      ,						squeeze(a__      ,						axis=2      ).numpy()      )      )






  @require_flax
  def 					__a			(  self		:	List[str]      )		->    List[str]:






       """simple docstring"""







       lowercase					:   List[Any]								= np.random.randn(1      ,						3      ,						4      )
       lowercase					:   Optional[Any]								= jnp.array(a__      )
       self.assertTrue(np.allclose(squeeze(a__      )      ,						np.asarray(squeeze(a__      )      )      )      )

       lowercase					:   List[Any]								= np.random.randn(1      ,						4      ,						1      ,						5      )
       lowercase					:   Dict								= jnp.array(a__      )
       self.assertTrue(np.allclose(squeeze(a__      ,						axis=2      )      ,						np.asarray(squeeze(a__      ,						axis=2      )      )      )      )






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






       """simple docstring"""







       lowercase					:   Dict								= np.random.randn(3      ,						4      )
       self.assertTrue(np.allclose(expand_dims(a__      ,						axis=1      )      ,						np.expand_dims(a__      ,						axis=1      )      )      )






  @require_torch
  def 					__a			(  self		:	Union[str, Any]      )		->    Optional[int]:






       """simple docstring"""







       lowercase					:   Any								= np.random.randn(3      ,						4      )
       lowercase					:   Union[str, Any]								= torch.tensor(a__      )
       self.assertTrue(np.allclose(expand_dims(a__      ,						axis=1      )      ,						expand_dims(a__      ,						axis=1      ).numpy()      )      )






  @require_tf
  def 					__a			(  self		:	Any      )		->    Any:






       """simple docstring"""







       lowercase					:   Optional[Any]								= np.random.randn(3      ,						4      )
       lowercase					:   Optional[int]								= tf.constant(a__      )
       self.assertTrue(np.allclose(expand_dims(a__      ,						axis=1      )      ,						expand_dims(a__      ,						axis=1      ).numpy()      )      )






  @require_flax
  def 					__a			(  self		:	int      )		->    Any:






       """simple docstring"""







       lowercase					:   str								= np.random.randn(3      ,						4      )
       lowercase					:   Optional[Any]								= jnp.array(a__      )
       self.assertTrue(np.allclose(expand_dims(a__      ,						axis=1      )      ,						np.asarray(expand_dims(a__      ,						axis=1      )      )      )      )






import os
import unittest

from transformers.models.bartpho.tokenization_bartpho import VOCAB_FILES_NAMES, BartphoTokenizer
from transformers.testing_utils import get_tests_dir

from ...test_tokenization_common import TokenizerTesterMixin


snake_case_				=			get_tests_dir('fixtures/test_sentencepiece_bpe.model')

class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase						,      unittest.TestCase  ):
      A_						:				List[Any]							=    BartphoTokenizer
      A_						:				List[str]							=    False
      A_						:				Optional[Any]							=    True


      def    a (self       :      Tuple      ):





         """simple docstring"""



         super().setUp()

         __snake_case							       =   ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']
         __snake_case							       =   dict(zip(a__				,			range(len(a__      )      )      )      )
         __snake_case							       =   {'''unk_token''': '''<unk>'''}

         __snake_case							       =   os.path.join(self.tmpdirname				,			VOCAB_FILES_NAMES['''monolingual_vocab_file''']      )
         with open(self.monolingual_vocab_file				,			'''w'''				,			encoding='''utf-8'''      ) as fp:
            for token in vocab_tokens:
               fp.write(f"""{token} {vocab_tokens[token]}\n"""      )

         __snake_case							       =   BartphoTokenizer(a__				,			self.monolingual_vocab_file				,			**self.special_tokens_map      )
         tokenizer.save_pretrained(self.tmpdirname      )


      def    a (self       :      str				,			**a__       :      str      ):





         """simple docstring"""



         kwargs.update(self.special_tokens_map      )
         return BartphoTokenizer.from_pretrained(self.tmpdirname				,			**a__      )


      def    a (self       :      str				,			a__       :      Any      ):





         """simple docstring"""



         __snake_case							       =   '''This is a là test'''
         __snake_case							       =   '''This is a<unk><unk> test'''
         return input_text, output_text


      def    a (self       :      Dict      ):





         """simple docstring"""



         __snake_case							       =   BartphoTokenizer(a__				,			self.monolingual_vocab_file				,			**self.special_tokens_map      )
         __snake_case							       =   '''This is a là test'''
         __snake_case							       =   '''▁This ▁is ▁a ▁l à ▁t est'''.split()
         __snake_case							       =   tokenizer.tokenize(a__      )
         self.assertListEqual(a__				,			a__      )

         __snake_case							       =   tokens + [tokenizer.unk_token]
         __snake_case							       =   [4, 5, 6, 3, 3, 7, 8, 3]
         self.assertListEqual(tokenizer.convert_tokens_to_ids(a__      )				,			a__      )

