MCPcopy Create free account
hub / github.com/SooLab/CGFormer / tokenize

Method tokenize

bert/tokenization_utils.py:256–365  ·  view source on GitHub ↗

Converts a string in a sequence of tokens (string), using the tokenizer. Split in words for word-based vocabulary or sub-words for sub-word-based vocabularies (BPE/SentencePieces/WordPieces). Take care of added tokens. Args: text (:o

(self, text: TextInput, **kwargs)

Source from the content-addressed store, hash-verified

254 return len(self.build_inputs_with_special_tokens(token_ids_0, token_ids_1 if pair else None))
255
256 def tokenize(self, text: TextInput, **kwargs):
257 """ Converts a string in a sequence of tokens (string), using the tokenizer.
258 Split in words for word-based vocabulary or sub-words for sub-word-based
259 vocabularies (BPE/SentencePieces/WordPieces).
260
261 Take care of added tokens.
262
263 Args:
264 text (:obj:`string`): The sequence to be encoded.
265 **kwargs (:obj: `dict`): Arguments passed to the model-specific `prepare_for_tokenization` preprocessing method.
266 """
267 # Simple mapping string => AddedToken for special tokens with specific tokenization behaviors
268 all_special_tokens_extended = dict(
269 (str(t), t) for t in self.all_special_tokens_extended if isinstance(t, AddedToken)
270 )
271
272 text, kwargs = self.prepare_for_tokenization(text, **kwargs)
273
274 if kwargs:
275 logger.warning(f"Keyword arguments {kwargs} not recognized.")
276
277 # TODO: should this be in the base class?
278 if self.init_kwargs.get("do_lower_case", False):
279 # convert non-special tokens to lowercase
280 escaped_special_toks = [re.escape(s_tok) for s_tok in self.all_special_tokens]
281 pattern = r"(" + r"|".join(escaped_special_toks) + r")|" + r"(.+?)"
282 text = re.sub(pattern, lambda m: m.groups()[0] or m.groups()[1].lower(), text)
283
284 def split_on_token(tok, text):
285 result = []
286 tok_extended = all_special_tokens_extended.get(tok, None)
287 split_text = text.split(tok)
288 full_word = ""
289 for i, sub_text in enumerate(split_text):
290 # AddedToken can control whitespace stripping around them.
291 # We use them for GPT2 and Roberta to have different behavior depending on the special token
292 # Cf. https://github.com/huggingface/transformers/pull/2778
293 # and https://github.com/huggingface/transformers/issues/3788
294 if isinstance(tok_extended, AddedToken):
295 if tok_extended.single_word:
296 # Try to avoid splitting on token
297 if (
298 i < len(split_text) - 1
299 and not _is_end_of_word(sub_text)
300 and not _is_start_of_word(split_text[i + 1])
301 ):
302 # Don't extract the special token
303 full_word += sub_text + tok
304 elif full_word:
305 full_word += sub_text
306 result += [full_word]
307 full_word = ""
308 continue
309 # Strip white spaces on the right
310 if tok_extended.rstrip and i > 0:
311 # A bit counter-intuitive but we strip the left of the string
312 # since tok_extended.rstrip means the special token is eating all white spaces on its right
313 sub_text = sub_text.lstrip()

Callers 1

get_input_idsMethod · 0.95

Calls 1

Tested by

no test coverage detected