
"""simple docstring"""


import argparse
import os
import re

import packaging.version


_lowercase				 =					'''examples/'''
_lowercase				 =					{
    '''examples''': (re.compile(r'''^check_min_version\("[^"]+"\)\s*$''', re.MULTILINE), '''check_min_version("VERSION")\n'''),
    '''init''': (re.compile(r'''^__version__\s+=\s+"([^"]+)"\s*$''', re.MULTILINE), '''__version__ = "VERSION"\n'''),
    '''setup''': (re.compile(r'''^(\s*)version\s*=\s*"[^"]+",''', re.MULTILINE), r'''\1version="VERSION",'''),
    '''doc''': (re.compile(r'''^(\s*)release\s*=\s*"[^"]+"$''', re.MULTILINE), '''release = "VERSION"\n'''),
}
_lowercase				 =					{
    '''init''': '''src/transformers/__init__.py''',
    '''setup''': '''setup.py''',
}
_lowercase				 =					'''README.md'''






def    _snake_case					(		snake_case__ :  int						,   snake_case__ :  Optional[int]						,   snake_case__ :  List[Any]  ):
	with open(snake_case__						,   'r'						,   encoding='utf-8'						,   newline='\n'  ) as f:
		A     = f.read()
	A ,   A     = REPLACE_PATTERNS[pattern]
	A     = replace.replace('VERSION'						,   snake_case__  )
	A     = re_pattern.sub(snake_case__						,   snake_case__  )
	with open(snake_case__						,   'w'						,   encoding='utf-8'						,   newline='\n'  ) as f:
		f.write(snake_case__  )






def    _snake_case					(		snake_case__ :  Tuple  ):
	for folder, directories, fnames in os.walk(snake_case__  ):
		# Removing some of the folders with non-actively maintained examples from the walk
		if "research_projects" in directories:
			directories.remove('research_projects'  )
		if "legacy" in directories:
			directories.remove('legacy'  )
		for fname in fnames:
			if fname.endswith('.py'  ):
				update_version_in_file(os.path.join(snake_case__						,   snake_case__  )						,   snake_case__						,   pattern='examples'  )






def    _snake_case					(		snake_case__ :  int						,   snake_case__ :  int=False  ):
	for pattern, fname in REPLACE_FILES.items():
		update_version_in_file(snake_case__						,   snake_case__						,   snake_case__  )
	if not patch:
		update_version_in_examples(snake_case__  )






def    _snake_case					(		):
	A     = '🤗 Transformers currently provides the following architectures'
	A     = '1. Want to contribute a new model?'
	with open(snake_case__						,   'r'						,   encoding='utf-8'						,   newline='\n'  ) as f:
		A     = f.readlines()

	# Find the start of the list.
	A     = 0
	while not lines[start_index].startswith(_start_prompt  ):
		start_index += 1
	start_index += 1

	A     = start_index
	# Update the lines in the model list.
	while not lines[index].startswith(_end_prompt  ):
		if lines[index].startswith('1.'  ):
			A     = lines[index].replace(
			    'https://huggingface.co/docs/transformers/main/model_doc'						,   'https://huggingface.co/docs/transformers/model_doc'						,   )
		index += 1

	with open(snake_case__						,   'w'						,   encoding='utf-8'						,   newline='\n'  ) as f:
		f.writelines(snake_case__  )






def    _snake_case					(		):
	with open(REPLACE_FILES['init']						,   'r'  ) as f:
		A     = f.read()
	A     = REPLACE_PATTERNS['init'][0].search(snake_case__  ).groups()[0]
	return packaging.version.parse(snake_case__  )






def    _snake_case					(		snake_case__ :  Optional[Any]=False  ):
	A     = get_version()
	if patch and default_version.is_devrelease:
		raise ValueError('Can\'t create a patch version from the dev branch, checkout a released version!'  )
	if default_version.is_devrelease:
		A     = default_version.base_version
	elif patch:
		A     = F'{default_version.major}.{default_version.minor}.{default_version.micro + 1}'
	else:
		A     = F'{default_version.major}.{default_version.minor + 1}.0'

	# Now let's ask nicely if that's the right one.
	A     = input(F'Which version are you releasing? [{default_version}]'  )
	if len(snake_case__  ) == 0:
		A     = default_version

	print(F'Updating version to {version}.'  )
	global_version_update(snake_case__						,   patch=snake_case__  )
	if not patch:
		print('Cleaning main README, don\'t forget to run `make fix-copies`.'  )
		clean_main_ref_in_model_list()






