(self, text, context_length=32)
| 118 | return result |
| 119 | |
| 120 | def encode(self, text, context_length=32): |
| 121 | # Clean + lowercase |
| 122 | text = re.sub(r"\s+", " ", text).strip().lower() |
| 123 | |
| 124 | bpe_tokens = [] |
| 125 | for token in re.findall(self.pat, text): |
| 126 | encoded = "".join(self.byte_encoder[b] for b in token.encode("utf-8")) |
| 127 | bpe_tokens.extend( |
| 128 | self.encoder[bpe_token] for bpe_token in self.bpe(encoded).split(" ") |
| 129 | ) |
| 130 | |
| 131 | # Build result: [SOT, tokens..., EOT, 0, 0, ...] |
| 132 | result = [self.sot_token] + bpe_tokens + [self.eot_token] |
| 133 | if len(result) > context_length: |
| 134 | result = result[:context_length] |
| 135 | result[-1] = self.eot_token |
| 136 | result += [0] * (context_length - len(result)) |
| 137 | return result |
| 138 | |
| 139 | |
| 140 | TEST_STRINGS = [ |
no test coverage detected