-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbloomfilter.py
More file actions
47 lines (36 loc) · 1.3 KB
/
bloomfilter.py
File metadata and controls
47 lines (36 loc) · 1.3 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
'''
sonicskye
bloomfilter.py
Requirements:
bitarray (https://github.com/ilanschnell/bitarray)
pybloof (https://github.com/jhgg/pybloof)
Pybloof library is used due to its built-in export and import features
These features are convenient for storing the bloom filter information to the smart contract
and import them if needed
'''
import pybloof
import utilities as u
def createstringbloomfilter(wordlist):
# if the size is too small then the result is inaccurate
# makes sure that there is an extra 500 allowance; false positive still exists
# the input wordlist is converted into setwordlist to remove duplicates
setwordlist = set(wordlist)
sz = len(setwordlist) + 500
bf = pybloof.StringBloomFilter(size=sz, hashes=9)
for word in wordlist:
#bf.add(word)
if word not in bf:
bf.add(word)
return bf.to_base64().decode('utf-8')
def teststringbloomfilter(bfValue, wordlist):
bf = pybloof.StringBloomFilter.from_base64(bfValue.encode('utf-8'))
setwordlist = set(wordlist)
wlength = len(setwordlist)
#print (str(wlength))
positive = 0
for word in setwordlist:
if word in bf:
positive += 1;
res = round(positive/wlength *100)
return res
##################################### test ################################