The :ref:`minhash_lsh` index. It supports query with `Jaccard similarity`_ threshold. Reference: `Chapter 3, Mining of Massive Datasets `_. Args: threshold (float): The Jaccard similarity threshold between 0.0 and 1.0. The initialized M
| 47 | for _ in range(length)).encode('utf8') |
| 48 | |
| 49 | class MinHashLSH(object): |
| 50 | ''' |
| 51 | The :ref:`minhash_lsh` index. |
| 52 | It supports query with `Jaccard similarity`_ threshold. |
| 53 | Reference: `Chapter 3, Mining of Massive Datasets |
| 54 | <http://www.mmds.org/>`_. |
| 55 | |
| 56 | Args: |
| 57 | threshold (float): The Jaccard similarity threshold between 0.0 and |
| 58 | 1.0. The initialized MinHash LSH will be optimized for the threshold by |
| 59 | minizing the false positive and false negative. |
| 60 | num_perm (int, optional): The number of permutation functions used |
| 61 | by the MinHash to be indexed. For weighted MinHash, this |
| 62 | is the sample size (`sample_size`). |
| 63 | weights (tuple, optional): Used to adjust the relative importance of |
| 64 | minimizing false positive and false negative when optimizing |
| 65 | for the Jaccard similarity threshold. |
| 66 | `weights` is a tuple in the format of |
| 67 | :code:`(false_positive_weight, false_negative_weight)`. |
| 68 | params (tuple, optional): The LSH parameters (i.e., number of bands and size |
| 69 | of each bands). This is used to bypass the parameter optimization |
| 70 | step in the constructor. `threshold` and `weights` will be ignored |
| 71 | if this is given. |
| 72 | storage_config (dict, optional): Type of storage service to use for storing |
| 73 | hashtables and keys. |
| 74 | `basename` is an optional property whose value will be used as the prefix to |
| 75 | stored keys. If this is not set, a random string will be generated instead. If you |
| 76 | set this, you will be responsible for ensuring there are no key collisions. |
| 77 | prepickle (bool, optional): If True, all keys are pickled to bytes before |
| 78 | insertion. If None, a default value is chosen based on the |
| 79 | `storage_config`. |
| 80 | hashfunc (function, optional): If a hash function is provided it will be used to |
| 81 | compress the index keys to reduce the memory footprint. This could cause a higher |
| 82 | false positive rate. |
| 83 | |
| 84 | Note: |
| 85 | `weights` must sum to 1.0, and the format is |
| 86 | (false positive weight, false negative weight). |
| 87 | For example, if minimizing false negative (or maintaining high recall) is more |
| 88 | important, assign more weight toward false negative: weights=(0.4, 0.6). |
| 89 | Try to live with a small difference between weights (i.e. < 0.5). |
| 90 | ''' |
| 91 | |
| 92 | def __init__(self, threshold=0.9, num_perm=128, weights=(0.5, 0.5), |
| 93 | params=None, storage_config=None, prepickle=None, hashfunc=None): |
| 94 | storage_config = {'type': 'dict'} if not storage_config else storage_config |
| 95 | self._buffer_size = 50000 |
| 96 | if threshold > 1.0 or threshold < 0.0: |
| 97 | raise ValueError("threshold must be in [0.0, 1.0]") |
| 98 | if num_perm < 2: |
| 99 | raise ValueError("Too few permutation functions") |
| 100 | if any(w < 0.0 or w > 1.0 for w in weights): |
| 101 | raise ValueError("Weight must be in [0.0, 1.0]") |
| 102 | if sum(weights) != 1.0: |
| 103 | raise ValueError("Weights must sum to 1.0") |
| 104 | self.h = num_perm |
| 105 | if params is not None: |
| 106 | self.b, self.r = params |
no outgoing calls
no test coverage detected