-
Notifications
You must be signed in to change notification settings - Fork 2
/
dualcode.py
61 lines (50 loc) · 1.86 KB
/
dualcode.py
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
import hashlib
import numpy as np
import cryptography
import Cryptodome
from pybloom_live import ScalableBloomFilter, BloomFilter
'''
对偶编码函数,将字符转成向量形势
给定字符串 S_1 = C_1C_2C_3....C_n
和二进制向量 S 2 = b0b1 … b_(m-1) ,
S 2 中每位元素的初始值为0 ,其中 n < m .通过 Hash函数 H ,
把 S1中相邻2个字符散列映射为0~ ( m -1 )之间的数,
当且仅当 H(C_j , C_{j+1} ) = i 时, b_i =1
'''
def Dual_code(S1):
temp = []
for index,value in enumerate(S1):
x = 0
temp1 = [x for i in range(676)] #建立一个全为0的数组
# print(value)
length=len(value)
for i,v in enumerate(value): # 遍历当前关键字
if(i==length-1):
C = ord(v)+ord(' ') #如果是最后一个字符,将最后一个字符转换成ascii相加
C = C%676
while(temp1[C]==1): #将hash表中对应位置置1,如果该位置已经有1,则后移
if(C!=675):
C+=1
else:
C=0
temp1[C] = 1
else:
C = ord(v)+ord(S1[index][i+1])
C = C%676
while (temp1[C] == 1):
if (C != 675):
C += 1
else:
C = 0
temp1[C] = 1
temp.append(temp1)
return temp
if __name__ == '__main__':
S1=['jugment ae12','kugou']
S2=Dual_code(S1)
print(S2[0].count(1))
print(S2[0])
k=np.random.randint(0,1,128)
bloom = BloomFilter(capacity=1024, error_rate=0.01)
bloom.add(S2[0])
print(S2[0] in bloom)