def    _snake_case					(		):
	A     = get_version()
	A     = F'{current_version.major}.{current_version.minor + 1}.0.dev0'
	A     = current_version.base_version

	# Check with the user we got that right.
	A     = input(F'Which version are we developing now? [{dev_version}]'  )
	if len(snake_case__  ) == 0:
		A     = dev_version

	print(F'Updating version to {version}.'  )
	global_version_update(snake_case__  )
	print('Cleaning main README, don\'t forget to run `make fix-copies`.'  )
	clean_main_ref_in_model_list()


if __name__ == "__main__":
		_lowercase				 =					argparse.ArgumentParser()
		parser.add_argument('''--post_release''', action='''store_true''', help='''Whether this is pre or post release.''')
		parser.add_argument('''--patch''', action='''store_true''', help='''Whether or not this is a patch release.''')
		_lowercase				 =					parser.parse_args()
		if not args.post_release:
				pre_release_work(patch=args.patch)
		elif args.patch:
				print('''Nothing to do after a patch :-)''')
		else:
				post_release_work()

"""simple docstring"""


import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


_lowercase				 =					logging.get_logger(__name__)

_lowercase				 =					'''▁'''

_lowercase				 =					{'''vocab_file''': '''sentencepiece.bpe.model'''}

_lowercase				 =					{
    '''vocab_file''': {
        '''xlm-roberta-base''': '''https://huggingface.co/xlm-roberta-base/resolve/main/sentencepiece.bpe.model''',
        '''xlm-roberta-large''': '''https://huggingface.co/xlm-roberta-large/resolve/main/sentencepiece.bpe.model''',
        '''xlm-roberta-large-finetuned-conll02-dutch''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-dutch/resolve/main/sentencepiece.bpe.model'''
        ),
        '''xlm-roberta-large-finetuned-conll02-spanish''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll02-spanish/resolve/main/sentencepiece.bpe.model'''
        ),
        '''xlm-roberta-large-finetuned-conll03-english''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-english/resolve/main/sentencepiece.bpe.model'''
        ),
        '''xlm-roberta-large-finetuned-conll03-german''': (
            '''https://huggingface.co/xlm-roberta-large-finetuned-conll03-german/resolve/main/sentencepiece.bpe.model'''
        ),
    }
}

_lowercase				 =					{
    '''xlm-roberta-base''': 5_12,
    '''xlm-roberta-large''': 5_12,
    '''xlm-roberta-large-finetuned-conll02-dutch''': 5_12,
    '''xlm-roberta-large-finetuned-conll02-spanish''': 5_12,
    '''xlm-roberta-large-finetuned-conll03-english''': 5_12,
    '''xlm-roberta-large-finetuned-conll03-german''': 5_12,
}







