-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithms-left.py
110 lines (104 loc) · 3.37 KB
/
algorithms-left.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3
def bin_search(S, g, is_censored):
"""Perform a binary search over g and return the index of the rightmost
character of the rightmost component of the keyword combination whose
rightmost component is leftmost in g.
:param S: set of strings to include with test messages
:param g: str
:return: index of the rightmost character of the rightmost component of the
keyword combination whose rightmost component is leftmost in g
"""
lo, hi = 0, len(g)
while hi - lo > 1:
mid = (lo + hi) // 2
if is_censored({g[:mid]}.union(S)):
hi = mid
else:
lo = mid
return hi
def bisect_left(S, g, after, is_censored):
"""Perform a binary search over g and return the index of the leftmost
character of the leftmost component of the keyword combination whose
leftmost component is rightmost in g.
:param S: set of strings to include with test messages
:param g: str
:param before: append 'after' to tested slices of g
:return: index of the leftmost character of the leftmost component of the
keyword combination whose leftmost component is rightmost in g
"""
lo, hi = 0, len(g)
while hi - lo > 1:
mid = (lo + hi) // 2
if not is_censored({g[mid:] + after}.union(S)):
hi = mid
else:
lo = mid
return lo
def comp_aware_bin_split(s, is_censored):
"""Isolates a censored keyword combination. If more than one censored
keyword combination is present, it isolate the one whose rightmost
component is leftmost in g.
:return: censored keyword combination
"""
C = set()
j = len(s)
while True:
i = bin_search(C, s, is_censored)
j = min(i - 1, j - 1)
while j > 0:
if is_censored({s[:i-1], s[j:i]}.union(C)):
break
else:
j = j - 1
C = {s[j:i]}.union(C)
if j > 0:
s = s[:i-1]
else:
s = ""
if not s or is_censored(C):
break
return C
def comp_aware_bin_split_2(s, is_censored):
"""Modified version of comp_aware_bin_split which uses a binary search
rather than linear search to identify ends of components.
"""
C = set()
j = len(s)
while True:
i = bin_search(C, s, is_censored)
diff = 1
j = min(i - 1, j - 1)
while j > 0:
if is_censored({s[:i-1], s[j:i]}.union(C)):
break
else:
j -= diff
diff *= 2
diff //= 2
s_1 = s[:i]
k = max(j, 0)
j = k + bisect_left({s_1[:-1]}.union(C),
s_1[k:j+diff],
s_1[j+diff:],
is_censored)
C = {s[j:i]}.union(C)
if j > 0:
s = s[:i-1]
else:
s = ""
if not s or is_censored(C):
break
return C
def main():
from simulator import Simulator
sim = Simulator()
def is_censored(test):
separator = '\x00' # will be platform specific
return sim.send(separator.join(test))
for art in sim.get_articles():
kw = comp_aware_bin_split(art, is_censored)
sim.report_found_keyword(kw)
print(sim.query_log[sim.this_article])
print(sim.queries / len(sim.articles))
if __name__ == "__main__":
main()