Seite 1 von 1

Can someone explain me the meaning of the functions?

Verfasst: Sonntag 12. April 2020, 15:52
von Kathi94
Can someone pls explain me, what the functions add und look up do?
Especially I don't understand the meaning of seed and string, what are they doing?


from bitarray import bitarray
import mmh3

class BloomFilter:

def __init__(self, size, hash_count):
self.size = size
self.hash_count = hash_count
self.bit_array = bitarray(size)
self.bit_array.setall(0)

def add(self, string):
for seed in xrange(self.hash_count):
result = mmh3.hash(string, seed) % self.size
self.bit_array[result] = 1

def lookup(self, string):
for seed in xrange(self.hash_count):
result = mmh3.hash(string, seed) % self.size
if self.bit_array[result] == 0:
return "Nope"
return "Probably"

bf = BloomFilter(500000, 7)

lines = open("/usr/share/dict/american-english").read().splitlines()
for line in lines:
bf.add(line)

print bf.lookup("google")
>>> Nope
print bf.lookup("Max")
>>> Probably
print bf.lookup("mice")
>>> Probably
print bf.lookup("3")
>>> Nope

Das Code gehört Max Burstein