



'''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			):







	"""simple docstring"""






	lowerCamelCase__			 =       MODEL_FOR_MASKED_LM_MAPPING
	lowerCamelCase__			 =       TF_MODEL_FOR_MASKED_LM_MAPPING







	def      A	(						self			:     List[str]		)			->		List[Any]:
								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      A	(						self			:     List[str]		)			->		List[Any]:
								UpperCAmelCase :       Tuple 							=		pipeline(task='''fill-mask'''		,			model='''sshleifer/tiny-distilroberta-base'''		,			top_k=2		,			framework='''tf'''		)
								UpperCAmelCase :       str 							=		unmasker('''My name is <mask>'''		)
								self.assertEqual(
								    nested_simplify(__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'''},
								    ]		,			)

								UpperCAmelCase :       Optional[Any] 							=		unmasker('''The largest city in France is <mask>'''		)
								self.assertEqual(
								    nested_simplify(__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''',
								        },
								    ]		,			)

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

								UpperCAmelCase :       List[Any] 							=		unmasker('''My name is <mask>'''		)
								self.assertEqual(
								    nested_simplify(__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'''},
								    ]		,			)

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

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

								UpperCAmelCase :       int 							=		unmasker('''My name is <mask> <mask>'''		,			top_k=2		)

								self.assertEqual(
								    nested_simplify(__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      A	(						self			:     Optional[Any]		)			->		Optional[Any]:
								UpperCAmelCase :       List[Any] 							=		pipeline('''fill-mask'''		,			model='''hf-internal-testing/tiny-random-distilbert'''		,			device=0		,			framework='''pt'''		)

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

								UpperCAmelCase :       List[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(__snake_case		,			__snake_case		)







	@slow
	@require_torch
	def      A	(						self			:     Optional[Any]		)			->		int:
								UpperCAmelCase :       Tuple 							=		pipeline(task='''fill-mask'''		,			model='''distilroberta-base'''		,			top_k=2		,			framework='''pt'''		)
								self.run_large_test(__snake_case		)







	@slow
	@require_tf
	def      A	(						self			:     Dict		)			->		Optional[int]:
								UpperCAmelCase :       Tuple 							=		pipeline(task='''fill-mask'''		,			model='''distilroberta-base'''		,			top_k=2		,			framework='''tf'''		)
								self.run_large_test(__snake_case		)







	def      A	(						self			:     Dict		,			__snake_case			:     Any		)			->		str:
								UpperCAmelCase :       List[str] 							=		unmasker('''My name is <mask>'''		)
								self.assertEqual(
								    nested_simplify(__snake_case		)		,			[
								        {'''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'''},
								    ]		,			)
								UpperCAmelCase :       Optional[int] 							=		unmasker('''The largest city in France is <mask>'''		)
								self.assertEqual(
								    nested_simplify(__snake_case		)		,			[
								        {
								            '''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''',
								        },
								    ]		,			)

								UpperCAmelCase :       Tuple 							=		unmasker('''My name is <mask>'''		,			targets=[''' Patrick''', ''' Clara''', ''' Teven''']		,			top_k=3		)
								self.assertEqual(
								    nested_simplify(__snake_case		)		,			[
								        {'''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      A	(						self			:     Union[str, Any]		)			->		Dict:
								UpperCAmelCase :       List[str] 							=		pipeline(task='''fill-mask'''		,			model='''sshleifer/tiny-distilroberta-base'''		,			framework='''pt'''		)
								UpperCAmelCase :       Optional[Any] 							=		None
								UpperCAmelCase :       Union[str, Any] 							=		None
								self.run_pipeline_test(__snake_case		,			[]		)







	@require_tf
	def      A	(						self			:     Dict		)			->		List[Any]:
								UpperCAmelCase :       List[str] 							=		pipeline(task='''fill-mask'''		,			model='''sshleifer/tiny-distilroberta-base'''		,			framework='''tf'''		)
								UpperCAmelCase :       Dict 							=		None
								UpperCAmelCase :       Union[str, Any] 							=		None
								self.run_pipeline_test(__snake_case		,			[]		)







	def      A	(						self			:     Union[str, Any]		,			__snake_case			:     Optional[int]		,			__snake_case			:     int		,			__snake_case			:     Optional[int]		)			->		Union[str, Any]:
								if tokenizer is None or tokenizer.mask_token_id is None:
															self.skipTest('''The provided tokenizer has no mask token, (probably reformer or wav2vec2)'''		)

								UpperCAmelCase :       Union[str, Any] 							=		FillMaskPipeline(model=__snake_case		,			tokenizer=__snake_case		)
								UpperCAmelCase :       Optional[Any] 							=		[
								    F"""This is another {tokenizer.mask_token} test""",
								]
								return fill_masker, examples







	def      A	(						self			:     List[str]		,			__snake_case			:     Any		,			__snake_case			:     str		)			->		str:
								UpperCAmelCase :       int 							=		fill_masker.tokenizer
								UpperCAmelCase :       Tuple 							=		fill_masker.model

								UpperCAmelCase :       Optional[Any] 							=		fill_masker(
								    F"""This is a {tokenizer.mask_token}"""		,			)
								self.assertEqual(
								    __snake_case		,			[
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								    ]		,			)

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

								UpperCAmelCase :       Union[str, Any] 							=		fill_masker([F"""This is a {tokenizer.mask_token}""", F"""Another {tokenizer.mask_token} great test."""]		)
								self.assertEqual(
								    __snake_case		,			[
								        [
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        ],
								        [
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								            {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        ],
								    ]		,			)

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

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







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

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

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

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







	def      A	(						self			:     Optional[Any]		,			__snake_case			:     Any		,			__snake_case			:     Optional[int]		)			->		Optional[Any]:
								UpperCAmelCase :       List[Any] 							=		FillMaskPipeline(model=__snake_case		,			tokenizer=__snake_case		,			top_k=2		)
								UpperCAmelCase :       Optional[int] 							=		fill_masker(F"""This is a {tokenizer.mask_token}"""		)
								self.assertEqual(
								    __snake_case		,			[
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								    ]		,			)

								UpperCAmelCase :       Union[str, Any] 							=		FillMaskPipeline(model=__snake_case		,			tokenizer=__snake_case		)
								UpperCAmelCase :       Tuple 							=		fill_masker(F"""This is a {tokenizer.mask_token}"""		,			top_k=2		)
								self.assertEqual(
								    __snake_case		,			[
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								        {'''sequence''': ANY(__snake_case		), '''score''': ANY(__snake_case		), '''token''': ANY(__snake_case		), '''token_str''': ANY(__snake_case		)},
								    ]		,			)
								self.assertEqual(nested_simplify(__snake_case		)		,			nested_simplify(__snake_case		)		)







	def      A	(						self			:     Dict		,			__snake_case			:     List[Any]		,			__snake_case			:     Tuple		)			->		int:
								UpperCAmelCase :       List[str] 							=		tokenizer.get_vocab()
								UpperCAmelCase :       Optional[int] 							=		FillMaskPipeline(model=__snake_case		,			tokenizer=__snake_case		)

								# top_k=2, ntargets=3
								UpperCAmelCase :       Union[str, Any] 							=		sorted(vocab.keys()		)[:3]
								UpperCAmelCase :       Any 							=		fill_masker(F"""This is a {tokenizer.mask_token}"""		,			top_k=2		,			targets=__snake_case		)

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







	def      A	(						self			:     Tuple		,			__snake_case			:     Dict		,			__snake_case			:     Dict		)			->		Union[str, Any]:
								UpperCAmelCase :       List[str] 							=		FillMaskPipeline(model=__snake_case		,			tokenizer=__snake_case		)
								UpperCAmelCase :       str 							=		tokenizer.get_vocab()
								# String duplicates + id duplicates
								UpperCAmelCase :       Optional[int] 							=		sorted(vocab.keys()		)[:3]
								UpperCAmelCase :       Union[str, Any] 							=		[targets[0], targets[1], targets[0], targets[2], targets[1]]
								UpperCAmelCase :       List[Any] 							=		fill_masker(F"""My name is {tokenizer.mask_token}"""		,			targets=__snake_case		,			top_k=10		)

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







	def      A	(						self			:     Tuple		,			__snake_case			:     Optional[Any]		,			__snake_case			:     Union[str, Any]		)			->		Optional[int]:
								UpperCAmelCase :       List[Any] 							=		FillMaskPipeline(model=__snake_case		,			tokenizer=__snake_case		)

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








"""simple docstring"""





from __future__ import annotations

from typing import Any




class 					_lowerCAmelCase  :







 """simple docstring"""







 def __init__(			self							:			Any,					UpperCAmelCase__							:			int	):
    __lowercase								=						num_of_nodes
    __lowercase								=						[]
    __lowercase								=						{}


 def 					_lowercase (			self							:			Optional[Any],					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			int	):
    self.m_edges.append([u_node, v_node, weight]	)


 def 					_lowercase (			self							:			Union[str, Any],					UpperCAmelCase__							:			int	):

    if self.m_component[u_node] == u_node:
       return u_node
    return self.find_component(self.m_component[u_node]	)


 def 					_lowercase (			self							:			List[Any],					UpperCAmelCase__							:			int	):

    if self.m_component[u_node] != u_node:
       for k in self.m_component:
          __lowercase								=						self.find_component(UpperCAmelCase__	)


 def 					_lowercase (			self							:			Union[str, Any],					UpperCAmelCase__							:			list[int],					UpperCAmelCase__							:			int,					UpperCAmelCase__							:			int	):

    if component_size[u_node] <= component_size[v_node]:
       __lowercase								=						v_node
       component_size[v_node] += component_size[u_node]
       self.set_component(UpperCAmelCase__	)

    elif component_size[u_node] >= component_size[v_node]:
       __lowercase								=						self.find_component(UpperCAmelCase__	)
       component_size[u_node] += component_size[v_node]
       self.set_component(UpperCAmelCase__	)



 def 					_lowercase (			self							:			Any	):
    __lowercase								=						[]
    __lowercase								=						0

    __lowercase								=						[-1] * self.m_num_of_nodes

    # A list of components (initialized to all of the nodes)
    for node in range(self.m_num_of_nodes	):
       self.m_component.update({node: node}	)
       component_size.append(1	)

    __lowercase								=						self.m_num_of_nodes

    while num_of_components > 1:
       for edge in self.m_edges:
          __lowercase		,__lowercase		,__lowercase								=						edge

          __lowercase								=						self.m_component[u]
          __lowercase								=						self.m_component[v]

          if u_component != v_component:

             for component in (u_component, v_component):
                if (
                    minimum_weight_edge[component] == -1
                    or minimum_weight_edge[component][2] > w
                ):
                   __lowercase								=						[u, v, w]

       for edge in minimum_weight_edge:
          if isinstance(UpperCAmelCase__,					UpperCAmelCase__	):
             __lowercase		,__lowercase		,__lowercase								=						edge

             __lowercase								=						self.m_component[u]
             __lowercase								=						self.m_component[v]

             if u_component != v_component:
                mst_weight += w
                self.union(UpperCAmelCase__,					UpperCAmelCase__,					UpperCAmelCase__	)
                print(F"""Added edge [{u} - {v}]\nAdded weight: {w}\n"""	)
                num_of_components -= 1

       __lowercase								=						[-1] * self.m_num_of_nodes
    print(F"""The total weight of the minimal spanning tree is: {mst_weight}"""	)







def   _A  (  )	->						None:
   '''simple docstring'''




if __name__ == "__main__":
   import doctest

   doctest.testmod()




