-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloom.py
More file actions
102 lines (78 loc) · 3.15 KB
/
bloom.py
File metadata and controls
102 lines (78 loc) · 3.15 KB
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
98
from math import ceil, log
import mmh3
import sys
import csv
import random as rm
from bitarray import bitarray
class HashFunction:
def __init__(self, seed: int, size: int):
self.seed: int = seed
self.array_size: int = size
def __call__(self,key: str):
return mmh3.hash128(key,self.seed) % self.array_size
class BloomFilter: #construct a bloomfilter based on a dataset
def constructBloomFilter(n: int,targetProbability = 0.0000001):
#returns the empty bloomFilter bitarray and the hash functions
#n: expected number of items initially in the bloom filter
#targetProbability: is the expected number of false positives
m = ceil((n * log(targetProbability)) / log(1 / pow(2, log(2))))
k = ceil(m/n * log(2,2))
hash_functions = []
fib3 = 1 #use fibonnacci sequence to generate seeds
fib2 = 1
fib1 = 0
for _ in range(k):
fib3 = fib2 + fib1
fib1 = fib2
fib2 = fib3
hash_functions.append(HashFunction(fib3,m))
bloomFilter = bitarray(m)
return (bloomFilter, hash_functions)
def __init__(self, size: int, p = 0.0000001): #initialize bloom filter
self.array, self.hash_functions = BloomFilter.constructBloomFilter(size,p)
def lookup(self,element: str): #check if value in bloom filter
for hash in self.hash_functions:
hashedIndex: int = hash(element)
if self.array[hashedIndex] == False:
return False
return True
def insert(self,element):
for hash in self.hash_functions:
hashedIndex = hash(element)
self.array[hashedIndex] = True
def insertList(self,dataset):
for element in dataset:
for hash in self.hash_functions:
hashedIndex = hash(element)
self.array[hashedIndex] = True
arguments = sys.argv
#capture the paths of the CSV's
if len(arguments) >= 3:
emailListCSV = arguments[1]
emailTestCSV = arguments[2]
emailList = []
with open(emailListCSV, 'r') as csvfile:
emailreader = csv.reader(csvfile)
next(emailreader) #skip the headers
for email in emailreader:
if email:
emailList.append(email[0])
emailBloomFilter = BloomFilter(len(emailList),0.0000001)
#insert all of our emails into the bloom filter
for email in emailList:
emailBloomFilter.insert(email)
#generate OutputCSV
with open('results.csv', 'w', newline="") as outputfile:
csvwriter = csv.writer(outputfile)
header = ['Email','Result']
csvwriter.writerow(header)
with open(emailTestCSV, 'r') as testfile:
emailreader = csv.reader(testfile)
next(emailreader) #skip the header
for email in emailreader:
if email:
elementFound = emailBloomFilter.lookup(email[0])
if elementFound:
csvwriter.writerow((email,"Probably in the DB"))
else:
csvwriter.writerow((email,"Not in the DB"))