MinHash is a probabilistic data structure for computing `Jaccard similarity`_ between sets. Args: num_perm (int, optional): Number of random permutation functions. It will be ignored if `hashvalues` is not None. seed (int, optional): The random seed controls the
| 26 | _hash_range = (1 << 32) |
| 27 | |
| 28 | class MinHash(object): |
| 29 | '''MinHash is a probabilistic data structure for computing |
| 30 | `Jaccard similarity`_ between sets. |
| 31 | |
| 32 | Args: |
| 33 | num_perm (int, optional): Number of random permutation functions. |
| 34 | It will be ignored if `hashvalues` is not None. |
| 35 | seed (int, optional): The random seed controls the set of random |
| 36 | permutation functions generated for this MinHash. |
| 37 | hashfunc (optional): The hash function used by this MinHash. |
| 38 | It takes the input passed to the `update` method and |
| 39 | returns an integer that can be encoded with 32 bits. |
| 40 | The default hash function is based on SHA1 from hashlib_. |
| 41 | hashobj (**deprecated**): This argument is deprecated since version |
| 42 | 1.4.0. It is a no-op and has been replaced by `hashfunc`. |
| 43 | hashvalues (`numpy.array` or `list`, optional): The hash values is |
| 44 | the internal state of the MinHash. It can be specified for faster |
| 45 | initialization using the existing state from another MinHash. |
| 46 | permutations (optional): The permutation function parameters. This argument |
| 47 | can be specified for faster initialization using the existing |
| 48 | state from another MinHash. |
| 49 | |
| 50 | Note: |
| 51 | To save memory usage, consider using :class:`datasketch.LeanMinHash`. |
| 52 | |
| 53 | Note: |
| 54 | Since version 1.1.1, MinHash will only support serialization using |
| 55 | `pickle`_. ``serialize`` and ``deserialize`` methods are removed, |
| 56 | and are supported in :class:`datasketch.LeanMinHash` instead. |
| 57 | MinHash serialized before version 1.1.1 cannot be deserialized properly |
| 58 | in newer versions (`need to migrate? <https://github.com/ekzhu/datasketch/issues/18>`_). |
| 59 | |
| 60 | Note: |
| 61 | Since version 1.1.3, MinHash uses Numpy's random number generator |
| 62 | instead of Python's built-in random package. This change makes the |
| 63 | hash values consistent across different Python versions. |
| 64 | The side-effect is that now MinHash created before version 1.1.3 won't |
| 65 | work (i.e., ``jaccard``, ``merge`` and ``union``) |
| 66 | with those created after. |
| 67 | |
| 68 | .. _`Jaccard similarity`: https://en.wikipedia.org/wiki/Jaccard_index |
| 69 | .. _hashlib: https://docs.python.org/3.5/library/hashlib.html |
| 70 | .. _`pickle`: https://docs.python.org/3/library/pickle.html |
| 71 | ''' |
| 72 | |
| 73 | def __init__(self, num_perm=128, seed=1, |
| 74 | hashfunc=sha1_hash32, |
| 75 | hashobj=None, # Deprecated. |
| 76 | hashvalues=None, permutations=None): |
| 77 | # print("no") |
| 78 | if hashvalues is not None: |
| 79 | num_perm = len(hashvalues) |
| 80 | if num_perm > _hash_range: |
| 81 | # Because 1) we don't want the size to be too large, and |
| 82 | # 2) we are using 4 bytes to store the size value |
| 83 | raise ValueError("Cannot have more than %d number of\ |
| 84 | permutation functions" % _hash_range) |
| 85 | self.seed = seed |
no outgoing calls
no test coverage detected