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

Method tokenize

bert/tokenization_bert.py:371–401  ·  view source on GitHub ↗

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

(self, text, never_split=None)

Source from the content-addressed store, hash-verified

369 self.tokenize_chinese_chars = tokenize_chinese_chars
370
371 def tokenize(self, text, never_split=None):
372 """ Basic Tokenization of a piece of text.
373 Split on "white spaces" only, for sub-word tokenization, see WordPieceTokenizer.
374
375 Args:
376 **never_split**: (`optional`) list of str
377 Kept for backward compatibility purposes.
378 Now implemented directly at the base class level (see :func:`PreTrainedTokenizer.tokenize`)
379 List of token not to split.
380 """
381 # union() returns a new set by concatenating the two sets.
382 never_split = self.never_split.union(set(never_split)) if never_split else self.never_split
383
384 # This was added on November 1st, 2018 for the multilingual and Chinese
385 # models. This is also applied to the English models now, but it doesn't
386 # matter since the English models were not trained on any Chinese data
387 # and generally don't have any Chinese data in them (there are Chinese
388 # characters in the vocabulary because Wikipedia does have some Chinese
389 # words in the English Wikipedia.).
390 if self.tokenize_chinese_chars:
391 text = self._tokenize_chinese_chars(text)
392 orig_tokens = whitespace_tokenize(text)
393 split_tokens = []
394 for token in orig_tokens:
395 if self.do_lower_case and token not in never_split:
396 token = token.lower()
397 token = self._run_strip_accents(token)
398 split_tokens.extend(self._run_split_on_punc(token, never_split))
399
400 output_tokens = whitespace_tokenize(" ".join(split_tokens))
401 return output_tokens
402
403 def _run_strip_accents(self, text):
404 """Strips accents from a piece of text."""

Callers 1

_tokenizeMethod · 0.45

Calls 4

_run_strip_accentsMethod · 0.95
_run_split_on_puncMethod · 0.95
whitespace_tokenizeFunction · 0.85

Tested by

no test coverage detected