A MoonBit port of HuggingFace tokenizers. Loads tokenizer.json and runs BPE / byte-level BPE / WordPiece / Unigram tokenization across all backends.
Dependencies
Status: actively developed. See PROGRESS.md for the full capability matrix and roadmap.中文文档:README.zh.md
moon new my-app && cd my-app
moon add howtomakeaname/tokenizers-moonbitimport {
"howtomakeaname/tokenizers-moonbit/tokenizer",
}fn main {
try {
// Load from a tokenizer.json string (backend-agnostic, no file IO):
// let tok = @tokenizer.Tokenizer::from_str(json_text)
// Or load from a file (uses moonbitlang/x/fs, available on all backends):
let tok = @tokenizer.from_file("tokenizer.json")
// Encode:
let enc = tok.encode("Hello world")
println(enc.ids) // [Int]
println(enc.tokens) // [String]
println(enc.attention_mask) // [Int]
// Decode:
let text = tok.decode(enc.ids, skip_special_tokens=true)
println(text)
} catch {
e => println("failed: \{e.message()}")
}
}async fn main {
try {
let tok = @hub.from_pretrained("bert-base-uncased")
// Use a HuggingFace-compatible mirror when needed:
let tok2 = @hub.from_pretrained(
"bert-base-uncased",
options=@hub.HubDownloadOptions::new(endpoint="https://hf-mirror.com"),
)
println(tok2.get_vocab_size())
} catch {
e => println("failed: \{e.message()}")
}
}| HuggingFace (Python) | MoonBit |
|---|---|
| Tokenizer.from_file("tokenizer.json") | @tokenizer.from_file("tokenizer.json") |
| tok.encode(text) | tok.encode(text) |
| tok.encode(text, add_special_tokens=False) | tok.encode(text, add_special_tokens=false) |
| tok.encode(a, b) | tok.encode_pair(a, b) |
| tok.decode(ids, skip_special_tokens=True) | tok.decode(ids, skip_special_tokens=true) |
| enc.ids / enc.tokens / enc.attention_mask | enc.ids / enc.tokens / enc.attention_mask |
export PATH="$HOME/.moon/bin:$PATH"
moon test # default backend (wasm-gc)
moon test --target native # also: wasm, wasm-gc, js# download model fixtures (classic + modern matrix; gated/renamed models skip)
python3 scripts/fetch_models.py
# generate expected outputs with Python tokenizers
pip install tokenizers
python3 scripts/gen_parity.py
moon test --target nativeInstall
Download zipA MoonBit port of HuggingFace tokenizers. Loads tokenizer.json and runs BPE / byte-level BPE / WordPiece / Unigram tokenization across all backends.
Dependencies