class 					lowerCAmelCase_					(   _lowercase     ):



						'''simple docstring'''


						_lowerCamelCase:     Optional[Any]   =     VOCAB_FILES_NAMES
						_lowerCamelCase:     List[str]   =     PRETRAINED_VOCAB_FILES_MAP
						_lowerCamelCase:     Union[str, Any]   =     PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						_lowerCamelCase:     Any   =     ['''input_ids''', '''attention_mask''']


						def __init__(			self  :   Union[str, Any]	,A_  :   str	,A_  :   str="<s>"	,A_  :   Any="</s>"	,A_  :   Tuple="</s>"	,A_  :   Any="<s>"	,A_  :   Optional[Any]="<unk>"	,A_  :   int="<pad>"	,A_  :   str="<mask>"	,A_  :   Optional[Dict[str, Any]] = None	,**A_  :   Optional[int]	,)  -> None:
							# Mask token behave like a normal word, i.e. include the space before it
							A     = AddedToken(A_	,lstrip=A_	,rstrip=A_   ) if isinstance(A_	,A_   ) else mask_token

							A     = {} if sp_model_kwargs is None else sp_model_kwargs

							super().__init__(
							    bos_token=A_	,eos_token=A_	,unk_token=A_	,sep_token=A_	,cls_token=A_	,pad_token=A_	,mask_token=A_	,sp_model_kwargs=self.sp_model_kwargs	,**A_	,)

							A     = spm.SentencePieceProcessor(**self.sp_model_kwargs   )
							self.sp_model.Load(str(A_   )   )
							A     = vocab_file

							# Original fairseq vocab and spm vocab must be "aligned":
							# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
							# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
							# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
							# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

							# Mimic fairseq token-to-id alignment for the first 4 token
							A     = {'<s>': 0, '<pad>': 1, '</s>': 2, '<unk>': 3}

							# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
							A     = 1

							A     = len(self.sp_model   ) + self.fairseq_offset
							A     = {v: k for k, v in self.fairseq_tokens_to_ids.items()}


						def __getstate__(			self  :   Union[str, Any]   )  -> Any:
							A     = self.__dict__.copy()
							A     = None
							A     = self.sp_model.serialized_model_proto()
							return state


						def __setstate__(			self  :   str	,A_  :   str   )  -> Optional[Any]:
							A     = d

							# for backward compatibility
							if not hasattr(self	,'sp_model_kwargs'   ):
								A     = {}

							A     = spm.SentencePieceProcessor(**self.sp_model_kwargs   )
							self.sp_model.LoadFromSerializedProto(self.sp_model_proto   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   Tuple	,A_  :   List[int]	,A_  :   Optional[List[int]] = None   )  -> List[int]:

							if token_ids_a is None:
								return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
							A     = [self.cls_token_id]
							A     = [self.sep_token_id]
							return cls + token_ids_a + sep + sep + token_ids_a + sep


						def     _SCREAMING_SNAKE_CASE   (			self  :   List[Any]	,A_  :   List[int]	,A_  :   Optional[List[int]] = None	,A_  :   bool = False   )  -> List[int]:

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

							if token_ids_a is None:
								return [1] + ([0] * len(A_   )) + [1]
							return [1] + ([0] * len(A_   )) + [1, 1] + ([0] * len(A_   )) + [1]


						def     _SCREAMING_SNAKE_CASE   (			self  :   str	,A_  :   List[int]	,A_  :   Optional[List[int]] = None   )  -> List[int]:
							A     = [self.sep_token_id]
							A     = [self.cls_token_id]

							if token_ids_a is None:
								return len(cls + token_ids_a + sep   ) * [0]
							return len(cls + token_ids_a + sep + sep + token_ids_a + sep   ) * [0]


						@property
						def     _SCREAMING_SNAKE_CASE   (			self  :   List[str]   )  -> Optional[Any]:
							return len(self.sp_model   ) + self.fairseq_offset + 1  # Add the <mask> token


						def     _SCREAMING_SNAKE_CASE   (			self  :   Dict   )  -> Optional[Any]:
							A     = {self.convert_ids_to_tokens(A_   ): i for i in range(self.vocab_size   )}
							vocab.update(self.added_tokens_encoder   )
							return vocab


						def     _SCREAMING_SNAKE_CASE   (			self  :   str	,A_  :   str   )  -> List[str]:
							return self.sp_model.encode(A_	,out_type=A_   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   Tuple	,A_  :   List[Any]   )  -> Tuple:
							if token in self.fairseq_tokens_to_ids:
								return self.fairseq_tokens_to_ids[token]
							A     = self.sp_model.PieceToId(A_   )

							# Need to return unknown token if the SP model returned 0
							return spm_id + self.fairseq_offset if spm_id else self.unk_token_id


						def     _SCREAMING_SNAKE_CASE   (			self  :   str	,A_  :   str   )  -> int:
							if index in self.fairseq_ids_to_tokens:
								return self.fairseq_ids_to_tokens[index]
							return self.sp_model.IdToPiece(index - self.fairseq_offset   )


						def     _SCREAMING_SNAKE_CASE   (			self  :   List[str]	,A_  :   Optional[Any]   )  -> List[Any]:
							A     = ''.join(A_   ).replace(A_	,' '   ).strip()
							return out_string




						def     _SCREAMING_SNAKE_CASE   (			self  :   Union[str, Any]	,A_  :   str	,A_  :   Optional[str] = None   )  -> Tuple[str]:
							if not os.path.isdir(A_   ):
								logger.error(F'Vocabulary path ({save_directory}) should be a directory'   )
								return
							A     = os.path.join(
							    A_	,(filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']   )

							if os.path.abspath(self.vocab_file   ) != os.path.abspath(A_   ) and os.path.isfile(self.vocab_file   ):
								copyfile(self.vocab_file	,A_   )
							elif not os.path.isfile(self.vocab_file   ):
								with open(A_	,'wb'   ) as fi:
									A     = self.sp_model.serialized_model_proto()
									fi.write(A_   )

							return (out_vocab_file,)