Skip to main content

mokuji (Python)

Read Mokuji files a range at a time, from a path, a URL or any source of bytes. Pure standard library: zlib for the required raw deflate, urllib for range requests, nothing to install beside it.

It is the fourth implementation of SPEC.md, after the Rust crates, the Python fixture builder and the TypeScript reader, whose API it follows with Python names. It answers to the same conformance cases, read counts included.

Installed from git rather than PyPI while the format is a draft (below).

Use

from mokuji import MokujiIndex, MokujiCollection

# One file. A URL makes the reader fetch ranges itself.
index = MokujiIndex.open("https://example.com/places.mokuji")
done = index.complete("東京タ", 10)  # .docs, .payloads, .from_table

# A collection: a manifest over several files (MANIFEST.md), restricted to a
# place so that only the files there are opened.
collection = MokujiCollection.open("https://example.com/world.json", countries=["JP"])
found = collection.complete("tokyo", 10)

The query goes through the tokenizer the file records (TOKENIZERS.md) before it is looked up, so 一丁目 finds 1丁目 in a ja-address index.

search ranks by BM25, the same ranking as mokuji search with the same options, and splits the query the way the file's tokenizer does — into bigrams, for a ja-address-bigram index:

fuzzy = MokujiIndex.open("https://example.com/abr-fuzzy.mokuji")
hits = fuzzy.search("和光市", boost="municipality")
# hits.results[0].payload == "埼玉県和光市|139.605693,35.781104"

# Typed wrong: any of its bigrams, the six rarest, without length normalization.
fuzzy.search("千代田区丸乃内", any=True, probe=6, length_norm=0)

The scores agree with mokuji search's and the TypeScript reader's to about 1e-12, not to the last bit. JavaScript's Math.log is not correctly rounded, and Python's is, so two documents whose scores differ by less than that may come out in either order between the two.

Every result is accompanied by index.stats — the range requests and bytes it cost since opening or reset_stats() — and index.attribution / index.license, the credit the file states.

Reading a file on hand

Give open a path, and it reads with positional reads: a file of gigabytes is searched without being loaded, and costs what a query touches.

with MokujiIndex.open("postal.mokuji") as postal:
    postal.lookup("1000005", 10)

Reading from something else

Give open any object with read(offset, length) returning the bytes — an object store client, a cache in front of one. A size attribute, if it has one, lets the reader check offsets against the file's length.

class Bucket:
    def read(self, offset, length):
        return s3.get_object(Bucket="b", Key="places.mokuji",
                             Range=f"bytes={offset}-{offset + length - 1}")["Body"].read()

index = MokujiIndex.open(Bucket())

Write

Builder is the Rust crate's mokuji::write. Documents are added first, in ID order; postings then, terms in byte order and each term's documents ascending. The writer checks the order and owns no sort. split turns a document's text into terms as a tokenizer from TOKENIZERS.md does.

from mokuji import Builder, Metadata, Provenance, WriteOptions, split

options = WriteOptions()
builder = Builder(options)
doc = builder.add_document("35.671,139.772")
# ... every document, then every posting, in order
builder.add_posting("chuo", doc)

meta = Metadata(
    sources=[Provenance("sha2-256:…", "places.tsv", None, None)],
    generator_params=options.params(),
    tokenizer_id="space-split",
)
data = builder.finish(meta.encode())

The metadata is the caller's to compose: it names the source the index was derived from, which the writer never sees (SPEC.md §6).

Stored raw (compress=False), a file is the Rust writer's byte for byte, and the tests rebuild the conformance fixtures to show it. Compressed with zlib, it is not, since no specification of deflate fixes which of the valid encodings a compressor emits; it says the same thing, every reader reads it, and this writer reproduces it, which is what SPEC.md §3.4 asks. Its generator.name is mokuji-python, so that a rebuild is checked against the writer that made it.

Install

pip install mokuji

The format and this API may change before 1.0, so pin the version.

Develop

From the repository root:

python3 -m unittest discover -s python/tests

The tests run the conformance cases from conformance/, and, when node and a built js/dist are present, ask the TypeScript reader the same questions and compare the answers.

Where it differs from the TypeScript reader

  • A term that sorts before the first block is absent without a read (SPEC.md §5.3.2), as it is in the Rust reader; so is a prefix or a range that nothing in the dictionary can begin with or fall in.
  • It checks what SPEC.md says a reader must: varints of at most ten bytes, a strictly increasing term index, offsets inside their sections, a postings run's count against its entry's doc_freq, and HAS_POSITIONS only with HAS_FREQS.
  • The metadata decoder reads every CBOR type, so an unknown key holding a boolean, a negative number or bytes does not stop a file opening.
  • ja-address strips White_Space, as the Rust tokenizer does; JavaScript's trim differs on U+0085 and U+FEFF.
  • Requests are made one after another, not at once.

Metadata

Release files for mokuji 0.1.1

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

Source distribution (sdist)

Source distribution for mokuji 0.1.1
File Size Uploaded Provenance Actions
mokuji-0.1.1.tar.gz 50.1 kB With provenance Details for mokuji-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mokuji 0.1.1
File Interpreter ABI Platform Actions
mokuji-0.1.1-py3-none-any.whl Python 3 none any Details for mokuji-0.1.1-py3-none-any.whl

Total release size: 85.9 kB

Release files / mokuji-0.1.1.tar.gz

Download URL mokuji-0.1.1.tar.gz
Size 50.1 kB
Tags Source
SHA-256 checksum
How to use checksums
941a62a40687305014e64c2d22d900485241a5601c716b38209985d55fc443e7
BLAKE2b-256 checksum
How to use checksums
8803d4188b491a146c31fd951e19a6033b64418dcae774b8e3242b4ef51e40ff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Release files / mokuji-0.1.1-py3-none-any.whl

Download URL mokuji-0.1.1-py3-none-any.whl
Size 35.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a01504b591b9424afc8425a4694face55291803cb7ec6e0c6b3772e69c47f562
BLAKE2b-256 checksum
How to use checksums
6d97096f30b267dc0c9753831b85bfc37e74371d8ba29abd178e336b34a884f3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

Security for mokuji 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mokuji-0.1.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 24, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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