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.1.tar.gz (5.8 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.1-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: minhashlib-0.2.1.tar.gz
  • Upload date:
  • Size: 5.8 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.1.tar.gz
Algorithm Hash digest
SHA256 fc8a67e5657638bdeaa39bde60793f64d2f99900dfbbeb911a860f9e5f8c3061
MD5 f8d9389b80c245fb1cc78d061ce0b908
BLAKE2b-256 30bfc4d21df89cc05a6f3bbebfe4d605de6eacfa76f5dd518ceba9d300dc5c58

See more details on using hashes here.

File details

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

File metadata

  • Download URL: minhashlib-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 7.0 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c847d7904cd76e82463fab3f27bd4a933e4355d236eb50b1d2925886f95a30f9
MD5 da17af7568be70f954b8450e76b1497c
BLAKE2b-256 6f1da28a0e08bc9019989aa502359372069fc84635320fc5f9ec48503686fbdd

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