Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions simhash/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,22 @@ def distance(self, another):
return ans


class MultiSimhash(Simhash):
def __init__(self, simhashes):
multi_f = 0
if not isinstance(simhashes, Iterable):
raise Exception('Value passed is not a list of simhashes')
for i in simhashes:
multi_f = multi_f + i.f
if multi_f % 8:
raise Exception('Simhashes do not the same length (f)')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, could you explain here, what do you want to check?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, well looking at it twice, that code doesn't make much sense. I will change it to check them all 1 by 1 to the length of the first simshash.

multi_value = self._concatenate_simhashes(simhashes)
super(MultiSimhash, self).__init__(value=multi_value, f=multi_f, hashfunc=simhashes[0].hashfunc)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before using simhases[0], we need to check the length of simhashes to make sure it's not empty. Also since you are using the first element's hashfunc, do we assume all hashfunc should be the same for each element in simahases list?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, will add some check to look if its empty or not.
Regarding the hashfunc, I think it's safe they must be the same. If they are not, which one do you chose for the new multihash?


def _concatenate_simhashes(self, objs):
digests = [int_to_bytes(obj.value, obj.f_bytes) for obj in objs]
return bytes_to_int(b''.join(digests))

class SimhashIndex(object):

def __init__(self, objs, f=64, k=2, log=None):
Expand Down
15 changes: 14 additions & 1 deletion tests/test_simhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from sklearn.feature_extraction.text import TfidfVectorizer

from simhash import Simhash, SimhashIndex
from simhash import MultiSimhash, Simhash, SimhashIndex


class TestSimhash(TestCase):
Expand Down Expand Up @@ -146,6 +146,19 @@ def test_get_near_dup(self):
dups = self.index.get_near_dups(s1)
self.assertEqual(3, len(dups))

class TestMultiSimhash(TestCase):
def test_creation(self):
a = Simhash('My name is John')
b = 1
self.assertRaises(Exception, [a, b])

a = Simhash(0xaaaaaaaa, f=32)
b = Simhash(0xbbbbbbbb, f=32)
ms = MultiSimhash([a, b])
d = Simhash(0xaaaaaaaabbbbbbbb, f=64)
self.assertEqual(ms.value, d.value)

self.assertEqual(ms.f, d.f)

if __name__ == '__main__':
main()