
'''simple docstring'''




import gc
import unittest

from transformers import MODEL_FOR_MASKED_LM_MAPPING, TF_MODEL_FOR_MASKED_LM_MAPPING, FillMaskPipeline, pipeline
from transformers.pipelines import PipelineException
from transformers.testing_utils import (
    is_pipeline_test,
    is_torch_available,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_gpu,
    slow,
)

from .test_pipelines_common import ANY





@is_pipeline_test
class 					A							(				unittest.TestCase		):
     __magic_name__          =						MODEL_FOR_MASKED_LM_MAPPING
     __magic_name__          =						TF_MODEL_FOR_MASKED_LM_MAPPING





     def 							__lowerCAmelCase				(				self      )					->							Tuple:






         """simple docstring"""






         super().tearDown()
         # clean-up as much as possible GPU memory occupied by PyTorch
         gc.collect()
         if is_torch_available():
             import torch

             torch.cuda.empty_cache()





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






         """simple docstring"""






         A			:  int   =		pipeline(task='''fill-mask'''		,     model='''sshleifer/tiny-distilroberta-base'''		,     top_k=2		,     framework='''tf'''      )
         A			:  List[Any]   =		unmasker('''My name is <mask>'''      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 {'''sequence''': '''My name is grouped''', '''score''': 2.1e-05, '''token''': 38015, '''token_str''': ''' grouped'''},
                 {'''sequence''': '''My name is accuser''', '''score''': 2.1e-05, '''token''': 25506, '''token_str''': ''' accuser'''},
             ]		,     )

         A			:  Union[str, Any]   =		unmasker('''The largest city in France is <mask>'''      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 {
                     '''sequence''': '''The largest city in France is grouped''',
                     '''score''': 2.1e-05,
                     '''token''': 38015,
                     '''token_str''': ''' grouped''',
                 },
                 {
                     '''sequence''': '''The largest city in France is accuser''',
                     '''score''': 2.1e-05,
                     '''token''': 25506,
                     '''token_str''': ''' accuser''',
                 },
             ]		,     )

         A			:  Optional[int]   =		unmasker('''My name is <mask>'''		,     targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,     top_k=3      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 {'''sequence''': '''My name is Clara''', '''score''': 2e-05, '''token''': 13606, '''token_str''': ''' Clara'''},
                 {'''sequence''': '''My name is Patrick''', '''score''': 2e-05, '''token''': 3499, '''token_str''': ''' Patrick'''},
                 {'''sequence''': '''My name is Te''', '''score''': 1.9e-05, '''token''': 2941, '''token_str''': ''' Te'''},
             ]		,     )





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






         """simple docstring"""






         A			:  Any   =		pipeline(task='''fill-mask'''		,     model='''sshleifer/tiny-distilroberta-base'''		,     top_k=2		,     framework='''pt'''      )

         A			:  Dict   =		unmasker('''My name is <mask>'''      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 {'''sequence''': '''My name is Maul''', '''score''': 2.2e-05, '''token''': 35676, '''token_str''': ''' Maul'''},
                 {'''sequence''': '''My name isELS''', '''score''': 2.2e-05, '''token''': 16416, '''token_str''': '''ELS'''},
             ]		,     )

         A			:  Tuple   =		unmasker('''The largest city in France is <mask>'''      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 {
                     '''sequence''': '''The largest city in France is Maul''',
                     '''score''': 2.2e-05,
                     '''token''': 35676,
                     '''token_str''': ''' Maul''',
                 },
                 {'''sequence''': '''The largest city in France isELS''', '''score''': 2.2e-05, '''token''': 16416, '''token_str''': '''ELS'''},
             ]		,     )

         A			:  Dict   =		unmasker('''My name is <mask>'''		,     targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,     top_k=3      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 {'''sequence''': '''My name is Patrick''', '''score''': 2.1e-05, '''token''': 3499, '''token_str''': ''' Patrick'''},
                 {'''sequence''': '''My name is Te''', '''score''': 2e-05, '''token''': 2941, '''token_str''': ''' Te'''},
                 {'''sequence''': '''My name is Clara''', '''score''': 2e-05, '''token''': 13606, '''token_str''': ''' Clara'''},
             ]		,     )

         A			:  Union[str, Any]   =		unmasker('''My name is <mask> <mask>'''		,     top_k=2      )

         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE		,     decimals=6      )		,     [
                 [
                     {
                         '''score''': 2.2e-05,
                         '''token''': 35676,
                         '''token_str''': ''' Maul''',
                         '''sequence''': '''<s>My name is Maul<mask></s>''',
                     },
                     {'''score''': 2.2e-05, '''token''': 16416, '''token_str''': '''ELS''', '''sequence''': '''<s>My name isELS<mask></s>'''},
                 ],
                 [
                     {
                         '''score''': 2.2e-05,
                         '''token''': 35676,
                         '''token_str''': ''' Maul''',
                         '''sequence''': '''<s>My name is<mask> Maul</s>''',
                     },
                     {'''score''': 2.2e-05, '''token''': 16416, '''token_str''': '''ELS''', '''sequence''': '''<s>My name is<mask>ELS</s>'''},
                 ],
             ]		,     )





     @require_torch_gpu
     def 							__lowerCAmelCase				(				self      )					->							str:






         """simple docstring"""






         A			:  Optional[int]   =		pipeline('''fill-mask'''		,     model='''hf-internal-testing/tiny-random-distilbert'''		,     device=0		,     framework='''pt'''      )

         # convert model to fp16
         pipe.model.half()

         A			:  List[str]   =		pipe('''Paris is the [MASK] of France.'''      )
         # We actually don't care about the result, we just want to make sure
         # it works, meaning the float16 tensor got casted back to float32
         # for postprocessing.
         self.assertIsInstance(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )





     @slow
     @require_torch
     def 							__lowerCAmelCase				(				self      )					->							Optional[int]:






         """simple docstring"""






         A			:  Any   =		pipeline(task='''fill-mask'''		,     model='''distilroberta-base'''		,     top_k=2		,     framework='''pt'''      )
         self.run_large_test(SCREAMING_SNAKE_CASE      )





     @slow
     @require_tf
     def 							__lowerCAmelCase				(				self      )					->							Dict:






         """simple docstring"""






         A			:  Tuple   =		pipeline(task='''fill-mask'''		,     model='''distilroberta-base'''		,     top_k=2		,     framework='''tf'''      )
         self.run_large_test(SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE      )					->							Tuple:






         """simple docstring"""






         A			:  Optional[int]   =		unmasker('''My name is <mask>'''      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE      )		,     [
                 {'''sequence''': '''My name is John''', '''score''': 0.008, '''token''': 610, '''token_str''': ''' John'''},
                 {'''sequence''': '''My name is Chris''', '''score''': 0.007, '''token''': 1573, '''token_str''': ''' Chris'''},
             ]		,     )
         A			:  Tuple   =		unmasker('''The largest city in France is <mask>'''      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE      )		,     [
                 {
                     '''sequence''': '''The largest city in France is Paris''',
                     '''score''': 0.251,
                     '''token''': 2201,
                     '''token_str''': ''' Paris''',
                 },
                 {
                     '''sequence''': '''The largest city in France is Lyon''',
                     '''score''': 0.214,
                     '''token''': 12790,
                     '''token_str''': ''' Lyon''',
                 },
             ]		,     )

         A			:  int   =		unmasker('''My name is <mask>'''		,     targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,     top_k=3      )
         self.assertEqual(
             nested_simplify(SCREAMING_SNAKE_CASE      )		,     [
                 {'''sequence''': '''My name is Patrick''', '''score''': 0.005, '''token''': 3499, '''token_str''': ''' Patrick'''},
                 {'''sequence''': '''My name is Clara''', '''score''': 0.000, '''token''': 13606, '''token_str''': ''' Clara'''},
                 {'''sequence''': '''My name is Te''', '''score''': 0.000, '''token''': 2941, '''token_str''': ''' Te'''},
             ]		,     )





     @require_torch
     def 							__lowerCAmelCase				(				self      )					->							List[str]:






         """simple docstring"""






         A			:  int   =		pipeline(task='''fill-mask'''		,     model='''sshleifer/tiny-distilroberta-base'''		,     framework='''pt'''      )
         A			:  int   =		None
         A			:  int   =		None
         self.run_pipeline_test(SCREAMING_SNAKE_CASE		,     []      )





     @require_tf
     def 							__lowerCAmelCase				(				self      )					->							int:






         """simple docstring"""






         A			:  Union[str, Any]   =		pipeline(task='''fill-mask'''		,     model='''sshleifer/tiny-distilroberta-base'''		,     framework='''tf'''      )
         A			:  Dict   =		None
         A			:  int   =		None
         self.run_pipeline_test(SCREAMING_SNAKE_CASE		,     []      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Optional[int]:






         """simple docstring"""






         if tokenizer is None or tokenizer.mask_token_id is None:
             self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)'''      )

         A			:  List[Any]   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE      )
         A			:  List[str]   =		[
             F'This is another {tokenizer.mask_token} test',
         ]
         return fill_masker, examples





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Any:






         """simple docstring"""






         A			:  Dict   =		fill_masker.tokenizer
         A			:  Union[str, Any]   =		fill_masker.model

         A			:  int   =		fill_masker(
             F'This is a {tokenizer.mask_token}'		,     )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
             ]		,     )

         A			:  Optional[int]   =		fill_masker([F'This is a {tokenizer.mask_token}']      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
             ]		,     )

         A			:  Optional[int]   =		fill_masker([F'This is a {tokenizer.mask_token}', F'Another {tokenizer.mask_token} great test.']      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 [
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 ],
                 [
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 ],
             ]		,     )

         with self.assertRaises(SCREAMING_SNAKE_CASE      ):
             fill_masker([None]      )
         # No mask_token is not supported
         with self.assertRaises(SCREAMING_SNAKE_CASE      ):
             fill_masker('''This is'''      )

         self.run_test_top_k(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )
         self.run_test_targets(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )
         self.run_test_top_k_targets(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )
         self.fill_mask_with_duplicate_targets_and_top_k(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )
         self.fill_mask_with_multiple_masks(SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Optional[Any]:






         """simple docstring"""






         A			:  List[str]   =		tokenizer.get_vocab()
         A			:  Union[str, Any]   =		sorted(vocab.keys()      )[:2]
         # Pipeline argument
         A			:  int   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE		,     targets=SCREAMING_SNAKE_CASE      )
         A			:  Optional[Any]   =		fill_masker(F'This is a {tokenizer.mask_token}'      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
             ]		,     )
         A			:  Optional[int]   =		{vocab[el] for el in targets}
         self.assertEqual({el['''token'''] for el in outputs}		,     SCREAMING_SNAKE_CASE      )
         A			:  Optional[Any]   =		[tokenizer.decode([x]      ) for x in target_ids]
         self.assertEqual({el['''token_str'''] for el in outputs}		,     set(SCREAMING_SNAKE_CASE      )      )

         # Call argument
         A			:  int   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE      )
         A			:  Optional[Any]   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     targets=SCREAMING_SNAKE_CASE      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
             ]		,     )
         A			:  Dict   =		{vocab[el] for el in targets}
         self.assertEqual({el['''token'''] for el in outputs}		,     SCREAMING_SNAKE_CASE      )
         A			:  Union[str, Any]   =		[tokenizer.decode([x]      ) for x in target_ids]
         self.assertEqual({el['''token_str'''] for el in outputs}		,     set(SCREAMING_SNAKE_CASE      )      )

         # Score equivalence
         A			:  List[str]   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     targets=SCREAMING_SNAKE_CASE      )
         A			:  Dict   =		[top_mask["""token_str"""] for top_mask in outputs]
         A			:  Dict   =		[top_mask["""score"""] for top_mask in outputs]

         # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
         if set(SCREAMING_SNAKE_CASE      ) == set(SCREAMING_SNAKE_CASE      ):
             A			:  Optional[int]   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     targets=SCREAMING_SNAKE_CASE      )
             A			:  List[str]   =		[top_mask["""score"""] for top_mask in unmasked_targets]
             self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE      )		,     nested_simplify(SCREAMING_SNAKE_CASE      )      )

         # Raises with invalid
         with self.assertRaises(SCREAMING_SNAKE_CASE      ):
             A			:  str   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     targets=[]      )
         # For some tokenizers, `""` is actually in the vocabulary and the expected error won't raised
         if "" not in tokenizer.get_vocab():
             with self.assertRaises(SCREAMING_SNAKE_CASE      ):
                 A			:  Union[str, Any]   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     targets=['''''']      )
             with self.assertRaises(SCREAMING_SNAKE_CASE      ):
                 A			:  List[str]   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     targets=''''''      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							List[Any]:






         """simple docstring"""






         A			:  List[str]   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE		,     top_k=2      )
         A			:  Any   =		fill_masker(F'This is a {tokenizer.mask_token}'      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
             ]		,     )

         A			:  List[str]   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE      )
         A			:  int   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     top_k=2      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
             ]		,     )
         self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE      )		,     nested_simplify(SCREAMING_SNAKE_CASE      )      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							str:






         """simple docstring"""






         A			:  Optional[int]   =		tokenizer.get_vocab()
         A			:  str   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE      )

         # top_k=2, ntargets=3
         A			:  List[str]   =		sorted(vocab.keys()      )[:3]
         A			:  Optional[int]   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     top_k=2		,     targets=SCREAMING_SNAKE_CASE      )

         # If we use the most probably targets, and filter differently, we should still
         # have the same results
         A			:  Optional[Any]   =		[el["""token_str"""] for el in sorted(SCREAMING_SNAKE_CASE		,     key=lambda SCREAMING_SNAKE_CASE      : x["score"]		,     reverse=SCREAMING_SNAKE_CASE      )]
         # For some BPE tokenizers, `</w>` is removed during decoding, so `token_str` won't be the same as in `targets`.
         if set(SCREAMING_SNAKE_CASE      ).issubset(SCREAMING_SNAKE_CASE      ):
             A			:  Any   =		fill_masker(F'This is a {tokenizer.mask_token}'		,     top_k=3		,     targets=SCREAMING_SNAKE_CASE      )
             # They should yield exactly the same result
             self.assertEqual(nested_simplify(SCREAMING_SNAKE_CASE      )		,     nested_simplify(SCREAMING_SNAKE_CASE      )      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							Any:






         """simple docstring"""






         A			:  Tuple   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE      )
         A			:  List[Any]   =		tokenizer.get_vocab()
         # String duplicates + id duplicates
         A			:  List[Any]   =		sorted(vocab.keys()      )[:3]
         A			:  str   =		[targets[0], targets[1], targets[0], targets[2], targets[1]]
         A			:  Union[str, Any]   =		fill_masker(F'My name is {tokenizer.mask_token}'		,     targets=SCREAMING_SNAKE_CASE		,     top_k=10      )

         # The target list contains duplicates, so we can't output more
         # than them
         self.assertEqual(len(SCREAMING_SNAKE_CASE      )		,     3      )





     def 							__lowerCAmelCase				(				self		,     SCREAMING_SNAKE_CASE		,     SCREAMING_SNAKE_CASE      )					->							List[Any]:






         """simple docstring"""






         A			:  Optional[int]   =		FillMaskPipeline(model=SCREAMING_SNAKE_CASE		,     tokenizer=SCREAMING_SNAKE_CASE      )

         A			:  Optional[int]   =		fill_masker(
             F'This is a {tokenizer.mask_token} {tokenizer.mask_token} {tokenizer.mask_token}'		,     top_k=2      )
         self.assertEqual(
             SCREAMING_SNAKE_CASE		,     [
                 [
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 ],
                 [
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 ],
                 [
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                     {'''sequence''': ANY(SCREAMING_SNAKE_CASE      ), '''score''': ANY(SCREAMING_SNAKE_CASE      ), '''token''': ANY(SCREAMING_SNAKE_CASE      ), '''token_str''': ANY(SCREAMING_SNAKE_CASE      )},
                 ],
             ]		,     )








"""simple docstring"""
from __future__ import annotations

import queue




class 	lowercase__				:





   def __init__(							self      ,   SCREAMING_SNAKE_CASE)  ->  int:
      _lowerCamelCase	:					int   =		data
      _lowerCamelCase	:					List[str]   =		None
      _lowerCamelCase	:					Any   =		None







def 				_snake_case      (   ):




   """simple docstring"""







   print("""\n********Press N to stop entering at any point of time********\n"""   )
   _lowerCamelCase	:					Optional[int]   =		input("""Enter the value of the root node: """   ).strip().lower()
   _lowerCamelCase	:					queue.Queue   =		queue.Queue()
   _lowerCamelCase	:					Optional[int]   =		TreeNode(int(__snake_case   )   )
   q.put(__snake_case   )
   while not q.empty():
      _lowerCamelCase	:					Tuple   =		q.get()
      _lowerCamelCase	:					Any   =		F'Enter the left node of {node_found.data}: '
      _lowerCamelCase	:					Union[str, Any]   =		input(__snake_case   ).strip().lower() or """n"""
      if check == "n":
         return tree_node
      _lowerCamelCase	:					Dict   =		TreeNode(int(__snake_case   )   )
      _lowerCamelCase	:					List[str]   =		left_node
      q.put(__snake_case   )
      _lowerCamelCase	:					Optional[int]   =		F'Enter the right node of {node_found.data}: '
      _lowerCamelCase	:					Optional[Any]   =		input(__snake_case   ).strip().lower() or """n"""
      if check == "n":
         return tree_node
      _lowerCamelCase	:					List[Any]   =		TreeNode(int(__snake_case   )   )
      _lowerCamelCase	:					List[Any]   =		right_node
      q.put(__snake_case   )
   raise

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   print(node.data						, end=""","""   )
   pre_order(node.left   )
   pre_order(node.right   )

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   in_order(node.left   )
   print(node.data						, end=""","""   )
   in_order(node.right   )

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   post_order(node.left   )
   post_order(node.right   )
   print(node.data						, end=""","""   )

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   _lowerCamelCase	:					queue.Queue   =		queue.Queue()
   q.put(__snake_case   )
   while not q.empty():
      _lowerCamelCase	:					Any   =		q.get()
      print(node_dequeued.data						, end=""","""   )
      if node_dequeued.left:
         q.put(node_dequeued.left   )
      if node_dequeued.right:
         q.put(node_dequeued.right   )

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   _lowerCamelCase	:					queue.Queue   =		queue.Queue()
   q.put(__snake_case   )
   while not q.empty():
      _lowerCamelCase	:					Optional[Any]   =		[]
      while not q.empty():
         _lowerCamelCase	:					Dict   =		q.get()
         print(node_dequeued.data						, end=""","""   )
         if node_dequeued.left:
            list_.append(node_dequeued.left   )
         if node_dequeued.right:
            list_.append(node_dequeued.right   )
      print()
      for node in list_:
         q.put(__snake_case   )

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   _lowerCamelCase	:					list[TreeNode]   =		[]
   _lowerCamelCase	:					Optional[int]   =		node
   while n or stack:
      while n:  # start from root node, find its left child
         print(n.data						, end=""","""   )
         stack.append(__snake_case   )
         _lowerCamelCase	:					Tuple   =		n.left
      # end of while means current node doesn't have left child
      _lowerCamelCase	:					Optional[Any]   =		stack.pop()
      # start to traverse its right child
      _lowerCamelCase	:					Dict   =		n.right

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   _lowerCamelCase	:					list[TreeNode]   =		[]
   _lowerCamelCase	:					int   =		node
   while n or stack:
      while n:
         stack.append(__snake_case   )
         _lowerCamelCase	:					Any   =		n.left
      _lowerCamelCase	:					Optional[Any]   =		stack.pop()
      print(n.data						, end=""","""   )
      _lowerCamelCase	:					List[Any]   =		n.right

def 				_snake_case      (   __snake_case							:      TreeNode   ):




   """simple docstring"""







   if not isinstance(__snake_case						, __snake_case   ) or not node:
      return
   _lowerCamelCase   ,    _lowerCamelCase	:					Union[str, Any]   =		[], []
   _lowerCamelCase	:					Optional[Any]   =		node
   stacka.append(__snake_case   )
   while stacka:  # to find the reversed order of post order, store it in stack2
      _lowerCamelCase	:					Union[str, Any]   =		stacka.pop()
      if n.left:
         stacka.append(n.left   )
      if n.right:
         stacka.append(n.right   )
      stacka.append(__snake_case   )
   while stacka:  # pop up from stack2 will be the post order
      print(stacka.pop().data						, end=""","""   )

def 				_snake_case      (   __snake_case							:      str = ""						, __snake_case							:      Any=50						, __snake_case							:      List[str]="*"   ):




   """simple docstring"""







   if not s:
      return "\n" + width * char
   _lowerCamelCase   ,    _lowerCamelCase	:					Optional[int]   =		divmod(width - len(__snake_case   ) - 2						, 2   )
   return F'{left * char} {s} {(left + extra) * char}'


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(prompt("""Binary Tree Traversals"""))

 UpperCAmelCase				=						build_tree()
 print(prompt("""Pre Order Traversal"""))
 pre_order(node)
 print(prompt() + """\n""")

 print(prompt("""In Order Traversal"""))
 in_order(node)
 print(prompt() + """\n""")

 print(prompt("""Post Order Traversal"""))
 post_order(node)
 print(prompt() + """\n""")

 print(prompt("""Level Order Traversal"""))
 level_order(node)
 print(prompt() + """\n""")

 print(prompt("""Actual Level Order Traversal"""))
 level_order_actual(node)
 print("""*""" * 50 + """\n""")

 print(prompt("""Pre Order Traversal - Iteration Version"""))
 pre_order_iter(node)
 print(prompt() + """\n""")

 print(prompt("""In Order Traversal - Iteration Version"""))
 in_order_iter(node)
 print(prompt() + """\n""")

 print(prompt("""Post Order Traversal - Iteration Version"""))
 post_order_iter(node)
 print(prompt())

