

'''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 		__SCREAMING_SNAKE_CASE       ( unittest.TestCase			):
				lowerCamelCase_							=						MODEL_FOR_MASKED_LM_MAPPING
				lowerCamelCase_							=						TF_MODEL_FOR_MASKED_LM_MAPPING







				def    lowerCamelCase_  (					self    :		str      ):
										'''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    :		Any      ):
										'''simple docstring'''
										lowercase   :       int								=pipeline(task='''fill-mask'''		,				model='''sshleifer/tiny-distilroberta-base'''		,				top_k=2		,				framework='''tf'''      )
										lowercase   :       Tuple								=unmasker('''My name is <mask>'''      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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'''},
										    ]		,				)

										lowercase   :       Tuple								=unmasker('''The largest city in France is <mask>'''      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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''',
										        },
										    ]		,				)

										lowercase   :       int								=unmasker('''My name is <mask>'''		,				targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,				top_k=3      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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    :		Any      ):
										'''simple docstring'''
										lowercase   :       List[Any]								=pipeline(task='''fill-mask'''		,				model='''sshleifer/tiny-distilroberta-base'''		,				top_k=2		,				framework='''pt'''      )

										lowercase   :       str								=unmasker('''My name is <mask>'''      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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'''},
										    ]		,				)

										lowercase   :       Tuple								=unmasker('''The largest city in France is <mask>'''      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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'''},
										    ]		,				)

										lowercase   :       Optional[Any]								=unmasker('''My name is <mask>'''		,				targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,				top_k=3      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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'''},
										    ]		,				)

										lowercase   :       Any								=unmasker('''My name is <mask> <mask>'''		,				top_k=2      )

										self.assertEqual(
										    nested_simplify(UpperCAmelCase__		,				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    :		Tuple      ):
										'''simple docstring'''
										lowercase   :       Any								=pipeline('''fill-mask'''		,				model='''hf-internal-testing/tiny-random-distilbert'''		,				device=0		,				framework='''pt'''      )

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

										lowercase   :       Optional[Any]								=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(UpperCAmelCase__		,				UpperCAmelCase__      )







				@slow
				@require_torch
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										lowercase   :       Any								=pipeline(task='''fill-mask'''		,				model='''distilroberta-base'''		,				top_k=2		,				framework='''pt'''      )
										self.run_large_test(UpperCAmelCase__      )







				@slow
				@require_tf
				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       str								=pipeline(task='''fill-mask'''		,				model='''distilroberta-base'''		,				top_k=2		,				framework='''tf'''      )
										self.run_large_test(UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Dict		,				UpperCAmelCase__    :		Dict      ):
										'''simple docstring'''
										lowercase   :       List[str]								=unmasker('''My name is <mask>'''      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__      )		,				[
										        {'''sequence''': '''My name is John''', '''score''': 0.0_08, '''token''': 610, '''token_str''': ''' John'''},
										        {'''sequence''': '''My name is Chris''', '''score''': 0.0_07, '''token''': 1573, '''token_str''': ''' Chris'''},
										    ]		,				)
										lowercase   :       int								=unmasker('''The largest city in France is <mask>'''      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__      )		,				[
										        {
										            '''sequence''': '''The largest city in France is Paris''',
										            '''score''': 0.2_51,
										            '''token''': 2201,
										            '''token_str''': ''' Paris''',
										        },
										        {
										            '''sequence''': '''The largest city in France is Lyon''',
										            '''score''': 0.2_14,
										            '''token''': 12790,
										            '''token_str''': ''' Lyon''',
										        },
										    ]		,				)

										lowercase   :       Union[str, Any]								=unmasker('''My name is <mask>'''		,				targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,				top_k=3      )
										self.assertEqual(
										    nested_simplify(UpperCAmelCase__      )		,				[
										        {'''sequence''': '''My name is Patrick''', '''score''': 0.0_05, '''token''': 3499, '''token_str''': ''' Patrick'''},
										        {'''sequence''': '''My name is Clara''', '''score''': 0.0_00, '''token''': 13606, '''token_str''': ''' Clara'''},
										        {'''sequence''': '''My name is Te''', '''score''': 0.0_00, '''token''': 2941, '''token_str''': ''' Te'''},
										    ]		,				)







				@require_torch
				def    lowerCamelCase_  (					self    :		Any      ):
										'''simple docstring'''
										lowercase   :       int								=pipeline(task='''fill-mask'''		,				model='''sshleifer/tiny-distilroberta-base'''		,				framework='''pt'''      )
										lowercase   :       List[str]								=None
										lowercase   :       Optional[int]								=None
										self.run_pipeline_test(UpperCAmelCase__		,				[]      )







				@require_tf
				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										lowercase   :       Dict								=pipeline(task='''fill-mask'''		,				model='''sshleifer/tiny-distilroberta-base'''		,				framework='''tf'''      )
										lowercase   :       Any								=None
										lowercase   :       Any								=None
										self.run_pipeline_test(UpperCAmelCase__		,				[]      )







				def    lowerCamelCase_  (					self    :		int		,				UpperCAmelCase__    :		Optional[int]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		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)'''      )

										lowercase   :       Tuple								=FillMaskPipeline(model=UpperCAmelCase__		,				tokenizer=UpperCAmelCase__      )
										lowercase   :       str								=[
										    F'''This is another {tokenizer.mask_token} test''',
										]
										return fill_masker, examples







				def    lowerCamelCase_  (					self    :		int		,				UpperCAmelCase__    :		Optional[int]		,				UpperCAmelCase__    :		Union[str, Any]      ):
										'''simple docstring'''
										lowercase   :       List[str]								=fill_masker.tokenizer
										lowercase   :       List[str]								=fill_masker.model

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

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

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

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

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







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

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

										# Score equivalence
										lowercase   :       int								=fill_masker(F'''This is a {tokenizer.mask_token}'''		,				targets=UpperCAmelCase__      )
										lowercase   :       List[str]								=[top_mask['''token_str'''] for top_mask in outputs]
										lowercase   :       Tuple								=[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(UpperCAmelCase__      ) == set(UpperCAmelCase__      ):
																lowercase   :       List[str]								=fill_masker(F'''This is a {tokenizer.mask_token}'''		,				targets=UpperCAmelCase__      )
																lowercase   :       List[str]								=[top_mask['''score'''] for top_mask in unmasked_targets]
																self.assertEqual(nested_simplify(UpperCAmelCase__      )		,				nested_simplify(UpperCAmelCase__      )      )

										# Raises with invalid
										with self.assertRaises(UpperCAmelCase__      ):
																lowercase   :       Any								=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(UpperCAmelCase__      ):
																						lowercase   :       Dict								=fill_masker(F'''This is a {tokenizer.mask_token}'''		,				targets=['''''']      )
																with self.assertRaises(UpperCAmelCase__      ):
																						lowercase   :       Union[str, Any]								=fill_masker(F'''This is a {tokenizer.mask_token}'''		,				targets=''''''      )







				def    lowerCamelCase_  (					self    :		int		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		Optional[int]      ):
										'''simple docstring'''
										lowercase   :       Any								=FillMaskPipeline(model=UpperCAmelCase__		,				tokenizer=UpperCAmelCase__		,				top_k=2      )
										lowercase   :       Dict								=fill_masker(F'''This is a {tokenizer.mask_token}'''      )
										self.assertEqual(
										    UpperCAmelCase__		,				[
										        {'''sequence''': ANY(UpperCAmelCase__      ), '''score''': ANY(UpperCAmelCase__      ), '''token''': ANY(UpperCAmelCase__      ), '''token_str''': ANY(UpperCAmelCase__      )},
										        {'''sequence''': ANY(UpperCAmelCase__      ), '''score''': ANY(UpperCAmelCase__      ), '''token''': ANY(UpperCAmelCase__      ), '''token_str''': ANY(UpperCAmelCase__      )},
										    ]		,				)

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







				def    lowerCamelCase_  (					self    :		Optional[Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       Tuple								=tokenizer.get_vocab()
										lowercase   :       int								=FillMaskPipeline(model=UpperCAmelCase__		,				tokenizer=UpperCAmelCase__      )

										# top_k=2, ntargets=3
										lowercase   :       Dict								=sorted(vocab.keys()      )[:3]
										lowercase   :       str								=fill_masker(F'''This is a {tokenizer.mask_token}'''		,				top_k=2		,				targets=UpperCAmelCase__      )

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







				def    lowerCamelCase_  (					self    :		Tuple		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Any      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=FillMaskPipeline(model=UpperCAmelCase__		,				tokenizer=UpperCAmelCase__      )
										lowercase   :       Optional[int]								=tokenizer.get_vocab()
										# String duplicates + id duplicates
										lowercase   :       Union[str, Any]								=sorted(vocab.keys()      )[:3]
										lowercase   :       List[Any]								=[targets[0], targets[1], targets[0], targets[2], targets[1]]
										lowercase   :       Union[str, Any]								=fill_masker(F'''My name is {tokenizer.mask_token}'''		,				targets=UpperCAmelCase__		,				top_k=10      )

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







				def    lowerCamelCase_  (					self    :		Optional[int]		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		Optional[int]      ):
										'''simple docstring'''
										lowercase   :       Dict								=FillMaskPipeline(model=UpperCAmelCase__		,				tokenizer=UpperCAmelCase__      )

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








'''simple docstring'''





import argparse
import copy






def 							_lowerCAmelCase (      __magic_name__	:    List[str]	)							->     Union[str, Any]:
						lowercase   :       int								={}

						with open(__magic_name__	) as f:
												for line in f:
																		if line.split()[0] not in dict_of_neighbours:
																								lowercase   :       List[str]								=[]
																								_list.append([line.split()[1], line.split()[2]]	)
																								lowercase   :       Tuple								=_list
																		else:
																								dict_of_neighbours[line.split()[0]].append(
																								    [line.split()[1], line.split()[2]]	)
																		if line.split()[1] not in dict_of_neighbours:
																								lowercase   :       List[Any]								=[]
																								_list.append([line.split()[0], line.split()[2]]	)
																								lowercase   :       Union[str, Any]								=_list
																		else:
																								dict_of_neighbours[line.split()[1]].append(
																								    [line.split()[0], line.split()[2]]	)

						return dict_of_neighbours






def 							_lowerCAmelCase (      __magic_name__	:    Optional[int]      ,   __magic_name__	:    List[Any]	)							->     str:

						with open(__magic_name__	) as f:
												lowercase   :       Optional[int]								=f.read(1	)
						lowercase   :       List[Any]								=start_node

						lowercase   :       List[Any]								=[]

						lowercase   :       str								=start_node

						lowercase   :       str								=0
						while visiting not in first_solution:
												lowercase   :       Optional[int]								=10000
												for k in dict_of_neighbours[visiting]:
																		if int(k[1]	) < int(__magic_name__	) and k[0] not in first_solution:
																								lowercase   :       List[Any]								=k[1]
																								lowercase   :       str								=k[0]

												first_solution.append(__magic_name__	)
												lowercase   :       Any								=distance_of_first_solution + int(__magic_name__	)
												lowercase   :       Optional[int]								=best_node

						first_solution.append(__magic_name__	)

						lowercase   :       str								=0
						for k in dict_of_neighbours[first_solution[-2]]:
												if k[0] == start_node:
																		break
												position += 1

						lowercase   :       str								=(
						    distance_of_first_solution
						    + int(dict_of_neighbours[first_solution[-2]][position][1]	)
						    - 10000
						)
						return first_solution, distance_of_first_solution






def 							_lowerCAmelCase (      __magic_name__	:    str      ,   __magic_name__	:    Any	)							->     Tuple:
						lowercase   :       Tuple								=[]

						for n in solution[1:-1]:
												lowercase   :       Dict								=solution.index(__magic_name__	)
												for kn in solution[1:-1]:
																		lowercase   :       Tuple								=solution.index(__magic_name__	)
																		if n == kn:
																								continue

																		lowercase   :       Union[str, Any]								=copy.deepcopy(__magic_name__	)
																		lowercase   :       Optional[int]								=kn
																		lowercase   :       List[Any]								=n

																		lowercase   :       List[Any]								=0

																		for k in _tmp[:-1]:
																								lowercase   :       Optional[int]								=_tmp[_tmp.index(__magic_name__	) + 1]
																								for i in dict_of_neighbours[k]:
																														if i[0] == next_node:
																																				lowercase   :       Optional[int]								=distance + int(i[1]	)
																		_tmp.append(__magic_name__	)

																		if _tmp not in neighborhood_of_solution:
																								neighborhood_of_solution.append(_tmp	)

						lowercase   :       Union[str, Any]								=len(neighborhood_of_solution[0]	) - 1

						neighborhood_of_solution.sort(key=lambda __magic_name__	: x[index_of_last_item_in_the_list]	)
						return neighborhood_of_solution






def 							_lowerCAmelCase (      __magic_name__	:    Any      ,   __magic_name__	:    str      ,   __magic_name__	:    List[Any]      ,   __magic_name__	:    Tuple      ,   __magic_name__	:    Dict	)							->     Union[str, Any]:
						lowercase   :       str								=1
						lowercase   :       List[Any]								=first_solution
						lowercase   :       Any								=[]
						lowercase   :       str								=distance_of_first_solution
						lowercase   :       str								=solution

						while count <= iters:
												lowercase   :       Union[str, Any]								=find_neighborhood(__magic_name__      ,   __magic_name__	)
												lowercase   :       Dict								=0
												lowercase   :       int								=neighborhood[index_of_best_solution]
												lowercase   :       Optional[int]								=len(__magic_name__	) - 1

												lowercase   :       List[Any]								=False
												while not found:
																		lowercase   :       List[Any]								=0
																		while i < len(__magic_name__	):
																								if best_solution[i] != solution[i]:
																														lowercase   :       List[str]								=best_solution[i]
																														lowercase   :       Dict								=solution[i]
																														break
																								lowercase   :       Any								=i + 1

																		if [first_exchange_node, second_exchange_node] not in tabu_list and [
																		    second_exchange_node,
																		    first_exchange_node,
																		] not in tabu_list:
																								tabu_list.append([first_exchange_node, second_exchange_node]	)
																								lowercase   :       str								=True
																								lowercase   :       int								=best_solution[:-1]
																								lowercase   :       Any								=neighborhood[index_of_best_solution][best_cost_index]
																								if cost < best_cost:
																														lowercase   :       Optional[int]								=cost
																														lowercase   :       str								=solution
																		else:
																								lowercase   :       Optional[int]								=index_of_best_solution + 1
																								lowercase   :       List[Any]								=neighborhood[index_of_best_solution]

												if len(__magic_name__	) >= size:
																		tabu_list.pop(0	)

												lowercase   :       Optional[int]								=count + 1

						return best_solution_ever, best_cost






def 							_lowerCAmelCase (      __magic_name__	:    str=None	)							->     Tuple:
						lowercase   :       List[str]								=generate_neighbours(args.File	)

						lowercase		,  lowercase   :       Optional[Any]								=generate_first_solution(
						    args.File      ,   __magic_name__	)

						lowercase		,  lowercase   :       int								=tabu_search(
						    __magic_name__      ,   __magic_name__      ,   __magic_name__      ,   args.Iterations      ,   args.Size      ,   )

						print(f'''Best solution: {best_sol}, with total distance: {best_cost}.'''	)


if __name__ == "__main__":
							UpperCamelCase_							=					argparse.ArgumentParser(description="""Tabu Search""")
							parser.add_argument(
							    """-f""",
							    """--File""",
							    type=str,
							    help="""Path to the file containing the data""",
							    required=True,
							)
							parser.add_argument(
							    """-i""",
							    """--Iterations""",
							    type=int,
							    help="""How many iterations the algorithm should perform""",
							    required=True,
							)
							parser.add_argument(
							    """-s""", """--Size""", type=int, help="""Size of the tabu list""", required=True
							)

							# Pass the arguments to main method
							main(parser.parse_args())





