|
| 1 | +from bloomfilter import BloomFilter |
| 2 | +from random import shuffle |
| 3 | + |
| 4 | +n = 20 #no of items to add |
| 5 | +p = 0.05 #false positive probability |
| 6 | + |
| 7 | +bloomf = BloomFilter(n,p) |
| 8 | +print("Size of bit array:{}".format(bloomf.size)) |
| 9 | +print("False positive Probability:{}".format(bloomf.fp_prob)) |
| 10 | +print("Number of hash functions:{}".format(bloomf.hash_count)) |
| 11 | + |
| 12 | +# words to be added |
| 13 | +word_present = ['abound','abounds','abundance','abundant','accessable', |
| 14 | + 'bloom','blossom','bolster','bonny','bonus','bonuses', |
| 15 | + 'coherent','cohesive','colorful','comely','comfort', |
| 16 | + 'gems','generosity','generous','generously','genial'] |
| 17 | + |
| 18 | +# word not added |
| 19 | +word_absent = ['bluff','cheater','hate','war','humanity', |
| 20 | + 'racism','hurt','nuke','gloomy','facebook', |
| 21 | + 'geeksforgeeks','twitter'] |
| 22 | + |
| 23 | +for item in word_present: |
| 24 | + bloomf.add(item) |
| 25 | + |
| 26 | +shuffle(word_present) |
| 27 | +shuffle(word_absent) |
| 28 | + |
| 29 | +test_words = word_present[:10] + word_absent |
| 30 | +shuffle(test_words) |
| 31 | +for word in test_words: |
| 32 | + if bloomf.check(word): |
| 33 | + if word in word_absent: |
| 34 | + print("'{}' is a false positive!".format(word)) |
| 35 | + else: |
| 36 | + print("'{}' is probably present!".format(word)) |
| 37 | + else: |
| 38 | + print("'{}' is definitely not present!".format(word)) |
0 commit comments