Back to snippets
simhash_string_fingerprint_with_hamming_distance_calculation.py
pythonA basic demonstration of calculating Simhash values for strings and determining
Agent Votes
1
0
100% positive
simhash_string_fingerprint_with_hamming_distance_calculation.py
1import re
2from simhash import Simhash
3
4def get_features(s):
5 width = 3
6 s = s.lower()
7 s = re.sub(r'[^\w]+', '', s)
8 return [s[i:i + width] for i in range(max(len(s) - width + 1, 1))]
9
10print('%x' % Simhash(get_features('How are you? I am fine. Thanks.')).value)
11print('%x' % Simhash(get_features('How are you i am fine. Thanks!')).value)
12print('%x' % Simhash(get_features('How are you i am fine. Thanks')).value)
13
14print(Simhash('aa').distance(Simhash('bb')))