daily pastebin goal
2%
SHARE
TWEET

Untitled

a guest Dec 14th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/env python
  2. # -*- coding: UTF-8 -*-
  3. __author__ = 'Myautsai Pan'
  4. import sys
  5. import numpy as np
  6.  
  7. class SimHash(object):
  8.     '''Implements of SimHash for chromaprint'''
  9.     def __init__(self, chromaprint=[], bits=32):
  10.         self.hash_bits = 32
  11.         self.value = self.cal_hash(chromaprint)
  12.  
  13.     def cal_hash(self, chromaprint):
  14.         ''' input a hashed array `chromaprint`, output `self.hash_bits` bit hash'''
  15.         Q = [0]*self.hash_bits
  16.         for fp_hash in chromaprint:
  17.             #TODO assert type(fp_hash) == int, "assert fingerprint type being integer."
  18.             for i in xrange(self.hash_bits):
  19.                 r =(((fp_hash >> i) & 1) << 1) -1 # right started i'th(start from 0) bit * 2 -1
  20.                 assert r == 1 or r == -1
  21.                 Q[i] += r
  22.         sim_hash = 0
  23.         for i in xrange(self.hash_bits):
  24.             w = 1 if Q[i]>0 else 0
  25.             sim_hash |= w << i
  26.         return sim_hash
  27.    
  28.     def __int__(self):
  29.         return int(self.value)
  30.    
  31.     def __long__(self):
  32.         return long(self.value)
  33.  
  34.     def __hash__(self):
  35.         return self.value
  36.  
  37.     def __str__(self):
  38.         s = [str(self.value >> i & 1) for i in xrange(self.hash_bits)]
  39.         s.reverse()
  40.         return ''.join(s)
  41.  
  42.     def __repr__(self):
  43.         return ' '.join(('<SimHash',self.__str__(),' (',str(self.__int__()),')>'))
  44.    
  45.     def __call__(self):
  46.         return self.value
  47.  
  48.  
  49. def main(argv):
  50.     a = SimHash([1,2,8,8,8,8,8])
  51.     a = SimHash([1,2,8,8,8,8,7])
  52.     print 'SimHash a:',repr(a)
  53.     return 0
  54.  
  55. if __name__ == '__main__':
  56.     sys.exit(main(sys.argv))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top