tokenizer#

class XLMTokenizer(vocab_file, merges_file, unk_token='<unk>', bos_token='<s>', sep_token='</s>', pad_token='<pad>', cls_token='</s>', mask_token='<special1>', additional_special_tokens=['<special0>', '<special1>', '<special2>', '<special3>', '<special4>', '<special5>', '<special6>', '<special7>', '<special8>', '<special9>'], lang2id=None, id2lang=None, do_lowercase_and_remove_accent=True, **kwargs)[source]#

Bases: PretrainedTokenizer

Construct an XLM tokenizer. Based on Byte-Pair Encoding. The tokenization process is the following: - Moses preprocessing and tokenization for most supported languages. - Language specific tokenization for Chinese (Jieba), Japanese (KyTea) and Thai (PyThaiNLP). - Optionally lowercases and normalizes all inputs text. - The arguments special_tokens and the function set_special_tokens, can be used to add additional symbols (like

“__classify__”) to a vocabulary.

  • The lang2id attribute maps the languages supported by the model with their IDs if provided (automatically set for pretrained vocabularies).

  • The id2lang attributes does reverse mapping if provided (automatically set for pretrained vocabularies).

This tokenizer inherits from PretrainedTokenizer. Users should refer to this superclass for more information regarding those methods.

Parameters:
  • vocab_file (str) – Vocabulary file.

  • merges_file (str) – Merges file.

  • unk_token (str, optional) – The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead. The beginning of sequence token that was used during pretraining. Can be used a sequence classifier token. <Tip> When building a sequence using special tokens, this is not the token that is used for the beginning of sequence. The token used is the cls_token. </Tip> Defaults to "<unk>".

  • sep_token (str, optional) – The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for sequence classification or for a text and a question for question answering. It is also used as the last token of a sequence built with special tokens. Defaults to "</s>".

  • pad_token (str, optional) – The token used for padding, for example when batching sequences of different lengths. Defaults to "<pad>".

  • cls_token (str, optional) – The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens. Defaults to "</s>".

  • mask_token (str, optional) – The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict. Defaults to "<special1>".

  • additional_special_tokens (List[str], optional) – List of additional special tokens. Defaults to ["<special0>","<special1>","<special2>","<special3>","<special4>","<special5>","<special6>","<special7>","<special8>","<special9>"].

  • lang2id (Dict[str, int], optional) – Dictionary mapping languages string identifiers to their IDs.

  • id2lang (Dict[int, str], optional) – Dictionary mapping language IDs to their string identifiers.

  • do_lowercase_and_remove_accent (bool, optional) – Whether to lowercase and remove accents when tokenizing. Defaults to True.

ja_tokenize(text)[source]#

Tokenize a Japanese string.

property vocab_size#

Size of the base vocabulary (without the added tokens).

Type:

int

get_vocab()[source]#

Returns the vocabulary as a dictionary of token to index.

tokenizer.get_vocab()[token] is equivalent to tokenizer.convert_tokens_to_ids(token) when token is in the vocab.

Returns:

The vocabulary.

Return type:

Dict[str, int]

tokenize(text: str, **kwargs) List[str][source]#

Converts a string in a sequence of tokens, using the tokenizer.

Split in words for word-based vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces). Takes care of added tokens.

Parameters:
  • text (str) – The sequence to be encoded.

  • **kwargs (additional keyword arguments) – Passed along to the model-specific prepare_for_tokenization preprocessing method.

Returns:

The list of tokens.

Return type:

List[str]

convert_tokens_to_string(tokens)[source]#

Converts a sequence of tokens (string) in a single string.

build_inputs_with_special_tokens(token_ids_0: List[int], token_ids_1: List[int] | None = None) List[int][source]#

Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. An XLM sequence has the following format: - single sequence: <s> X - pair of sequences: <s> A

Parameters:
  • token_ids_0 (List[int]) – List of IDs to which the special tokens will be added.

  • token_ids_1 (List[int], optional) – Optional second list of IDs for sequence pairs.

Returns:

The model input with special tokens.

Return type:

List[int]

create_token_type_ids_from_sequences(token_ids_0: List[int], token_ids_1: List[int] | None = None) List[int][source]#

Create a mask from the two sequences passed to be used in a sequence-pair classification task. An XLM sequence pair mask has the following format: ` 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 | first sequence    | second sequence | ` If token_ids_1 is None, this method only returns the first portion of the mask (0s). :param token_ids_0: List of IDs. :type token_ids_0: List[int] :param token_ids_1: Optional second list of IDs for sequence pairs. :type token_ids_1: List[int], optional

Returns:

List of [token type IDs] according to the given sequence(s).

Return type:

List[int]

get_special_tokens_mask(token_ids_0: List[int], token_ids_1: List[int] | None = None, already_has_special_tokens: bool = False) List[int][source]#

Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding special tokens using the tokenizer prepare_for_model method.

Parameters:
  • token_ids_0 (List[int]) – List of IDs.

  • token_ids_1 (List[int], optional) – Optional second list of IDs for sequence pairs.

  • already_has_special_tokens (bool, optional, defaults to False) – Whether or not the token list is already formatted with special tokens for the model.

Returns:

1 for a special token, 0 for a sequence token.

Return type:

A list of integers in the range [0, 1]

save_resources(save_directory)[source]#

Save tokenizer related resources to files under save_directory.

Parameters:

save_directory (str) – Directory to save files into.