Skip to main content

morpheme (Python)

Python bindings for the morpheme tokenizer library: fast, Hugging Face-compatible tokenizer.json loading, encoding and decoding, built with PyO3 and maturin. The API is deliberately small: load a tokenizer, encode, decode, count tokens, and look up the vocabulary. It does not reproduce the Hugging Face tokenizers Python API, and it does not train tokenizers (use the Rust library or the morpheme CLI).

Install

pip install morpheme

Prebuilt abi3 wheels (one wheel per platform for CPython 3.9 and later) cover Linux x86_64 and aarch64 (manylinux), macOS on Apple silicon, and Windows x64. Other platforms, including Intel macOS, install from the sdist and need a Rust toolchain.

Usage

import morpheme

tok = morpheme.Tokenizer.from_pretrained("google-bert/bert-base-uncased")
# or: morpheme.Tokenizer.from_file("tokenizer.json")
#     morpheme.Tokenizer.from_str(json_text)

enc = tok.encode("Hello, world!")
print(enc.ids)                # [101, 7592, 1010, 2088, 999, 102]
print(enc.tokens)             # ['[CLS]', 'hello', ',', 'world', '!', '[SEP]']
print(enc.offsets)            # character spans: [(0, 0), (0, 5), (5, 6), ...]
print(tok.count("Hello, world!"))  # 6, the same as len(enc.ids)
print(tok.decode(enc.ids))    # hello, world!

batch = tok.encode_batch(["a", "b"])  # releases the GIL while encoding
try:
    morpheme.Tokenizer.from_file("missing.json")
except morpheme.MorphemeError as e:
    print(e)
Tokenizer
from_file(path), from_str(json), from_pretrained(repo_id, revision=None) Load a tokenizer.json (the Hub download uses the cache shared with Python huggingface_hub; HF_TOKEN and HF_HUB_OFFLINE apply)
encode(text, add_special_tokens=True), encode_batch(texts, add_special_tokens=True) Encoding objects with ids, tokens, offsets (character spans), type_ids, attention_mask, special_tokens_mask
decode(ids, skip_special_tokens=True), decode_batch(sequences, skip_special_tokens=True) Text
count(text, add_special_tokens=True) Number of ids encode returns (the file's padding/truncation settings apply)
token_to_id(token), id_to_token(id), vocab_size(with_added_tokens=True) Vocabulary lookups
to_str(pretty=False), save(path, pretty=False) Write tokenizer.json

Errors raise morpheme.MorphemeError; morpheme.__version__ is the package version.

Building locally

From a checkout, with the Hugging Face fixtures the tests use (./scripts/fetch-hf-fixtures.sh at the repository root):

python -m venv .venv
.venv/bin/pip install maturin pytest
(cd bindings/python && ../../.venv/bin/maturin develop --locked)
.venv/bin/pytest bindings/python/tests

This is what CI runs, from the repository root. maturin develop installs the extension into the active virtualenv, or into a .venv in the current directory or one of its parents.

The Rust crate (morpheme-python) is publish = false (it is not on crates.io); release wheels are built by the Python wheels workflow with maturin build --release and uploaded to PyPI when a version tag is pushed.

Metadata

Release files for morpheme 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for morpheme 0.5.0
File Size Uploaded
morpheme-0.5.0.tar.gz 183.1 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for morpheme 0.5.0
File
morpheme-0.5.0-cp39-abi3-win_amd64.whl CPython 3.9 abi3 Windows x86-64 Details
morpheme-0.5.0-cp39-abi3-manylinux_2_28_aarch64.whl CPython 3.9 abi3 Linux glibc 2.28+ ARM64 Details
morpheme-0.5.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.9 abi3 Linux glibc 2.17+ x86-64 Details
morpheme-0.5.0-cp39-abi3-macosx_11_0_arm64.whl CPython 3.9 abi3 macOS 11.0+ ARM64 Details

Total release size: 11.2 MB

Release files / morpheme-0.5.0.tar.gz

Download URL morpheme-0.5.0.tar.gz
Size 183.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3163fd77a867d6bb13563ad14458a994b79745d0c0fbe99bf03c20e0a7d69e4f
BLAKE2b-256 checksum
How to use checksums
c6d8580c2ba67bce92627d380f12ac32eb08ea1e60afd3e124bc070b2887955d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / morpheme-0.5.0-cp39-abi3-win_amd64.whl

Download URL morpheme-0.5.0-cp39-abi3-win_amd64.whl
Size 2.6 MB
Tags CPython 3.9 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
1b9b5f353d27e3ea454b696de893af078167d730c95b4a0be37392433e410374
BLAKE2b-256 checksum
How to use checksums
d6b58008ef012bdf0362e760f50c17d7ae127ab60bf99d698a93bf286e18ea91
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / morpheme-0.5.0-cp39-abi3-manylinux_2_28_aarch64.whl

Download URL morpheme-0.5.0-cp39-abi3-manylinux_2_28_aarch64.whl
Size 2.9 MB
Tags CPython 3.9 Linux glibc 2.28+ ARM64 abi3
SHA-256 checksum
How to use checksums
0de41dbb340f880090a8f6548c0fd5ba10b041d4a9326313b27cb3a04460039a
BLAKE2b-256 checksum
How to use checksums
6f140b91a888ee4829f80fe6345ef612ce5bddda4b74c198b683c6ada84052aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / morpheme-0.5.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL morpheme-0.5.0-cp39-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.9 MB
Tags CPython 3.9 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
d49e410eb91195bdd21c26981a687078ab0202b318ba75b8af012d3db58d7020
BLAKE2b-256 checksum
How to use checksums
a76f6666e1311d1d26ed6b8099cbff5111ec972a4651db1b9819e02122c16985
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / morpheme-0.5.0-cp39-abi3-macosx_11_0_arm64.whl

Download URL morpheme-0.5.0-cp39-abi3-macosx_11_0_arm64.whl
Size 2.6 MB
Tags CPython 3.9 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
05598acda887e86282914ee376c44897cec17d334dfde27f34690fc75072863b
BLAKE2b-256 checksum
How to use checksums
b261daae9fb4a41cd3a85261df5d77c33a2ad7a5760db3716667b4e16371aa9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release history Release notifications | RSS feed

This release

0.5.0 This release

5 release files

0.4.0

5 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page