-
Notifications
You must be signed in to change notification settings - Fork 1
/
coroutines.py
108 lines (100 loc) · 2.63 KB
/
coroutines.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
#!/usr/bin/env python3
def bin_search(S, g):
lo, hi = 0, len(g)
while hi - lo > 1:
mid = (lo + hi) // 2
was_censored = yield S.union({g[mid:]})
if was_censored:
lo = mid
else:
hi = mid
return lo
def bisect_right(S, g, before):
lo, hi = 0, len(g)
while hi - lo > 1:
mid = (lo + hi) // 2
was_censored = yield S.union({before + g[:mid]})
if not was_censored:
lo = mid
else:
hi = mid
return lo
def comp_aware_bin_split(s):
C = set()
j = 0
while True:
i = yield from bin_search(C, s)
j = max(i + 1, j)
k = len(s)
while j < k:
was_censored = yield C.union({s[i:j], s[i+1:]})
if was_censored:
k = j
else:
j = j + 1
C = C.union({s[i:j]})
if j != len(s):
s = s[i+1:]
else:
s = ''
if not s:
break
was_censored = yield C
if was_censored:
break
j -= i
return C
def comp_aware_bin_split_2(s):
C = set()
j = 0
while True:
i = yield from bin_search(C, s)
diff = 1
j = max(i + 1, j)
k = len(s)
while j < k:
was_censored = yield C.union({s[i:j], s[i+1:]})
if was_censored:
break
else:
j += diff
diff *= 2
diff //= 2
s_1, j = s[i:], j - i
j = i + 1 + (yield from bisect_right(C.union({s_1[1:]}),
s_1[j-diff:j],
s_1[:j-diff]))
C = C.union({s[i:j+diff]})
if j + diff != len(s):
s = s[i+1:]
else:
s = ""
if not s:
break
was_censored = yield C
if was_censored:
break
j -= i
return C
def isolate(isolator, sim):
was_censored = None
while True:
try:
test = isolator.send(was_censored)
except StopIteration as e:
return e.value
was_censored = is_censored(test, sim)
def is_censored(test, sim):
separator = '\x00' # will be platform specific
return sim.send(separator.join(test))
def main():
from simulator import Simulator
sim = Simulator()
for art in sim.get_articles():
isolator = comp_aware_bin_split(art)
kw = isolate(isolator, sim)
sim.report_found_keyword(kw)
print(sim.query_log[sim.this_article])
print(sim.queries / len(sim.articles))
if __name__ == "__main__":
main()