Tokenize and convert raw text to ids. Args: text: String to be tokenized. tokenizer: An instance of Tokenizer class. Returns: tokens: List of strings. char_to_token_map: List of token ids mapping each character in the original string to the first and last index of the spl
(text, tokenizer)
| 33 | |
| 34 | |
| 35 | def preprocess_text(text, tokenizer): |
| 36 | """Tokenize and convert raw text to ids. |
| 37 | |
| 38 | Args: |
| 39 | text: String to be tokenized. |
| 40 | tokenizer: An instance of Tokenizer class. |
| 41 | |
| 42 | Returns: |
| 43 | tokens: List of strings. |
| 44 | char_to_token_map: List of token ids mapping each character in the original |
| 45 | string to the first and last index of the split tokens it appears in. |
| 46 | """ |
| 47 | # Split by whitespace. |
| 48 | wspace_tokens = [] |
| 49 | char_to_wspace = [] |
| 50 | prev_is_whitespace = True |
| 51 | for c in text: |
| 52 | if is_whitespace(c): |
| 53 | prev_is_whitespace = True |
| 54 | else: |
| 55 | if prev_is_whitespace: |
| 56 | wspace_tokens.append(c) |
| 57 | else: |
| 58 | wspace_tokens[-1] += c |
| 59 | prev_is_whitespace = False |
| 60 | char_to_wspace.append(len(wspace_tokens) - 1) |
| 61 | |
| 62 | # Tokenize each split token. |
| 63 | orig_to_tok_index = [] |
| 64 | tokens = [] |
| 65 | for token in wspace_tokens: |
| 66 | orig_to_tok_index.append(len(tokens)) |
| 67 | sub_tokens = tokenizer.tokenize(token) |
| 68 | for sub_token in sub_tokens: |
| 69 | tokens.append(sub_token) |
| 70 | |
| 71 | # Map characters to tokens. |
| 72 | char_to_token_map = [] |
| 73 | for cc in char_to_wspace: |
| 74 | st = orig_to_tok_index[cc] |
| 75 | if cc == len(orig_to_tok_index) - 1: |
| 76 | en = len(tokens) - 1 |
| 77 | else: |
| 78 | en = orig_to_tok_index[cc + 1] - 1 |
| 79 | char_to_token_map.append((st, en)) |
| 80 | |
| 81 | return tokens, char_to_token_map |
| 82 | |
| 83 | |
| 84 | class FastPredict: |
nothing calls this directly
no test coverage detected
searching dependent graphs…