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=10, max_len: int=10)
| 24 | |
| 25 | |
| 26 | def encode_whitespaces(text: str, start_extra_id: int=10, max_len: int=10): |
| 27 | """Encode whitespaces to extra tokens. |
| 28 | |
| 29 | >>> encode_whitespaces('a\\n b\\n c', 10, 10) |
| 30 | 'a\\n<|extratoken_10|>b\\n<|extratoken_11|>c' |
| 31 | """ |
| 32 | for i in np.arange(max_len, 1, -1): |
| 33 | text = text.replace(" " * i, f"<|extratoken_{start_extra_id + i - 2}|>") |
| 34 | return text |
| 35 | |
| 36 | |
| 37 | def decode_whitespaces(text: str, start_extra_id: int=10, max_len: int=10): |