-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloom_filter.py
99 lines (64 loc) · 2.09 KB
/
bloom_filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import math
import mmh3
from bitarray import bitarray
class BloomFilter(object):
def __init__(self, items_count, fault):
self.fault = fault
self.size = self.get_size(items_count, fault)
self.hash_count = self.get_hash_count(self.size, items_count)
self.bit_array = bitarray(self.size)
self.bit_array.setall(0)
def add(self, item):
for i in range(self.hash_count):
digest = mmh3.hash(item, i) % self.size
self.bit_array[digest] = True
def check(self, item):
for i in range(self.hash_count):
digest = mmh3.hash(item, i) % self.size
if self.bit_array[digest] == False:
return False
return True
@classmethod
def get_size(self, n, p):
m = -(n * math.log(p)) / (math.log(2) ** 2)
return int(m)
@classmethod
def get_hash_count(self, m, n):
k = (m / n) * math.log(2)
return int(k)
############################################### TESTING !!! #####################################################
from random import choice
import string
import time
def get_random_string(length):
result_str = ''.join(choice(string.ascii_lowercase) for i in range(length))
return result_str
# INPUTS
n = 700000 #n o of items to add
p = 0.1 # set false positive probability
r = 100000 # no of reads
start = time.time()
bloomf = BloomFilter(n,p)
faults = 0
yes = 0
words_added = set()
for i in range(n):
word = get_random_string(6)
words_added.add(word)
bloomf.add(word) # (i%10) +5
for i in range(r):
word = get_random_string(6)
if bloomf.check(word):
if word not in words_added:
faults += 1
else:
yes += 1
# STATS
print("Size of bit array: {}".format(bloomf.size))
print("False positive Probability: {}".format(bloomf.fault))
print("Number of hash functions: {}\n".format(bloomf.hash_count))
print(f"'{faults}' FALSE POSITIVES")
print(f"'{yes}' PRESENT")
print(f"'{r-yes-faults}' NOT PRESENT\n")
end = time.time()
print(f"{math.ceil(end - start)} sec. Script run time")