Bloom Filter Using Python

Bloom Filter Algorithm of Big Data Analysis Using Python 

SourceCode : 

def sum(inp):
    s = 0
    for ch in inp:
        s = s+ord(ch)
    return s

def h1(inp):
    s = sum(inp)
    return(3*s+5)%15

def h2(inp):
    s = sum(inp)
    return(7*s+3)%13

bit_vector = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

while True:
    inp = raw_input("Enter the string :")
    if(inp == 'stop'):
        break
    print("H1",h1(inp),"H2",h2(inp))
    bit_vector[h1(inp)] = 1
    bit_vector[h2(inp)] = 1
    print(bit_vector)

inp = raw_input("\n Enter a string to check the memebership : ")
if(bit_vector[h1(inp)] == 1 and bit_vector[h2(inp)] == 1 ):
    print(inp,"may be present")
else:
    print(inp,"not present")



OUTPUT:

 Enter the string : Ashish
('H1', 5, 'H2', 11)
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0]
Enter the string :Rahul
('H1', 14, 'H2', 10)
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 1]
Enter the string :Aditya
('H1', 2, 'H2', 6)
[0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1]
Enter the string :Vishal
('H1', 5, 'H2', 5)
[0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1]
Enter the string :stop

 Enter a string to check the memebership : Vishal
('Vishal', 'may be present')

Comments