Skip to main content

A fast and minimal minhashing based similarity checking library.

Project description

minhashlib

minhashlib is a fast, minimal MinHash library for string similarity and near-duplicate detection, inspired by Jeffrey Ullman's Mining Massive Datasets.

Signatures are computed with a Numba-compiled permutation kernel over xxh3 k-shingles, which makes signature generation considerably faster than datasketch's default configuration. For finding near-duplicates across many documents without paying the O(n²) cost of all-pairs comparison, it ships a locality-sensitive hashing (LSH) index.

Installation

pip install minhashlib

Usage

Comparing two documents

from minhashlib import MinHash

mh = MinHash(num_perm=128, k=3)
mh.compare("near-duplicate detection", "near duplicate detection")  # -> ~0.9

MinHash is stateless: compare computes signatures and discards them. When comparing one document against many, compute each signature once and reuse it:

from minhashlib import MinHash, similarity

mh = MinHash(num_perm=128, k=3)
sigs = [mh.signature(doc) for doc in documents]   # O(n) signature generation
similarity(sigs[0], sigs[1])                       # cheap pairwise estimate

Near-duplicate search at scale (LSH)

MinHashLSH buckets signatures by bands so that only likely-similar documents are ever compared. Configure it with a similarity threshold (bands/rows are derived automatically) or set bands/rows explicitly.

from minhashlib import MinHash, MinHashLSH

mh = MinHash(num_perm=128, k=3)
lsh = MinHashLSH(threshold=0.8, num_perm=128)      # or MinHashLSH(num_perm=128, bands=32, rows=4)

for key, text in corpus.items():
    lsh.insert(key, mh.signature(text))

candidates = lsh.query(mh.signature(query_text))   # keys likely similar to query_text

insert, query, remove, in, and len are supported. The MinHash used for the index and for queries must share the same num_perm, k, p, and seed (the default seed makes signatures comparable across instances).

API

Object Purpose
MinHash(p, k, num_perm, seed) Signature engine. signature(doc), compare(a, b).
similarity(sig_a, sig_b) Estimate the Jaccard similarity of two signatures.
MinHashLSH(threshold, num_perm, bands=None, rows=None) LSH index. insert, query, remove.

Scope

minhashlib aims to be a fast, lean MinHash core plus LSH search. It does not target the full feature surface of datasketch (weighted MinHash, HyperLogLog, storage backends, etc.).

Contributing

Contributions are welcome.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minhashlib-0.2.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minhashlib-0.2.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file minhashlib-0.2.0.tar.gz.

File metadata

  • Download URL: minhashlib-0.2.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for minhashlib-0.2.0.tar.gz
Algorithm Hash digest
SHA256 335864da82a3444f4610ed6b5706a4a85bffa8f64a8376670ba42c6925ed9f85
MD5 f08a9d267d0bdc039a8d75878da5f9ac
BLAKE2b-256 2bbd7f5586453c2c757bff95d4c90b06ee6776f9656ef971756f0bb43c71c46d

See more details on using hashes here.

File details

Details for the file minhashlib-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: minhashlib-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for minhashlib-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d2cc5abc112ab4705121e78f060af5929d464c7cb8b05119c88c22a0b32bc836
MD5 0de9476cc5df89f96bd97dda9dfa1c7c
BLAKE2b-256 bb420c71cdee1e045bb34afc19166fb35cdae3f332e188b62f38267ef069a653

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page