mokuji 0.1.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mokuji-0.1.0/PKG-INFO ADDED
@@ -0,0 +1,157 @@
1
+ Metadata-Version: 2.4
2
+ Name: mokuji
3
+ Version: 0.1.0
4
+ Summary: Read Mokuji files — a cloud-optimized search index — a range at a time, from a path or over HTTP
5
+ Author: Re:Earth and contributors
6
+ License-Expression: MIT OR Apache-2.0
7
+ Project-URL: Repository, https://github.com/reearth/mokuji
8
+ Requires-Python: >=3.11
9
+ Description-Content-Type: text/markdown
10
+
11
+ # mokuji (Python)
12
+
13
+ Read [Mokuji](../README.md) files a range at a time, from a path, a URL or any
14
+ source of bytes. Pure standard library: `zlib` for the required raw deflate,
15
+ `urllib` for range requests, nothing to install beside it.
16
+
17
+ It is the fourth implementation of [SPEC.md](../SPEC.md), after the Rust
18
+ crates, the Python fixture builder and the [TypeScript reader](../js), whose
19
+ API it follows with Python names. It answers to the same conformance cases,
20
+ read counts included.
21
+
22
+ > Installed from git rather than PyPI while the format is a draft (below).
23
+
24
+ ## Use
25
+
26
+ ```python
27
+ from mokuji import MokujiIndex, MokujiCollection
28
+
29
+ # One file. A URL makes the reader fetch ranges itself.
30
+ index = MokujiIndex.open("https://example.com/places.mokuji")
31
+ done = index.complete("東京タ", 10) # .docs, .payloads, .from_table
32
+
33
+ # A collection: a manifest over several files (MANIFEST.md), restricted to a
34
+ # place so that only the files there are opened.
35
+ collection = MokujiCollection.open("https://example.com/world.json", countries=["JP"])
36
+ found = collection.complete("tokyo", 10)
37
+ ```
38
+
39
+ The query goes through the tokenizer the file records (`TOKENIZERS.md`)
40
+ before it is looked up, so `一丁目` finds `1丁目` in a `ja-address` index.
41
+
42
+ `search` ranks by BM25, the same ranking as `mokuji search` with the same
43
+ options, and splits the query the way the file's tokenizer does — into bigrams,
44
+ for a `ja-address-bigram` index:
45
+
46
+ ```python
47
+ fuzzy = MokujiIndex.open("https://example.com/abr-fuzzy.mokuji")
48
+ hits = fuzzy.search("和光市", boost="municipality")
49
+ # hits.results[0].payload == "埼玉県和光市|139.605693,35.781104"
50
+
51
+ # Typed wrong: any of its bigrams, the six rarest, without length normalization.
52
+ fuzzy.search("千代田区丸乃内", any=True, probe=6, length_norm=0)
53
+ ```
54
+
55
+ The scores agree with `mokuji search`'s and the TypeScript reader's to about
56
+ 1e-12, not to the last bit. JavaScript's `Math.log` is not correctly rounded,
57
+ and Python's is, so two documents whose scores differ by less than that may
58
+ come out in either order between the two.
59
+
60
+ Every result is accompanied by `index.stats` — the range requests and bytes it
61
+ cost since opening or `reset_stats()` — and `index.attribution` /
62
+ `index.license`, the credit the file states.
63
+
64
+ ### Reading a file on hand
65
+
66
+ Give `open` a path, and it reads with positional reads: a file of gigabytes is
67
+ searched without being loaded, and costs what a query touches.
68
+
69
+ ```python
70
+ with MokujiIndex.open("postal.mokuji") as postal:
71
+ postal.lookup("1000005", 10)
72
+ ```
73
+
74
+ ### Reading from something else
75
+
76
+ Give `open` any object with `read(offset, length)` returning the bytes — an
77
+ object store client, a cache in front of one. A `size` attribute, if it has
78
+ one, lets the reader check offsets against the file's length.
79
+
80
+ ```python
81
+ class Bucket:
82
+ def read(self, offset, length):
83
+ return s3.get_object(Bucket="b", Key="places.mokuji",
84
+ Range=f"bytes={offset}-{offset + length - 1}")["Body"].read()
85
+
86
+ index = MokujiIndex.open(Bucket())
87
+ ```
88
+
89
+ ## Write
90
+
91
+ `Builder` is the Rust crate's `mokuji::write`. Documents are added first, in
92
+ ID order; postings then, terms in byte order and each term's documents
93
+ ascending. The writer checks the order and owns no sort. `split` turns a
94
+ document's text into terms as a tokenizer from `TOKENIZERS.md` does.
95
+
96
+ ```python
97
+ from mokuji import Builder, Metadata, Provenance, WriteOptions, split
98
+
99
+ options = WriteOptions()
100
+ builder = Builder(options)
101
+ doc = builder.add_document("35.671,139.772")
102
+ # ... every document, then every posting, in order
103
+ builder.add_posting("chuo", doc)
104
+
105
+ meta = Metadata(
106
+ sources=[Provenance("sha2-256:…", "places.tsv", None, None)],
107
+ generator_params=options.params(),
108
+ tokenizer_id="space-split",
109
+ )
110
+ data = builder.finish(meta.encode())
111
+ ```
112
+
113
+ The metadata is the caller's to compose: it names the source the index was
114
+ derived from, which the writer never sees (SPEC.md §6).
115
+
116
+ Stored raw (`compress=False`), a file is the Rust writer's byte for byte, and
117
+ the tests rebuild the conformance fixtures to show it. Compressed with zlib,
118
+ it is not, since no specification of deflate fixes which of the valid
119
+ encodings a compressor emits; it says the same thing, every reader reads it,
120
+ and this writer reproduces it, which is what SPEC.md §3.4 asks. Its
121
+ `generator.name` is `mokuji-python`, so that a rebuild is checked against the
122
+ writer that made it.
123
+
124
+ ## Install
125
+
126
+ ```sh
127
+ pip install mokuji
128
+ ```
129
+
130
+ The format and this API may change before 1.0, so pin the version.
131
+
132
+ ## Develop
133
+
134
+ From the repository root:
135
+
136
+ ```sh
137
+ python3 -m unittest discover -s python/tests
138
+ ```
139
+
140
+ The tests run the conformance cases from `conformance/`, and, when `node` and
141
+ a built `js/dist` are present, ask the TypeScript reader the same questions
142
+ and compare the answers.
143
+
144
+ ## Where it differs from the TypeScript reader
145
+
146
+ - A term that sorts before the first block is absent without a read
147
+ (SPEC.md §5.3.2), as it is in the Rust reader; so is a prefix or a range
148
+ that nothing in the dictionary can begin with or fall in.
149
+ - It checks what SPEC.md says a reader must: varints of at most ten bytes, a
150
+ strictly increasing term index, offsets inside their sections, a postings
151
+ run's count against its entry's `doc_freq`, and `HAS_POSITIONS` only with
152
+ `HAS_FREQS`.
153
+ - The metadata decoder reads every CBOR type, so an unknown key holding a
154
+ boolean, a negative number or bytes does not stop a file opening.
155
+ - `ja-address` strips `White_Space`, as the Rust tokenizer does; JavaScript's
156
+ `trim` differs on U+0085 and U+FEFF.
157
+ - Requests are made one after another, not at once.
mokuji-0.1.0/README.md ADDED
@@ -0,0 +1,147 @@
1
+ # mokuji (Python)
2
+
3
+ Read [Mokuji](../README.md) files a range at a time, from a path, a URL or any
4
+ source of bytes. Pure standard library: `zlib` for the required raw deflate,
5
+ `urllib` for range requests, nothing to install beside it.
6
+
7
+ It is the fourth implementation of [SPEC.md](../SPEC.md), after the Rust
8
+ crates, the Python fixture builder and the [TypeScript reader](../js), whose
9
+ API it follows with Python names. It answers to the same conformance cases,
10
+ read counts included.
11
+
12
+ > Installed from git rather than PyPI while the format is a draft (below).
13
+
14
+ ## Use
15
+
16
+ ```python
17
+ from mokuji import MokujiIndex, MokujiCollection
18
+
19
+ # One file. A URL makes the reader fetch ranges itself.
20
+ index = MokujiIndex.open("https://example.com/places.mokuji")
21
+ done = index.complete("東京タ", 10) # .docs, .payloads, .from_table
22
+
23
+ # A collection: a manifest over several files (MANIFEST.md), restricted to a
24
+ # place so that only the files there are opened.
25
+ collection = MokujiCollection.open("https://example.com/world.json", countries=["JP"])
26
+ found = collection.complete("tokyo", 10)
27
+ ```
28
+
29
+ The query goes through the tokenizer the file records (`TOKENIZERS.md`)
30
+ before it is looked up, so `一丁目` finds `1丁目` in a `ja-address` index.
31
+
32
+ `search` ranks by BM25, the same ranking as `mokuji search` with the same
33
+ options, and splits the query the way the file's tokenizer does — into bigrams,
34
+ for a `ja-address-bigram` index:
35
+
36
+ ```python
37
+ fuzzy = MokujiIndex.open("https://example.com/abr-fuzzy.mokuji")
38
+ hits = fuzzy.search("和光市", boost="municipality")
39
+ # hits.results[0].payload == "埼玉県和光市|139.605693,35.781104"
40
+
41
+ # Typed wrong: any of its bigrams, the six rarest, without length normalization.
42
+ fuzzy.search("千代田区丸乃内", any=True, probe=6, length_norm=0)
43
+ ```
44
+
45
+ The scores agree with `mokuji search`'s and the TypeScript reader's to about
46
+ 1e-12, not to the last bit. JavaScript's `Math.log` is not correctly rounded,
47
+ and Python's is, so two documents whose scores differ by less than that may
48
+ come out in either order between the two.
49
+
50
+ Every result is accompanied by `index.stats` — the range requests and bytes it
51
+ cost since opening or `reset_stats()` — and `index.attribution` /
52
+ `index.license`, the credit the file states.
53
+
54
+ ### Reading a file on hand
55
+
56
+ Give `open` a path, and it reads with positional reads: a file of gigabytes is
57
+ searched without being loaded, and costs what a query touches.
58
+
59
+ ```python
60
+ with MokujiIndex.open("postal.mokuji") as postal:
61
+ postal.lookup("1000005", 10)
62
+ ```
63
+
64
+ ### Reading from something else
65
+
66
+ Give `open` any object with `read(offset, length)` returning the bytes — an
67
+ object store client, a cache in front of one. A `size` attribute, if it has
68
+ one, lets the reader check offsets against the file's length.
69
+
70
+ ```python
71
+ class Bucket:
72
+ def read(self, offset, length):
73
+ return s3.get_object(Bucket="b", Key="places.mokuji",
74
+ Range=f"bytes={offset}-{offset + length - 1}")["Body"].read()
75
+
76
+ index = MokujiIndex.open(Bucket())
77
+ ```
78
+
79
+ ## Write
80
+
81
+ `Builder` is the Rust crate's `mokuji::write`. Documents are added first, in
82
+ ID order; postings then, terms in byte order and each term's documents
83
+ ascending. The writer checks the order and owns no sort. `split` turns a
84
+ document's text into terms as a tokenizer from `TOKENIZERS.md` does.
85
+
86
+ ```python
87
+ from mokuji import Builder, Metadata, Provenance, WriteOptions, split
88
+
89
+ options = WriteOptions()
90
+ builder = Builder(options)
91
+ doc = builder.add_document("35.671,139.772")
92
+ # ... every document, then every posting, in order
93
+ builder.add_posting("chuo", doc)
94
+
95
+ meta = Metadata(
96
+ sources=[Provenance("sha2-256:…", "places.tsv", None, None)],
97
+ generator_params=options.params(),
98
+ tokenizer_id="space-split",
99
+ )
100
+ data = builder.finish(meta.encode())
101
+ ```
102
+
103
+ The metadata is the caller's to compose: it names the source the index was
104
+ derived from, which the writer never sees (SPEC.md §6).
105
+
106
+ Stored raw (`compress=False`), a file is the Rust writer's byte for byte, and
107
+ the tests rebuild the conformance fixtures to show it. Compressed with zlib,
108
+ it is not, since no specification of deflate fixes which of the valid
109
+ encodings a compressor emits; it says the same thing, every reader reads it,
110
+ and this writer reproduces it, which is what SPEC.md §3.4 asks. Its
111
+ `generator.name` is `mokuji-python`, so that a rebuild is checked against the
112
+ writer that made it.
113
+
114
+ ## Install
115
+
116
+ ```sh
117
+ pip install mokuji
118
+ ```
119
+
120
+ The format and this API may change before 1.0, so pin the version.
121
+
122
+ ## Develop
123
+
124
+ From the repository root:
125
+
126
+ ```sh
127
+ python3 -m unittest discover -s python/tests
128
+ ```
129
+
130
+ The tests run the conformance cases from `conformance/`, and, when `node` and
131
+ a built `js/dist` are present, ask the TypeScript reader the same questions
132
+ and compare the answers.
133
+
134
+ ## Where it differs from the TypeScript reader
135
+
136
+ - A term that sorts before the first block is absent without a read
137
+ (SPEC.md §5.3.2), as it is in the Rust reader; so is a prefix or a range
138
+ that nothing in the dictionary can begin with or fall in.
139
+ - It checks what SPEC.md says a reader must: varints of at most ten bytes, a
140
+ strictly increasing term index, offsets inside their sections, a postings
141
+ run's count against its entry's `doc_freq`, and `HAS_POSITIONS` only with
142
+ `HAS_FREQS`.
143
+ - The metadata decoder reads every CBOR type, so an unknown key holding a
144
+ boolean, a negative number or bytes does not stop a file opening.
145
+ - `ja-address` strips `White_Space`, as the Rust tokenizer does; JavaScript's
146
+ `trim` differs on U+0085 and U+FEFF.
147
+ - Requests are made one after another, not at once.
@@ -0,0 +1,81 @@
1
+ # mokuji — read Mokuji files a range at a time, from a path, a URL or any
2
+ # source of bytes, and write them, with nothing but the standard library. See
3
+ # SPEC.md and MANIFEST.md.
4
+
5
+ from .reader import (
6
+ CollectionCompletion,
7
+ CollectionStats,
8
+ Column,
9
+ Completion,
10
+ Completions,
11
+ Consulted,
12
+ FileSource,
13
+ Hit,
14
+ HttpSource,
15
+ Lookup,
16
+ LookupResult,
17
+ MokujiCollection,
18
+ MokujiError,
19
+ MokujiIndex,
20
+ Postings,
21
+ Probed,
22
+ Provenance,
23
+ Ranked,
24
+ RequestInfo,
25
+ SearchResult,
26
+ Stats,
27
+ TermEntry,
28
+ )
29
+ from .tokenizers import SPLITTERS, TOKENIZERS, bigram, ja_address, name_list
30
+ from .writer import (
31
+ GENERATOR_NAME,
32
+ SECTION_APPLICATION_MIN,
33
+ VERSION,
34
+ Builder,
35
+ BuildError,
36
+ CompletionOptions,
37
+ Metadata,
38
+ WriteOptions,
39
+ encode_cbor,
40
+ split,
41
+ )
42
+
43
+ __all__ = [
44
+ "CollectionCompletion",
45
+ "CollectionStats",
46
+ "Column",
47
+ "Completion",
48
+ "Completions",
49
+ "Consulted",
50
+ "FileSource",
51
+ "Hit",
52
+ "HttpSource",
53
+ "Lookup",
54
+ "LookupResult",
55
+ "MokujiCollection",
56
+ "MokujiError",
57
+ "MokujiIndex",
58
+ "Postings",
59
+ "Probed",
60
+ "Provenance",
61
+ "Ranked",
62
+ "RequestInfo",
63
+ "SearchResult",
64
+ "Stats",
65
+ "TermEntry",
66
+ "SPLITTERS",
67
+ "TOKENIZERS",
68
+ "bigram",
69
+ "ja_address",
70
+ "name_list",
71
+ "Builder",
72
+ "BuildError",
73
+ "CompletionOptions",
74
+ "GENERATOR_NAME",
75
+ "Metadata",
76
+ "SECTION_APPLICATION_MIN",
77
+ "VERSION",
78
+ "WriteOptions",
79
+ "encode_cbor",
80
+ "split",
81
+ ]