Encode whitespaces to extra tokens. >>> encode_whitespaces('a\\n b\\n c', 10, 10) 'a\\n<|extratoken_10|>b\\n<|extratoken_11|>c'
(text: str, start_extra_id: int, max_len: int)
| 5 | |
| 6 | |
| 7 | def encode_whitespaces(text: str, start_extra_id: int, max_len: int): |
| 8 | """ Encode whitespaces to extra tokens. |
| 9 | |
| 10 | >>> encode_whitespaces('a\\n b\\n c', 10, 10) |
| 11 | 'a\\n<|extratoken_10|>b\\n<|extratoken_11|>c' |
| 12 | """ |
| 13 | for i in np.arange(max_len, 1, -1): |
| 14 | text = text.replace(" " * i, f"<|extratoken_{start_extra_id + i - 2}|>") |
| 15 | return text |
| 16 | |
| 17 | |
| 18 | def decode_whitespaces(text: str, start_extra_id: int, max_len: int): |