Registry / ai-ml / simhash

simhash

JSON →
library2.1.2pypypi✓ verified 84d ago

The `simhash` library provides a Python implementation of the Simhash Algorithm, a technique for quickly finding near-duplicate documents or comparing the similarity of two texts or data objects. It's highly useful for tasks like large-scale content deduplication, spam detection, and content recommendation, offering a fast way to identify perceptually similar items. The current version is 2.1.2, and it follows an irregular release cadence based on contributions and bug fixes.

pip install simhash
INSTALL
IMPORT
SIG · SIMHASH
S
simhash
ai-mlpythonv2.1.2
Install
3.6s avg
Import
265ms
Disk
89MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.1.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.256s · 89.4MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 3.6s · import 0.273s · 86MB
89MB installed
● package 89MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Simhash
from simhash import Simhash

This quickstart demonstrates how to create `Simhash` objects from strings and calculate the Hamming distance between them. A smaller distance indicates greater similarity. The choice of similarity threshold depends on your specific application.

from simhash import Simhash # Create Simhash objects from text text1 = "The quick brown fox jumps over the lazy dog." text2 = "A quick brown fox jumps over the sleeping dog." text3 = "Python is a programming language." hash1 = Simhash(text1) hash2 = Simhash(text2) hash3 = Simhash(text3) # Calculate Hamming distance between hashes # A lower distance means higher similarity print(f"Distance between '{text1[:20]}...' and '{text2[:20]}...': {hash1.distance(hash2)}") print(f"Distance between '{text1[:20]}...' and '{text3[:20]}...': {hash1.distance(hash3)}") # You can use a similarity threshold to determine if items are 'duplicates' similarity_threshold = 3 if hash1.distance(hash2) < similarity_threshold: print(f"'{text1[:20]}...' and '{text2[:20]}...' are considered very similar.") else: print(f"'{text1[:20]}...' and '{text2[:20]}...' are not considered very similar.")
Debug
Known issues
gotchaThe default feature extraction (`f` parameter) is a simple word tokenizer, which may not be optimal for all types of text (e.g., code, specific languages, structured data). For better accuracy, especially with diverse content, consider providing a custom feature extraction function.
fix
Implement a custom callable function for feature extraction that takes a string and returns an iterable of features (strings or numbers), then pass it as `f=your_function` to the `Simhash` constructor.
affects: All
gotchaSimhash objects cannot be directly compared for hash value equality using `==`. Doing so will compare the object identities, not their underlying hash values, always returning `False` for distinct objects.
fix
To compare the actual hash values, use `simhash_obj_a.value == simhash_obj_b.value`. To measure similarity, use `simhash_obj_a.distance(simhash_obj_b)` which returns the Hamming distance.
affects: All
gotchaThe `distance()` method returns the Hamming distance, which is an integer representing the number of differing bits between two hashes. A *lower* distance indicates *higher* similarity, not the other way around. This can be counter-intuitive compared to similarity scores that range from 0 to 1.
fix
Interpret `distance(other)` results carefully: 0 means identical hashes, while higher numbers mean less similar. Define an appropriate distance threshold for your application's 'similar' definition (e.g., distance < 3).
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'simhash'
The `simhash` library has not been installed in your Python environment.
fix
Run `pip install simhash` in your terminal to install the library.
TypeError: 'str' object is not callable
You likely passed a string directly to the `f` parameter of the `Simhash` constructor, but it expects a callable function for feature extraction.
fix
Ensure the `f` parameter is assigned a function that takes a string and returns an iterable of features, e.g., `Simhash(text, f=my_feature_extractor_function)`.
ValueError: input is empty or contains no features
The input text or the custom feature extraction function (`f`) resulted in no features being generated, leading to an inability to compute the hash.
fix
Check the input string for content. If using a custom `f` function, ensure it correctly processes the input and returns at least one feature for non-empty or meaningful inputs. For example, your tokenizer might be too aggressive, removing all tokens.
Upgrade
Version history
2.1.2latest on PyPI · released Mar 3, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
simhash — pip install simhash · libregistry