Tokenizes a piece of text into its word pieces. This uses a greedy longest-match-first algorithm to perform tokenization using the given vocabulary. For example: input = "unaffable" output = ["un", "##aff", "##able"] Args: text: A single token or whitespace separ
(self, text)
| 302 | self.max_input_chars_per_word = max_input_chars_per_word |
| 303 | |
| 304 | def tokenize(self, text): |
| 305 | """Tokenizes a piece of text into its word pieces. |
| 306 | |
| 307 | This uses a greedy longest-match-first algorithm to perform tokenization |
| 308 | using the given vocabulary. |
| 309 | |
| 310 | For example: |
| 311 | input = "unaffable" |
| 312 | output = ["un", "##aff", "##able"] |
| 313 | |
| 314 | Args: |
| 315 | text: A single token or whitespace separated tokens. This should have |
| 316 | already been passed through `BasicTokenizer. |
| 317 | |
| 318 | Returns: |
| 319 | A list of wordpiece tokens. |
| 320 | """ |
| 321 | |
| 322 | text = convert_to_unicode(text) |
| 323 | |
| 324 | output_tokens = [] |
| 325 | for token in whitespace_tokenize(text): |
| 326 | chars = list(token) |
| 327 | if len(chars) > self.max_input_chars_per_word: |
| 328 | output_tokens.append(self.unk_token) |
| 329 | continue |
| 330 | |
| 331 | is_bad = False |
| 332 | start = 0 |
| 333 | sub_tokens = [] |
| 334 | while start < len(chars): |
| 335 | end = len(chars) |
| 336 | cur_substr = None |
| 337 | while start < end: |
| 338 | substr = "".join(chars[start:end]) |
| 339 | if start > 0: |
| 340 | substr = "##" + substr |
| 341 | if substr in self.vocab: |
| 342 | cur_substr = substr |
| 343 | break |
| 344 | end -= 1 |
| 345 | if cur_substr is None: |
| 346 | is_bad = True |
| 347 | break |
| 348 | sub_tokens.append(cur_substr) |
| 349 | start = end |
| 350 | |
| 351 | if is_bad: |
| 352 | output_tokens.append(self.unk_token) |
| 353 | else: |
| 354 | output_tokens.extend(sub_tokens) |
| 355 | return output_tokens |
| 356 | |
| 357 | |
| 358 | def _is_whitespace(char): |
nothing calls this directly
no test coverage detected