### Python: ctok_rs.compat.tokenize Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Delegates to upstream ctok if installed; raises ImportError otherwise. Tokenizes a string. ```APIDOC ## tokenize(text, version) ### Description Delegates to upstream `ctok.tokenize` if `ctok` is installed. Raises `ImportError` if not installed. ### Parameters - **text** (str) - Required - The input string to tokenize. - **version** (str) - Required - The model version string. ### Returns - Depends on upstream `ctok`. ### Errors - **ImportError** - If `ctok` is not installed. ``` -------------------------------- ### Install maturin and build local extension Source: https://github.com/steventran0410/ctok-rs/blob/main/CONTRIBUTING.md Install maturin and build the local extension for development. ```bash pip install maturin ``` ```bash maturin develop --release ``` -------------------------------- ### Install ctok-rs via pip Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Install the pre-built Python wheel for ctok-rs. Supports Python >= 3.10 with zero runtime dependencies. ```bash pip install ctok-rs ``` -------------------------------- ### Python quick start with ctok_rs Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Use ctok_rs.token_count, token_counts, normalize, and marked_stream. Non-str arguments raise TypeError; unknown versions raise NotImplementedError. ```python import ctok_rs ctok_rs.token_count("hello, world") # 10 (default "3.0") ctok_rs.token_count("hello, world", "4.7") # 15 ctok_rs.token_count("hello, world", "4.8") # 10 # Batch counting releases the GIL while counting in Rust ctok_rs.token_counts(["", "hello, world"], "4.8") # [6, 10] # Inspection helpers matching ctok.normalize and ctok.marked_stream ctok_rs.normalize("\u2018hello\u2019", "3.0") # "'hello'" ctok_rs.marked_stream("hello, world", "3.0") # "⟨bow⟩hello⟨eow⟩,⟨eow⟩⟨bow⟩world⟨eow⟩" # Non-str arguments raise TypeError; unknown versions raise NotImplementedError ``` -------------------------------- ### Python: ctok_rs.compat.witness Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Delegates to upstream ctok if installed; raises ImportError otherwise. Returns witness information. ```APIDOC ## witness(text, version) ### Description Delegates to upstream `ctok.witness` if `ctok` is installed. Raises `ImportError` if not installed. ### Parameters - **text** (str) - Required - The input string to process. - **version** (str) - Required - The model version string. ### Returns - Depends on upstream `ctok`. ### Errors - **ImportError** - If `ctok` is not installed. ``` -------------------------------- ### Python: ctok_rs.compat.pieces Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Delegates to upstream ctok if installed; raises ImportError otherwise. Returns pieces of a string. ```APIDOC ## pieces(text, version) ### Description Delegates to upstream `ctok.pieces` if `ctok` is installed. Raises `ImportError` if not installed. ### Parameters - **text** (str) - Required - The input string to process. - **version** (str) - Required - The model version string. ### Returns - Depends on upstream `ctok`. ### Errors - **ImportError** - If `ctok` is not installed. ``` -------------------------------- ### Drop-in replacement for ctok counting imports Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Replace 'from ctok import token_count' with 'from ctok_rs.compat import token_count'. The compat module mirrors ctok's signatures for token_count, normalize, and marked_stream, and exposes tokenize, pieces, and witness that delegate to upstream ctok if installed, raising ImportError otherwise. ```python # Before: from ctok import token_count from ctok_rs.compat import token_count token_count("hello, world", "4.8") # 10 ``` -------------------------------- ### Run parity checks with upstream ctok 1.4.0 Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Reproduce the parity checks locally with upstream ctok==1.4.0 installed. The commands run fuzz parity checks on seeds 0-3 with 20,000 texts, all Unicode codepoints, and the pytest suite. ```bash python tools/fuzz_parity.py --seeds 0-3 --n 20000 python tools/fuzz_parity.py --all-codepoints pytest ``` -------------------------------- ### Run Rust unit and golden tests Source: https://github.com/steventran0410/ctok-rs/blob/main/CONTRIBUTING.md Run Rust unit and golden tests. ```bash cargo test ``` -------------------------------- ### Add ctok-rs as git dependency in Cargo.toml Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Optional: add the crate as a git dependency in Cargo.toml, using tag v0.1.1. ```toml ctok-rs = { git = "https://github.com/StevenTran0410/ctok-rs", tag = "v0.1.1" } ``` -------------------------------- ### Python: ctok_rs.compat.normalize Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Drop-in replacement for ctok's normalize function, mirroring its signature. Normalizes a string. ```APIDOC ## normalize(text, version) ### Description Drop-in replacement for `ctok.normalize`. Normalizes the input text using the specified model version. ### Parameters - **text** (str) - Required - The input string to normalize. - **version** (str) - Required - The model version string. ### Returns - **str** - The normalized string. ``` -------------------------------- ### Run full parity test suite with pytest and fuzz_parity.py Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Commands to run the full test suite locally, ensuring exact parity with upstream ctok 1.4.0. The fuzz command uses seeds 0-3 and 20000 iterations. ```bash pytest python tools/fuzz_parity.py --seeds 0-3 --n 20000 ``` -------------------------------- ### Run Python tests Source: https://github.com/steventran0410/ctok-rs/blob/main/CONTRIBUTING.md Run Python tests. Parity tests require Python 3.12 and specific package versions. ```bash pytest ``` -------------------------------- ### Python: ctok_rs.normalize Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Normalizes a string according to the specified model version. Returns the normalized string. ```APIDOC ## normalize(text, version) ### Description Normalizes the input text using the specified model version's normalization rules. ### Parameters - **text** (str) - Required - The input string to normalize. - **version** (str) - Required - The model version string (e.g., "3.0"). ### Returns - **str** - The normalized string. ### Example ```python import ctok_rs ctok_rs.normalize("\u2018hello\u2019", "3.0") # "'hello'" ``` ``` -------------------------------- ### Python: ctok_rs.marked_stream Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Returns a marked stream representation of the input text for the specified model version. Useful for inspection. ```APIDOC ## marked_stream(text, version) ### Description Generates a marked stream representation of the input text, showing token boundaries. ### Parameters - **text** (str) - Required - The input string to process. - **version** (str) - Required - The model version string (e.g., "3.0"). ### Returns - **str** - The marked stream string. ### Example ```python import ctok_rs ctok_rs.marked_stream("hello, world", "3.0") # "⟨bow⟩hello⟨eow⟩,⟨eow⟩⟨bow⟩world⟨eow⟩" ``` ``` -------------------------------- ### Use ctok-rs from Rust Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Use the one-shot token_count helper or hold a TokenCounter directly. The counter is Send + Sync and supports count and count_batch. ```rust use ctok_rs::{token_count, Family, TokenCounter}; fn main() -> Result<(), ctok_rs::Error> { // One-shot helper backed by a process-wide vocabulary cache assert_eq!(token_count("hello, world", "4.7")?, 15); // Or hold a counter directly (Send + Sync) let counter = TokenCounter::new(Family::V4_8)?; assert_eq!(counter.count("hello, world"), 10); assert_eq!(counter.count_batch(&["", "hello, world"]), vec![6, 10]); Ok(()) } ``` -------------------------------- ### Python: ctok_rs.compat.marked_stream Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Drop-in replacement for ctok's marked_stream function, mirroring its signature. Returns a marked stream representation. ```APIDOC ## marked_stream(text, version) ### Description Drop-in replacement for `ctok.marked_stream`. Generates a marked stream representation of the input text. ### Parameters - **text** (str) - Required - The input string to process. - **version** (str) - Required - The model version string. ### Returns - **str** - The marked stream string. ``` -------------------------------- ### Run lint and formatting checks Source: https://github.com/steventran0410/ctok-rs/blob/main/CONTRIBUTING.md Run formatting and linting checks. ```bash cargo fmt --check ``` ```bash cargo clippy --all-targets -- -D warnings ``` ```bash ruff check . ``` -------------------------------- ### Run fuzz parity against upstream Source: https://github.com/steventran0410/ctok-rs/blob/main/CONTRIBUTING.md Run fuzz parity against upstream ctok. ```bash python tools/fuzz_parity.py ``` -------------------------------- ### Python: ctok_rs.compat.token_count Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Drop-in replacement for ctok's token_count function, mirroring its signature. Counts tokens in a string. ```APIDOC ## token_count(text, version) ### Description Drop-in replacement for `ctok.token_count`. Counts tokens in the given text for the specified model version. ### Parameters - **text** (str) - Required - The input string to count tokens for. - **version** (str) - Required - The model version string (e.g., "4.8"). ### Returns - **int** - The token count. ### Example ```python from ctok_rs.compat import token_count token_count("hello, world", "4.8") # 10 ``` ``` -------------------------------- ### Python: ctok_rs.token_counts Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Counts tokens for a batch of strings, releasing the GIL while counting in Rust. Returns a list of token counts. ```APIDOC ## token_counts(texts, version) ### Description Counts tokens for each string in the input list using the specified model version. The GIL is released during counting. ### Parameters - **texts** (list of str) - Required - A list of strings to count tokens for. - **version** (str) - Required - The model version string (e.g., "4.8"). ### Returns - **list of int** - A list of token counts corresponding to each input string. ### Example ```python import ctok_rs ctok_rs.token_counts(["", "hello, world"], "4.8") # [6, 10] ``` ``` -------------------------------- ### Rust: ctok_rs::TokenCounter::new Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Creates a new TokenCounter for the specified model family. Returns a Result with the counter. ```APIDOC ## TokenCounter::new(family) ### Description Constructs a new `TokenCounter` for the given model family. The counter is `Send + Sync`. ### Parameters - **family** (Family) - Required - The model family (e.g., `Family::V4_8`). ### Returns - **Result** - The counter on success, or an error. ### Example ```rust use ctok_rs::{TokenCounter, Family}; let counter = TokenCounter::new(Family::V4_8)?; ``` ``` -------------------------------- ### Generate unicode tables Source: https://github.com/steventran0410/ctok-rs/blob/main/CONTRIBUTING.md Generate data/unicode_tables.bin using Python 3.12 with specific package versions. ```bash python tools/gen_unicode_tables.py ``` -------------------------------- ### Python: ctok_rs.token_count Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Counts tokens in a string using the specified model version. Returns an integer token count. ```APIDOC ## token_count(text, version) ### Description Counts the number of tokens in the given text for the specified model version. ### Parameters - **text** (str) - Required - The input string to count tokens for. - **version** (str) - Optional - The model version string (e.g., "3.0", "4.7", "4.8"). Defaults to "3.0". ### Returns - **int** - The token count. ### Example ```python import ctok_rs ctok_rs.token_count("hello, world") # 10 ctok_rs.token_count("hello, world", "4.7") # 15 ctok_rs.token_count("hello, world", "4.8") # 10 ``` ``` -------------------------------- ### Rust: ctok_rs::TokenCounter::count_batch Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Counts tokens for a batch of strings using the counter. Returns a vector of token counts. ```APIDOC ## TokenCounter::count_batch(texts) ### Description Counts tokens for each string in the input slice using this counter. ### Parameters - **texts** (&[&str]) - Required - A slice of strings to count tokens for. ### Returns - **Vec** - A vector of token counts corresponding to each input string. ### Example ```rust assert_eq!(counter.count_batch(&["", "hello, world"]), vec![6, 10]); ``` ``` -------------------------------- ### Rust: ctok_rs::token_count Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md One-shot helper function that counts tokens in a string using a process-wide vocabulary cache. Returns a Result with the token count. ```APIDOC ## token_count(text, version) ### Description Counts tokens in the given text for the specified model version. Uses a process-wide vocabulary cache. ### Parameters - **text** (&str) - Required - The input string to count tokens for. - **version** (&str) - Required - The model version string (e.g., "4.7"). ### Returns - **Result** - The token count on success, or an error. ### Example ```rust use ctok_rs::token_count; assert_eq!(token_count("hello, world", "4.7")?, 15); ``` ``` -------------------------------- ### Rust: ctok_rs::TokenCounter::count Source: https://github.com/steventran0410/ctok-rs/blob/main/README.md Counts tokens in a single string using the counter. Returns the token count. ```APIDOC ## TokenCounter::count(text) ### Description Counts tokens in the given text using this counter. ### Parameters - **text** (&str) - Required - The input string to count tokens for. ### Returns - **usize** - The token count. ### Example ```rust assert_eq!(counter.count("hello, world"), 10); ``` ```