module documentation
Context-aware text encoder
| Function | encode |
Encodes a string of token represented as text into a byte stream |
| Function | normalize |
Applies NFC normalization to a given string to ensure recognition of certain Unicode characters used as token names |
| Function | tokenize |
Tokenizes a string of tokens represented as text into a list of TIToken objects |
| Function | unparse |
Concatenates a TIToken sequence into a bytestream |
def encode(string:
str, *, model: TIModel = TI_84PCE, lang: str = None, mode: str = None) -> bytes:
(source)
¶
Encodes a string of token represented as text into a byte stream
For detailed information on tokenization modes, see tivars.tokenizer.tokenize.
| Parameters | |
string:str | The text string to encode |
model:TIModel | A model to target when encoding (defaults to no specific model) |
lang:str | The language used in string (defaults to the locale of model, or English, en) |
mode:str | The tokenization mode to use (defaults to smart) |
| Returns | |
bytes | The bytes comprising the tokens represented by string |
Applies NFC normalization to a given string to ensure recognition of certain Unicode characters used as token names
| Parameters | |
string:str | The text to normalize |
| Returns | |
| The text in string normalized |
def tokenize(string:
str, *, trie: TITokenTrie = None, mode: str = None, normalize: bool = True) -> list[ TIToken]:
(source)
¶
Tokenizes a string of tokens represented as text into a list of TIToken objects
- Tokenization is performed using one of three procedures, dictated by mode:
- max: Always munch maximally, i.e. consume the most input possible to produce a token
- min: Always munch minimally, i.e. consume the least input possible, which is often single characters
- smart: Munch maximally or minimally depending on context
- The smart tokenization mode uses the following contexts, munching maximally otherwise:
- Strings: munch minimally, except when interpolating using Send( or storing to an equation
- Program names: munch minimally up to 8 tokens
- List names: munch minimally up to 5 tokens
- In all modes:
- Standard glyphs can be used for substituting Unicode symbols
xXXanduUUUUoutput the denoted bytes exactly, regardless of validityABCDmaximally munchesABCD, regardless of surrounding context- Certain unprintable characters act as a hard separator for tokens
- U+001F (unit separator):
␟ - U+200A (hair space): ` `
- U+200C (zero width non-joiner):
- U+001F (unit separator):
- For reference, here are the tokenization modes utilized by popular IDEs and other software:
- SourceCoder: max
- TokenIDE: max
- TI Connect CE: ¯_(ツ)_/¯
- TI-Planet Project Builder: smart
- tivars_lib_cpp: smart
| Parameters | |
string:str | The text string to encode |
trie:TITokenTrie | The TokenTrie object to use for tokenization (defaults to the TI-84+CE trie) |
mode:str | The tokenization mode to use (defaults to smart) |
normalize:bool | Whether to apply NFC normalization to the input before encoding (defaults to true) |
| Returns | |
list[ | A list of TIToken objects represented by string |