python 实现的简单的BloomFilter

一个简单的BloomFilter的python实现,接触python没多久,代码不是很规范

# /usr/bin/env python

#python implemented simple bloomfilter

class BloomFilter():
    
    def __init__(self,length):
        self.length = length
        self.bitlist = [0]*length
        
    def read_file(self,file):
        str = open(file,"r")
        string = str.readlines()
        t = 0
        for eachLine in string:
            for str in eachLine.split():
                t += 1
                self.bitlist[(self.sax_hash(str))%self.length] = 1
                self.bitlist[(self.sdbm_hash(str))%self.length] = 1
        print t

    def sax_hash(self,string):
        h = 0
        for str in string:
            h ^= (h<<5) + (h>>2) + ord(str)
        return h
    
    def sdbm_hash(self,string):
        h = 0
        for str in string:
            h = ord(str) + (h<<6) + (h<<16) - h
        return h
        
             
    def is_in_file(self,str):
        if self.bitlist[(self.sax_hash(str))%self.length]\
           and  self.bitlist[(self.sdbm_hash(str))%self.length]:
            return True
        else:
            return False

def main():
    file_str = "data.txt"
    bf = BloomFilter(1<<20)
    bf.read_file(file_str)
    while 1:
        print "Enter the word:"
        str = raw_input('')
        print bf.is_in_file(str)
     
if __name__ == "__main__":
    main()
    




                                    
发布了54 篇原创文章 · 获赞 1 · 访问量 3万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览