|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""Tokenization classes for CharacterBERT.""" |
|
import json |
|
import os |
|
import unicodedata |
|
from collections import OrderedDict |
|
from typing import Dict, List, Optional, Tuple, Union |
|
|
|
import numpy as np |
|
|
|
from transformers.file_utils import is_tf_available, is_torch_available, to_py_obj |
|
from transformers.tokenization_utils import ( |
|
BatchEncoding, |
|
EncodedInput, |
|
PaddingStrategy, |
|
PreTrainedTokenizer, |
|
TensorType, |
|
_is_control, |
|
_is_punctuation, |
|
_is_whitespace, |
|
) |
|
from transformers.tokenization_utils_base import ADDED_TOKENS_FILE |
|
from transformers.utils import logging |
|
|
|
|
|
logger = logging.get_logger(__name__) |
|
|
|
VOCAB_FILES_NAMES = { |
|
"mlm_vocab_file": "mlm_vocab.txt", |
|
} |
|
|
|
PRETRAINED_VOCAB_FILES_MAP = { |
|
"mlm_vocab_file": { |
|
"helboukkouri/character-bert": "https://huggingface.co/helboukkouri/character-bert/resolve/main/mlm_vocab.txt", |
|
"helboukkouri/character-bert-medical": "https://huggingface.co/helboukkouri/character-bert-medical/resolve/main/mlm_vocab.txt", |
|
} |
|
} |
|
|
|
PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES = { |
|
"helboukkouri/character-bert": 512, |
|
"helboukkouri/character-bert-medical": 512, |
|
} |
|
|
|
PRETRAINED_INIT_CONFIGURATION = { |
|
"helboukkouri/character-bert": {"max_word_length": 50, "do_lower_case": True}, |
|
"helboukkouri/character-bert-medical": {"max_word_length": 50, "do_lower_case": True}, |
|
} |
|
|
|
PAD_TOKEN_CHAR_ID = 0 |
|
|
|
|
|
def whitespace_tokenize(text): |
|
"""Runs basic whitespace cleaning and splitting on a piece of text.""" |
|
text = text.strip() |
|
if not text: |
|
return [] |
|
tokens = text.split() |
|
return tokens |
|
|
|
|
|
def build_mlm_ids_to_tokens_mapping(mlm_vocab_file): |
|
"""Builds a Masked Language Modeling ids to masked tokens mapping.""" |
|
vocabulary = [] |
|
with open(mlm_vocab_file, "r", encoding="utf-8") as reader: |
|
for line in reader: |
|
line = line.strip() |
|
if line: |
|
vocabulary.append(line) |
|
return OrderedDict(list(enumerate(vocabulary))) |
|
|
|
|
|
class CharacterBertTokenizer(PreTrainedTokenizer): |
|
""" |
|
Construct a CharacterBERT tokenizer. Based on characters. |
|
|
|
This tokenizer inherits from [`PreTrainedTokenizer`] which contains most of the main methods. |
|
Users should refer to this superclass for more information regarding those methods. |
|
|
|
Args: |
|
mlm_vocab_file (`str`, *optional*, defaults to `None`): |
|
Path to the Masked Language Modeling vocabulary. This is used for converting the output (token ids) of the |
|
MLM model into tokens. |
|
max_word_length (`int`, *optional*, defaults to `50`): |
|
The maximum token length in characters (actually, in bytes as any non-ascii characters will be converted to |
|
a sequence of utf-8 bytes). |
|
do_lower_case (`bool`, *optional*, defaults to `True`): |
|
Whether or not to lowercase the input when tokenizing. |
|
do_basic_tokenize (`bool`, *optional*, defaults to `True`): |
|
Whether or not to do basic tokenization before WordPiece. |
|
never_split (`Iterable`, *optional*): |
|
Collection of tokens which will never be split during tokenization. Only has an effect when |
|
`do_basic_tokenize=True` |
|
unk_token (`str`, *optional*, defaults to `"[UNK]"`): |
|
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. |
|
sep_token (`str`, *optional*, defaults to `"[SEP]"`): |
|
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. |
|
pad_token (`str`, *optional*, defaults to `"[PAD]"`): |
|
The token used for padding, for example when batching sequences of different lengths. |
|
cls_token (`str`, *optional*, defaults to `"[CLS]"`): |
|
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. |
|
mask_token (`str`, *optional*, defaults to `"[MASK]"`): |
|
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. |
|
tokenize_chinese_chars (`bool`, *optional*, defaults to `True`): |
|
Whether or not to tokenize Chinese characters. |
|
strip_accents: (`bool`, *optional*): |
|
Whether or not to strip all accents. If this option is not specified, then it will be determined by the |
|
value for `lowercase` (as in the original BERT). |
|
""" |
|
|
|
vocab_files_names = VOCAB_FILES_NAMES |
|
pretrained_vocab_files_map = PRETRAINED_VOCAB_FILES_MAP |
|
pretrained_init_configuration = PRETRAINED_INIT_CONFIGURATION |
|
max_model_input_sizes = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES |
|
|
|
def __init__( |
|
self, |
|
mlm_vocab_file=None, |
|
max_word_length=50, |
|
do_lower_case=True, |
|
do_basic_tokenize=True, |
|
never_split=None, |
|
unk_token="[UNK]", |
|
sep_token="[SEP]", |
|
pad_token="[PAD]", |
|
cls_token="[CLS]", |
|
mask_token="[MASK]", |
|
tokenize_chinese_chars=True, |
|
strip_accents=None, |
|
**kwargs |
|
): |
|
super().__init__( |
|
max_word_length=max_word_length, |
|
do_lower_case=do_lower_case, |
|
do_basic_tokenize=do_basic_tokenize, |
|
never_split=never_split, |
|
unk_token=unk_token, |
|
sep_token=sep_token, |
|
pad_token=pad_token, |
|
cls_token=cls_token, |
|
mask_token=mask_token, |
|
tokenize_chinese_chars=tokenize_chinese_chars, |
|
strip_accents=strip_accents, |
|
**kwargs, |
|
) |
|
|
|
self.unique_no_split_tokens = [self.cls_token, self.mask_token, self.pad_token, self.sep_token, self.unk_token] |
|
|
|
if mlm_vocab_file is None: |
|
self.ids_to_tokens = None |
|
else: |
|
if not os.path.isfile(mlm_vocab_file): |
|
raise ValueError( |
|
f"Can't find a vocabulary file at path '{mlm_vocab_file}'. " |
|
"To load the vocabulary from a pretrained model use " |
|
"`tokenizer = CharacterBertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" |
|
) |
|
self.ids_to_tokens = build_mlm_ids_to_tokens_mapping(mlm_vocab_file) |
|
|
|
self.do_basic_tokenize = do_basic_tokenize |
|
if do_basic_tokenize: |
|
self.basic_tokenizer = BasicTokenizer( |
|
do_lower_case=do_lower_case, |
|
never_split=never_split, |
|
tokenize_chinese_chars=tokenize_chinese_chars, |
|
strip_accents=strip_accents, |
|
) |
|
|
|
self.max_word_length = max_word_length |
|
self._mapper = CharacterMapper(max_word_length=max_word_length) |
|
|
|
def __repr__(self) -> str: |
|
|
|
return ( |
|
f"CharacterBertTokenizer(name_or_path='{self.name_or_path}', " |
|
+ (f"mlm_vocab_size={self.mlm_vocab_size}, " if self.ids_to_tokens else "") |
|
+ f"model_max_len={self.model_max_length}, is_fast={self.is_fast}, " |
|
+ f"padding_side='{self.padding_side}', special_tokens={self.special_tokens_map_extended})" |
|
) |
|
|
|
def __len__(self): |
|
""" |
|
Size of the full vocabulary with the added tokens. |
|
""" |
|
|
|
return 0 + len(self.added_tokens_encoder) |
|
|
|
@property |
|
def do_lower_case(self): |
|
return self.basic_tokenizer.do_lower_case |
|
|
|
@property |
|
def vocab_size(self): |
|
raise NotImplementedError("CharacterBERT does not use a token vocabulary.") |
|
|
|
@property |
|
def mlm_vocab_size(self): |
|
if self.ids_to_tokens is None: |
|
raise ValueError( |
|
"CharacterBertTokenizer was initialized without a MLM " |
|
"vocabulary. You can either pass one manually or load a " |
|
"pre-trained tokenizer using: " |
|
"`tokenizer = CharacterBertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" |
|
) |
|
return len(self.ids_to_tokens) |
|
|
|
def add_special_tokens(self, *args, **kwargs): |
|
raise NotImplementedError("Adding special tokens is not supported for now.") |
|
|
|
def add_tokens(self, *args, **kwargs): |
|
|
|
|
|
pass |
|
|
|
def get_vocab(self): |
|
raise NotImplementedError("CharacterBERT does not have a token vocabulary.") |
|
|
|
def get_mlm_vocab(self): |
|
return {token: i for i, token in self.ids_to_tokens.items()} |
|
|
|
def _tokenize(self, text): |
|
split_tokens = [] |
|
if self.do_basic_tokenize: |
|
split_tokens = self.basic_tokenizer.tokenize(text=text, never_split=self.all_special_tokens) |
|
else: |
|
split_tokens = whitespace_tokenize(text) |
|
return split_tokens |
|
|
|
def convert_tokens_to_string(self, tokens): |
|
"""Converts a sequence of tokens (string) in a single string.""" |
|
out_string = " ".join(tokens).strip() |
|
return out_string |
|
|
|
def _convert_token_to_id(self, token): |
|
"""Converts a token (str) into a sequence of character ids.""" |
|
return self._mapper.convert_word_to_char_ids(token) |
|
|
|
def _convert_id_to_token(self, index: List[int]): |
|
|
|
|
|
"""Converts an index (actually, a list of indices) in a token (str).""" |
|
return self._mapper.convert_char_ids_to_word(index) |
|
|
|
def convert_ids_to_tokens( |
|
self, ids: Union[List[int], List[List[int]]], skip_special_tokens: bool = False |
|
) -> Union[str, List[str]]: |
|
""" |
|
Converts a single sequence of character indices or a sequence of character id sequences in a token or a |
|
sequence of tokens. |
|
|
|
Args: |
|
ids (`int` or `List[int]`): |
|
The token id (or token ids) to convert to tokens. |
|
skip_special_tokens (`bool`, *optional*, defaults to `False`): |
|
Whether or not to remove special tokens in the decoding. |
|
|
|
Returns: |
|
`str` or `List[str]`: The decoded token(s). |
|
""" |
|
if isinstance(ids, list) and isinstance(ids[0], int): |
|
if tuple(ids) in self.added_tokens_decoder: |
|
return self.added_tokens_decoder[tuple(ids)] |
|
else: |
|
return self._convert_id_to_token(ids) |
|
tokens = [] |
|
for indices in ids: |
|
indices = list(map(int, indices)) |
|
if skip_special_tokens and tuple(indices) in self.all_special_ids: |
|
continue |
|
if tuple(indices) in self.added_tokens_decoder: |
|
tokens.append(self.added_tokens_decoder[tuple(indices)]) |
|
else: |
|
tokens.append(self._convert_id_to_token(indices)) |
|
return tokens |
|
|
|
def convert_mlm_id_to_token(self, mlm_id): |
|
"""Converts an index (integer) in a token (str) using the vocab.""" |
|
if self.ids_to_tokens is None: |
|
raise ValueError( |
|
"CharacterBertTokenizer was initialized without a MLM " |
|
"vocabulary. You can either pass one manually or load a " |
|
"pre-trained tokenizer using: " |
|
"`tokenizer = CharacterBertTokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`" |
|
) |
|
assert ( |
|
mlm_id < self.mlm_vocab_size |
|
), "Attempting to convert a MLM id that is greater than the MLM vocabulary size." |
|
return self.ids_to_tokens[mlm_id] |
|
|
|
def build_inputs_with_special_tokens( |
|
self, token_ids_0: List[List[int]], token_ids_1: Optional[List[List[int]]] = None |
|
) -> List[List[int]]: |
|
""" |
|
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and |
|
adding special tokens. A CharacterBERT sequence has the following format: |
|
|
|
- single sequence: `[CLS] X [SEP]` |
|
- pair of sequences: `[CLS] A [SEP] B [SEP]` |
|
|
|
Args: |
|
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: |
|
`List[int]`: List of [input IDs](../glossary#input-ids) with the appropriate special tokens. |
|
""" |
|
if token_ids_1 is None: |
|
return [self.cls_token_id] + token_ids_0 + [self.sep_token_id] |
|
cls = [self.cls_token_id] |
|
sep = [self.sep_token_id] |
|
return cls + token_ids_0 + sep + token_ids_1 + sep |
|
|
|
def get_special_tokens_mask( |
|
self, |
|
token_ids_0: List[List[int]], |
|
token_ids_1: Optional[List[List[int]]] = None, |
|
already_has_special_tokens: bool = False, |
|
) -> List[int]: |
|
""" |
|
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. |
|
|
|
Args: |
|
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: |
|
`List[int]`: A list of integers in the range [0, 1]: 1 for a special token, 0 for a sequence token. |
|
""" |
|
if already_has_special_tokens: |
|
if token_ids_1 is not None: |
|
raise ValueError( |
|
"You should not supply a second sequence if the provided sequence of " |
|
"ids is already formatted with special tokens for the model." |
|
) |
|
return list(map(lambda x: 1 if x in [self.sep_token_id, self.cls_token_id] else 0, token_ids_0)) |
|
|
|
if token_ids_1 is not None: |
|
return [1] + ([0] * len(token_ids_0)) + [1] + ([0] * len(token_ids_1)) + [1] |
|
return [1] + ([0] * len(token_ids_0)) + [1] |
|
|
|
def create_token_type_ids_from_sequences( |
|
self, token_ids_0: List[List[int]], token_ids_1: Optional[List[List[int]]] = None |
|
) -> List[int]: |
|
""" |
|
Create a mask from the two sequences passed to be used in a sequence-pair classification task. A CharacterBERT |
|
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). |
|
|
|
Args: |
|
token_ids_0 (`List[int]`): |
|
List of IDs. |
|
token_ids_1 (`List[int]`, *optional*): |
|
Optional second list of IDs for sequence pairs. |
|
|
|
Returns: |
|
`List[int]`: List of [token type IDs](../glossary#token-type-ids) according to the given |
|
sequence(s). |
|
""" |
|
sep = [self.sep_token_id] |
|
cls = [self.cls_token_id] |
|
if token_ids_1 is None: |
|
return len(cls + token_ids_0 + sep) * [0] |
|
return len(cls + token_ids_0 + sep) * [0] + len(token_ids_1 + sep) * [1] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: |
|
logger.warning("CharacterBERT does not have a token vocabulary. " "Skipping saving `vocab.txt`.") |
|
return () |
|
|
|
def save_mlm_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str]: |
|
|
|
|
|
if os.path.isdir(save_directory): |
|
vocab_file = os.path.join( |
|
save_directory, (filename_prefix + "-" if filename_prefix else "") + "mlm_vocab.txt" |
|
) |
|
else: |
|
vocab_file = (filename_prefix + "-" if filename_prefix else "") + save_directory |
|
with open(vocab_file, "w", encoding="utf-8") as f: |
|
for _, token in self.ids_to_tokens.items(): |
|
f.write(token + "\n") |
|
return (vocab_file,) |
|
|
|
def _save_pretrained( |
|
self, |
|
save_directory: Union[str, os.PathLike], |
|
file_names: Tuple[str], |
|
legacy_format: Optional[bool] = None, |
|
filename_prefix: Optional[str] = None, |
|
) -> Tuple[str]: |
|
""" |
|
Save a tokenizer using the slow-tokenizer/legacy format: vocabulary + added tokens. |
|
|
|
Fast tokenizers can also be saved in a unique JSON file containing {config + vocab + added-tokens} using the |
|
specific [`~tokenization_utils_fast.PreTrainedTokenizerFast._save_pretrained`] |
|
""" |
|
if legacy_format is False: |
|
raise ValueError( |
|
"Only fast tokenizers (instances of PreTrainedTokenizerFast) can be saved in non legacy format." |
|
) |
|
|
|
save_directory = str(save_directory) |
|
|
|
added_tokens_file = os.path.join( |
|
save_directory, (filename_prefix + "-" if filename_prefix else "") + ADDED_TOKENS_FILE |
|
) |
|
added_vocab = self.get_added_vocab() |
|
if added_vocab: |
|
with open(added_tokens_file, "w", encoding="utf-8") as f: |
|
out_str = json.dumps(added_vocab, ensure_ascii=False) |
|
f.write(out_str) |
|
logger.info(f"added tokens file saved in {added_tokens_file}") |
|
|
|
vocab_files = self.save_mlm_vocabulary(save_directory, filename_prefix=filename_prefix) |
|
|
|
return file_names + vocab_files + (added_tokens_file,) |
|
|
|
|
|
class BasicTokenizer(object): |
|
""" |
|
Constructs a BasicTokenizer that will run basic tokenization (punctuation splitting, lower casing, etc.). |
|
|
|
Args: |
|
do_lower_case (`bool`, *optional*, defaults to `True`): |
|
Whether or not to lowercase the input when tokenizing. |
|
never_split (`Iterable`, *optional*): |
|
Collection of tokens which will never be split during tokenization. Only has an effect when |
|
`do_basic_tokenize=True` |
|
tokenize_chinese_chars (`bool`, *optional*, defaults to `True`): |
|
Whether or not to tokenize Chinese characters. |
|
|
|
This should likely be deactivated for Japanese (see this [issue](https://github.com/huggingface/transformers/issues/328)). |
|
strip_accents: (`bool`, *optional*): |
|
Whether or not to strip all accents. If this option is not specified, then it will be determined by the |
|
value for `lowercase` (as in the original BERT). |
|
""" |
|
|
|
def __init__(self, do_lower_case=True, never_split=None, tokenize_chinese_chars=True, strip_accents=None): |
|
if never_split is None: |
|
never_split = [] |
|
self.do_lower_case = do_lower_case |
|
self.never_split = set(never_split) |
|
self.tokenize_chinese_chars = tokenize_chinese_chars |
|
self.strip_accents = strip_accents |
|
|
|
def tokenize(self, text, never_split=None): |
|
""" |
|
Basic Tokenization of a piece of text. Split on "white spaces" only, for sub-word tokenization, see |
|
WordPieceTokenizer. |
|
|
|
Args: |
|
**never_split**: (*optional*) list of str |
|
Kept for backward compatibility purposes. Now implemented directly at the base class level (see |
|
[`PreTrainedTokenizer.tokenize`]) List of token not to split. |
|
""" |
|
|
|
never_split = self.never_split.union(set(never_split)) if never_split else self.never_split |
|
text = self._clean_text(text) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if self.tokenize_chinese_chars: |
|
text = self._tokenize_chinese_chars(text) |
|
orig_tokens = whitespace_tokenize(text) |
|
split_tokens = [] |
|
for token in orig_tokens: |
|
if token not in never_split: |
|
if self.do_lower_case: |
|
token = token.lower() |
|
if self.strip_accents is not False: |
|
token = self._run_strip_accents(token) |
|
elif self.strip_accents: |
|
token = self._run_strip_accents(token) |
|
split_tokens.extend(self._run_split_on_punc(token, never_split)) |
|
|
|
output_tokens = whitespace_tokenize(" ".join(split_tokens)) |
|
return output_tokens |
|
|
|
def _run_strip_accents(self, text): |
|
"""Strips accents from a piece of text.""" |
|
text = unicodedata.normalize("NFD", text) |
|
output = [] |
|
for char in text: |
|
cat = unicodedata.category(char) |
|
if cat == "Mn": |
|
continue |
|
output.append(char) |
|
return "".join(output) |
|
|
|
def _run_split_on_punc(self, text, never_split=None): |
|
"""Splits punctuation on a piece of text.""" |
|
if never_split is not None and text in never_split: |
|
return [text] |
|
chars = list(text) |
|
i = 0 |
|
start_new_word = True |
|
output = [] |
|
while i < len(chars): |
|
char = chars[i] |
|
if _is_punctuation(char): |
|
output.append([char]) |
|
start_new_word = True |
|
else: |
|
if start_new_word: |
|
output.append([]) |
|
start_new_word = False |
|
output[-1].append(char) |
|
i += 1 |
|
|
|
return ["".join(x) for x in output] |
|
|
|
def _tokenize_chinese_chars(self, text): |
|
"""Adds whitespace around any CJK character.""" |
|
output = [] |
|
for char in text: |
|
cp = ord(char) |
|
if self._is_chinese_char(cp): |
|
output.append(" ") |
|
output.append(char) |
|
output.append(" ") |
|
else: |
|
output.append(char) |
|
return "".join(output) |
|
|
|
def _is_chinese_char(self, cp): |
|
"""Checks whether CP is the codepoint of a CJK character.""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ( |
|
(cp >= 0x4E00 and cp <= 0x9FFF) |
|
or (cp >= 0x3400 and cp <= 0x4DBF) |
|
or (cp >= 0x20000 and cp <= 0x2A6DF) |
|
or (cp >= 0x2A700 and cp <= 0x2B73F) |
|
or (cp >= 0x2B740 and cp <= 0x2B81F) |
|
or (cp >= 0x2B820 and cp <= 0x2CEAF) |
|
or (cp >= 0xF900 and cp <= 0xFAFF) |
|
or (cp >= 0x2F800 and cp <= 0x2FA1F) |
|
): |
|
return True |
|
|
|
return False |
|
|
|
def _clean_text(self, text): |
|
"""Performs invalid character removal and whitespace cleanup on text.""" |
|
output = [] |
|
for char in text: |
|
cp = ord(char) |
|
if cp == 0 or cp == 0xFFFD or _is_control(char): |
|
continue |
|
if _is_whitespace(char): |
|
output.append(" ") |
|
else: |
|
output.append(char) |
|
return "".join(output) |
|
|
|
|
|
class CharacterMapper: |
|
""" |
|
NOTE: Adapted from ElmoCharacterMapper: |
|
https://github.com/allenai/allennlp/blob/main/allennlp/data/token_indexers/elmo_indexer.py Maps individual tokens |
|
to sequences of character ids, compatible with CharacterBERT. |
|
""" |
|
|
|
|
|
|
|
beginning_of_sentence_character = 256 |
|
end_of_sentence_character = 257 |
|
beginning_of_word_character = 258 |
|
end_of_word_character = 259 |
|
padding_character = 260 |
|
mask_character = 261 |
|
|
|
bos_token = "[CLS]" |
|
eos_token = "[SEP]" |
|
pad_token = "[PAD]" |
|
mask_token = "[MASK]" |
|
|
|
def __init__( |
|
self, |
|
max_word_length: int = 50, |
|
): |
|
self.max_word_length = max_word_length |
|
self.beginning_of_sentence_characters = self._make_char_id_sequence(self.beginning_of_sentence_character) |
|
self.end_of_sentence_characters = self._make_char_id_sequence(self.end_of_sentence_character) |
|
self.mask_characters = self._make_char_id_sequence(self.mask_character) |
|
|
|
|
|
self.pad_characters = [PAD_TOKEN_CHAR_ID - 1] * self.max_word_length |
|
|
|
def _make_char_id_sequence(self, character: int): |
|
char_ids = [self.padding_character] * self.max_word_length |
|
char_ids[0] = self.beginning_of_word_character |
|
char_ids[1] = character |
|
char_ids[2] = self.end_of_word_character |
|
return char_ids |
|
|
|
def convert_word_to_char_ids(self, word: str) -> List[int]: |
|
if word == self.bos_token: |
|
char_ids = self.beginning_of_sentence_characters |
|
elif word == self.eos_token: |
|
char_ids = self.end_of_sentence_characters |
|
elif word == self.mask_token: |
|
char_ids = self.mask_characters |
|
elif word == self.pad_token: |
|
char_ids = self.pad_characters |
|
else: |
|
|
|
word_encoded = word.encode("utf-8", "ignore")[: (self.max_word_length - 2)] |
|
|
|
char_ids = [self.padding_character] * self.max_word_length |
|
|
|
char_ids[0] = self.beginning_of_word_character |
|
|
|
for k, chr_id in enumerate(word_encoded, start=1): |
|
char_ids[k] = chr_id |
|
|
|
char_ids[len(word_encoded) + 1] = self.end_of_word_character |
|
|
|
|
|
|
|
|
|
return [c + 1 for c in char_ids] |
|
|
|
def convert_char_ids_to_word(self, char_ids: List[int]) -> str: |
|
"Converts a sequence of character ids into its corresponding word." |
|
|
|
assert len(char_ids) <= self.max_word_length, ( |
|
f"Got character sequence of length {len(char_ids)} while `max_word_length={self.max_word_length}`" |
|
) |
|
|
|
char_ids_ = [(i - 1) for i in char_ids] |
|
if char_ids_ == self.beginning_of_sentence_characters: |
|
return self.bos_token |
|
elif char_ids_ == self.end_of_sentence_characters: |
|
return self.eos_token |
|
elif char_ids_ == self.mask_characters: |
|
return self.mask_token |
|
elif char_ids_ == self.pad_characters: |
|
return self.pad_token |
|
else: |
|
utf8_codes = list( |
|
filter( |
|
lambda x: (x != self.padding_character) |
|
and (x != self.beginning_of_word_character) |
|
and (x != self.end_of_word_character), |
|
char_ids_, |
|
) |
|
) |
|
return bytes(utf8_codes).decode("utf-8") |