minhashlib 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.
@@ -0,0 +1,11 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.nbi
4
+ *.nbc
5
+
6
+ .cache/
7
+ .vscode/
8
+
9
+ data/
10
+ benchmark_outputs/
11
+ benchmark_outputs_fast/
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Sachin Avutu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,114 @@
1
+ Metadata-Version: 2.4
2
+ Name: minhashlib
3
+ Version: 0.1.0
4
+ Summary: A fast and minimal minhashing based similarity checking library.
5
+ Project-URL: Repository, https://github.com/ssavutu/minhashlib
6
+ Project-URL: Bug Tracker, https://github.com/ssavutu/minhashlib/issues
7
+ Author-email: Sachin Avutu <ssavutu@gmail.com>
8
+ Maintainer-email: Sachin Avutu <ssavutu@gmail.com>
9
+ License-Expression: MIT
10
+ License-File: LICENSE
11
+ Keywords: deduplication,difference,entity resolution,minhash,minhashing,similarity
12
+ Classifier: Development Status :: 3 - Alpha
13
+ Classifier: Programming Language :: Python
14
+ Requires-Python: >=3.10
15
+ Requires-Dist: numba
16
+ Requires-Dist: numpy
17
+ Requires-Dist: xxhash
18
+ Description-Content-Type: text/markdown
19
+
20
+ # Minhashlib
21
+
22
+ This is a minimal implementation of MinHashing as described in Jeffrey Ullman's book *Mining Massive Datasets*.
23
+
24
+ ## Current Benchmark Claim
25
+
26
+ Based on the benchmark outputs in this repository:
27
+
28
+ - On recent multi-seed CPU synthetic runs (seeds `42-46`), `minhashlib` builds signatures about `~6x` faster than `datasketch`.
29
+ - Accuracy is comparable in magnitude (similar MAE scale and matching threshold-based metrics in these runs), though `datasketch` is slightly better on MAE in most synthetic scenarios.
30
+ - Memory results are workload-dependent, so this project does not claim universal memory superiority.
31
+
32
+ In short: this implementation is minimal and fast, with accuracy that is broadly comparable to `datasketch`, but outcomes vary depending on dataset and configuration.
33
+
34
+ ## Benchmark Suite
35
+
36
+ Use `benchmarks/benchmark_claims_suite.py` to run comprehensive, reproducible benchmarks:
37
+
38
+ - Multiple datasets (`synthetic`, `20newsgroups`, `wikipedia`, `ag_news`, `local`)
39
+ - Multiple seeds with mean/std/95% CI
40
+ - Metrics: MAE(Mean Average Error), Precision/Recall/F1 at threshold, Precision@K/Recall@K
41
+ - Speed: build/pair-eval/retrieval latency and throughput
42
+ - Memory: peak allocation and bytes/signature
43
+ - Optional scaling sweeps over docs/number of hashes/doc length
44
+
45
+ Example (full):
46
+
47
+ ```bash
48
+ python3 benchmarks/benchmark_claims_suite.py
49
+ --datasets synthetic,20newsgroups,wikipedia
50
+ --wiki-dump-path data/simplewiki-latest-pages-articles.xml.bz2
51
+ --seeds 42,43,44
52
+ --p-values 2147483647,3037000493
53
+ --max-docs 2000
54
+ --random-pairs 3000
55
+ --num-queries 200
56
+ --include-scaling
57
+ ```
58
+
59
+ Example (offline/local corpus only):
60
+
61
+ ```bash
62
+ python3 benchmarks/benchmark_claims_suite.py
63
+ --datasets synthetic,local
64
+ --local-docs /path/to/docs.jsonl
65
+ --seeds 42,43,44
66
+ ```
67
+
68
+ Outputs are written to `benchmark_outputs/` by default:
69
+
70
+ - `raw_runs.json` / `raw_runs.csv`
71
+ - `summary_stats.json` / `summary_stats.csv`
72
+ - `run_metadata.json`
73
+ - `skipped_runs.json`
74
+
75
+ ### Benchmark data setup
76
+
77
+ Pull required benchmark datasets into local project paths:
78
+
79
+ ```bash
80
+ python3 scripts/setup_benchmark_data.py
81
+ ```
82
+
83
+ This prepares:
84
+
85
+ - `data/simplewiki-latest-pages-articles.xml.bz2` (for Wikipedia benchmarks)
86
+ - `.cache/scikit_learn_data` (for `20newsgroups` benchmarks)
87
+
88
+ Optional flags:
89
+
90
+ ```bash
91
+ python3 scripts/setup_benchmark_data.py --force
92
+ python3 scripts/setup_benchmark_data.py --skip-wikipedia
93
+ python3 scripts/setup_benchmark_data.py --skip-20newsgroups
94
+ ```
95
+
96
+ ### Individual Benchmarks
97
+
98
+ You can run individual benchmarks instead of the full suite:
99
+
100
+ ```bash
101
+ # Accuracy-only
102
+ python3 benchmarks/benchmark_claims_accuracy.py --datasets synthetic,20newsgroups,wikipedia
103
+
104
+ # Performance-only
105
+ python3 benchmarks/benchmark_claims_performance.py --datasets synthetic,20newsgroups,wikipedia
106
+
107
+ # Memory-only
108
+ python3 benchmarks/benchmark_claims_memory.py --datasets synthetic,20newsgroups,wikipedia
109
+
110
+ # Scaling-only (synthetic sweeps)
111
+ python3 benchmarks/benchmark_claims_scaling.py --datasets synthetic
112
+ ```
113
+
114
+ Each individual benchmark writes outputs under `benchmark_outputs/<test_name>/`.
@@ -0,0 +1,95 @@
1
+ # Minhashlib
2
+
3
+ This is a minimal implementation of MinHashing as described in Jeffrey Ullman's book *Mining Massive Datasets*.
4
+
5
+ ## Current Benchmark Claim
6
+
7
+ Based on the benchmark outputs in this repository:
8
+
9
+ - On recent multi-seed CPU synthetic runs (seeds `42-46`), `minhashlib` builds signatures about `~6x` faster than `datasketch`.
10
+ - Accuracy is comparable in magnitude (similar MAE scale and matching threshold-based metrics in these runs), though `datasketch` is slightly better on MAE in most synthetic scenarios.
11
+ - Memory results are workload-dependent, so this project does not claim universal memory superiority.
12
+
13
+ In short: this implementation is minimal and fast, with accuracy that is broadly comparable to `datasketch`, but outcomes vary depending on dataset and configuration.
14
+
15
+ ## Benchmark Suite
16
+
17
+ Use `benchmarks/benchmark_claims_suite.py` to run comprehensive, reproducible benchmarks:
18
+
19
+ - Multiple datasets (`synthetic`, `20newsgroups`, `wikipedia`, `ag_news`, `local`)
20
+ - Multiple seeds with mean/std/95% CI
21
+ - Metrics: MAE(Mean Average Error), Precision/Recall/F1 at threshold, Precision@K/Recall@K
22
+ - Speed: build/pair-eval/retrieval latency and throughput
23
+ - Memory: peak allocation and bytes/signature
24
+ - Optional scaling sweeps over docs/number of hashes/doc length
25
+
26
+ Example (full):
27
+
28
+ ```bash
29
+ python3 benchmarks/benchmark_claims_suite.py
30
+ --datasets synthetic,20newsgroups,wikipedia
31
+ --wiki-dump-path data/simplewiki-latest-pages-articles.xml.bz2
32
+ --seeds 42,43,44
33
+ --p-values 2147483647,3037000493
34
+ --max-docs 2000
35
+ --random-pairs 3000
36
+ --num-queries 200
37
+ --include-scaling
38
+ ```
39
+
40
+ Example (offline/local corpus only):
41
+
42
+ ```bash
43
+ python3 benchmarks/benchmark_claims_suite.py
44
+ --datasets synthetic,local
45
+ --local-docs /path/to/docs.jsonl
46
+ --seeds 42,43,44
47
+ ```
48
+
49
+ Outputs are written to `benchmark_outputs/` by default:
50
+
51
+ - `raw_runs.json` / `raw_runs.csv`
52
+ - `summary_stats.json` / `summary_stats.csv`
53
+ - `run_metadata.json`
54
+ - `skipped_runs.json`
55
+
56
+ ### Benchmark data setup
57
+
58
+ Pull required benchmark datasets into local project paths:
59
+
60
+ ```bash
61
+ python3 scripts/setup_benchmark_data.py
62
+ ```
63
+
64
+ This prepares:
65
+
66
+ - `data/simplewiki-latest-pages-articles.xml.bz2` (for Wikipedia benchmarks)
67
+ - `.cache/scikit_learn_data` (for `20newsgroups` benchmarks)
68
+
69
+ Optional flags:
70
+
71
+ ```bash
72
+ python3 scripts/setup_benchmark_data.py --force
73
+ python3 scripts/setup_benchmark_data.py --skip-wikipedia
74
+ python3 scripts/setup_benchmark_data.py --skip-20newsgroups
75
+ ```
76
+
77
+ ### Individual Benchmarks
78
+
79
+ You can run individual benchmarks instead of the full suite:
80
+
81
+ ```bash
82
+ # Accuracy-only
83
+ python3 benchmarks/benchmark_claims_accuracy.py --datasets synthetic,20newsgroups,wikipedia
84
+
85
+ # Performance-only
86
+ python3 benchmarks/benchmark_claims_performance.py --datasets synthetic,20newsgroups,wikipedia
87
+
88
+ # Memory-only
89
+ python3 benchmarks/benchmark_claims_memory.py --datasets synthetic,20newsgroups,wikipedia
90
+
91
+ # Scaling-only (synthetic sweeps)
92
+ python3 benchmarks/benchmark_claims_scaling.py --datasets synthetic
93
+ ```
94
+
95
+ Each individual benchmark writes outputs under `benchmark_outputs/<test_name>/`.
@@ -0,0 +1,3 @@
1
+ from .diffchecker import DiffChecker
2
+
3
+ __all__ = ["DiffChecker"]
@@ -0,0 +1,63 @@
1
+ import numpy as np
2
+ from numba import njit
3
+ from xxhash import xxh3_64_intdigest
4
+ from numpy.typing import NDArray
5
+
6
+
7
+ @njit(cache=True)
8
+ def _generate_signature_numba(
9
+ shingles: NDArray[np.int64], hash_params: NDArray[np.int64], p: int
10
+ ) -> NDArray[np.int64]:
11
+ num_perm = hash_params.shape[0]
12
+ sig = np.empty(num_perm, dtype=np.int64)
13
+ sig[:] = p
14
+
15
+ for i in range(shingles.shape[0]):
16
+ shingle = shingles[i]
17
+ for j in range(num_perm):
18
+ val = (hash_params[j, 0] * shingle + hash_params[j, 1]) % p
19
+ if val < sig[j]:
20
+ sig[j] = val
21
+
22
+ return sig
23
+
24
+
25
+ class DiffChecker:
26
+ def __init__(self, p=3037000493, k=2, num_perm=128, seed: int | None = 42):
27
+ self.p = p
28
+ self.k = k
29
+ self.num_perm = num_perm
30
+ self.rng = np.random.default_rng(seed)
31
+ self.signatureMatrix = np.empty((num_perm, 0), dtype=np.int64)
32
+ self.hashParams = self.generateKHashParameters()
33
+
34
+ def generateKShingles(self, document: str) -> NDArray[np.int64]:
35
+ if len(document) < self.k:
36
+ return np.array([], dtype=np.int64)
37
+
38
+ shingles = np.fromiter(
39
+ (
40
+ xxh3_64_intdigest(document[i : i + self.k].encode("utf-8")) % self.p
41
+ for i in range(len(document) - self.k + 1)
42
+ ),
43
+ dtype=np.int64,
44
+ )
45
+
46
+ return np.unique(shingles)
47
+
48
+ def generateKHashParameters(self) -> NDArray[np.int64]:
49
+ a = self.rng.integers(1, self.p, size=self.num_perm, dtype=np.int64)
50
+ b = self.rng.integers(0, self.p, size=self.num_perm, dtype=np.int64)
51
+ return np.column_stack((a, b))
52
+
53
+ def generateSignature(self, document: str) -> NDArray[np.int64]:
54
+ shingles = self.generateKShingles(document)
55
+ return _generate_signature_numba(shingles, self.hashParams, self.p)
56
+
57
+ def appendDocumentAsSignature(self, document: str):
58
+ signature = self.generateSignature(document)
59
+ self.signatureMatrix = np.column_stack([self.signatureMatrix, signature])
60
+
61
+ @staticmethod
62
+ def checkJaccardSignatureSimilarity(a: NDArray[np.int64], b: NDArray[np.int64]) -> float:
63
+ return float(np.count_nonzero(a == b)) / float(a.size)
@@ -0,0 +1,43 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "minhashlib"
7
+ version = "0.1.0"
8
+ dependencies = [
9
+ "numpy",
10
+ "numba",
11
+ "xxhash",
12
+ ]
13
+ requires-python = ">=3.10"
14
+ authors = [
15
+ {name = "Sachin Avutu", email = "ssavutu@gmail.com"},
16
+ ]
17
+ maintainers = [
18
+ {name = "Sachin Avutu", email = "ssavutu@gmail.com"}
19
+ ]
20
+ description = "A fast and minimal minhashing based similarity checking library."
21
+ readme = "README.md"
22
+ license = "MIT"
23
+ license-files = ["LICENSE"]
24
+ keywords = ["minhashing", "minhash", "similarity", "difference", "entity resolution", "deduplication"]
25
+ classifiers = [
26
+ "Development Status :: 3 - Alpha",
27
+ "Programming Language :: Python"
28
+ ]
29
+
30
+ [project.urls]
31
+ Repository = "https://github.com/ssavutu/minhashlib"
32
+ "Bug Tracker" = "https://github.com/ssavutu/minhashlib/issues"
33
+
34
+ [tool.hatch.build.targets.wheel]
35
+ packages = ["minhashlib"]
36
+
37
+ [tool.hatch.build.targets.sdist]
38
+ include = [
39
+ "minhashlib",
40
+ "README.md",
41
+ "LICENSE",
42
+ "pyproject.toml",
43
+ ]