-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsegmentation.py
261 lines (211 loc) · 6.65 KB
/
segmentation.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# coding = utf-8
import utils
import json
import threading
from nltk.corpus import wordnet as wn
# 获取密码
def get_passwd(path):
passwd_list = []
with open(path, 'r') as info:
info_dicts = info.readlines()
for dict in info_dicts:
passwd = json.loads(dict)['passwordplaintext']
passwd_list.append(passwd)
return passwd_list
class Candidate():
def __init__(self,content, next):
self.content = content
self.next = next
# 0 表示未被遍历
self.flag = 0
def has_next(self):
if len(self.next) == 0:
return False
else:
return True
# 切分密码
def segment_passwd(passwd):
res = []
for i in range(len(passwd)):
for j in range(i + 1, len(passwd) - 1):
word = passwd[i:j]
if utils.is_word(word):
res.append(word)
if j < len(passwd):
res.append(segment_passwd(passwd[j:len(passwd)]))
else:
return res
else:
continue
return res
def generate_candidate(result):
candidate_set = []
for i in range(0,result.__len__(),2):
content = result[i]
next_set = generate_candidate(result[i+1])
candidate = Candidate(content,next_set)
candidate_set.append(candidate)
return candidate_set
def get_all_candidate(candidate):
candidate_list = []
item = []
item.append(candidate)
flag = True
while flag:
flag = False
if candidate.has_next():
next = candidate.next
for i in next:
if i.flag == 0:
candidate = i
item.append(candidate)
# print(candidate.content)
candidate.flag = 1
flag = True
break
if not flag:
item.pop()
if item.__len__() != 0:
candidate = item.pop()
item.append(candidate)
flag = True
else:
res = []
for i in item:
res.append(i)
candidate_list.append(res)
item.pop()
if item.__len__() != 0:
candidate = item[item.__len__()-1]
flag = True
return candidate_list
# As previously stated, it contains high order N-gram frequencies
# that can help us rank the segmentations by likelihood. Let KN
# be an N-gram corpus and f(KN ) the total frequency of N-grams in corpus K
# 这个词出现的频率/所有这个长度的词出现的频率
def best_ngram_score(segs):
score = 0
length = len(segs)
if length == 1:
score = uni_gram_probability(segs)
elif length == 2:
score = bi_gram_probability(segs)
elif length == 3:
score = tri_gram_probability(segs)
if score == 0:
for i in range(1,3):
a = best_ngram_score(segs[:i])
b = best_ngram_score(segs[i:])
tem_score = a*b
if tem_score > score:
score = tem_score
return score
def uni_gram_probability(segments):
print("d")
return 1
def bi_gram_probability(segments):
print("d")
return 2
def tri_gram_probability(segments):
print("d")
return 3
def parse_passwd(passwd):
# 最大覆盖长度
max_coverage_len = 0
# 最大覆盖率
max_coverage = 0
# 覆盖率最大的切割方式
max_coverage_candidates = []
res = segment_passwd(passwd)
candidate_set = generate_candidate(res)
# candidate_set = get_all_candidate(candidate_set[0])
for candidate in candidate_set:
candidate_list = get_all_candidate(candidate)
for item in candidate_list:
# 覆盖长度
coverage_len = 0
for i in item:
coverage_len = coverage_len + len(i.content)
if coverage_len >= max_coverage_len:
max_coverage_len = coverage_len
max_coverage = max_coverage_len/len(passwd)
# print(max_coverage_len)
# print(max_coverage)
candidate_set = generate_candidate(res)
for candidate in candidate_set:
candidate_list = get_all_candidate(candidate)
for item in candidate_list:
# 覆盖长度
coverage_len = 0
for i in item:
coverage_len = coverage_len + len(i.content)
if coverage_len == max_coverage_len:
list = []
for i in item:
list.append(i.content)
max_coverage_candidates.append(list)
words = []
if max_coverage_len > 0:
words = max_coverage_candidates[0]
min_num = max_coverage_candidates[0].__len__()
for c in max_coverage_candidates:
num = c.__len__()
if num < min_num:
min_num = num
words = c
# print(words)
gaps = []
rest_passwd = [passwd]
for word in words:
tem = []
for p in rest_passwd:
tem += p.split(word)
rest_passwd = tem
for gap in rest_passwd:
if gap != '':
gaps.append(gap)
# print(gaps)
# 按位置进行排序
segs = words + gaps
segs_pos = []
# print(segs)
for seg in segs:
pos = passwd.find(seg)
segs_pos.append(pos)
sorted_segs = []
for seg in segs:
min_pos = min(segs_pos)
min_index = segs_pos.index(min_pos)
segs_pos[min_index] = len(passwd)
sorted_segs.append(segs[min_index])
# print(sorted_segs)
return (sorted_segs,words,gaps)
def run(passwd_list,file_name):
count = 0
with open("data/"+ file_name +".json",'w') as ps:
for passwd in passwd_list:
if count%100 == 0:
print(count)
count += 1
# print(passwd)
sorted_segs, words, gaps = parse_passwd(passwd)
dict = {'sorted_segs': sorted_segs, 'words':words, 'gaps': gaps}
# dict_list.append(dict)
# for dict in dict_list:
json.dump(dict, ps)
ps.write("\n")
if __name__ == "__main__":
dict_list = []
passwd_list = []
passwd_list = get_passwd("data/collection_32.json")
total = passwd_list.__len__()
# passwd = "any1one23barks98"
# passwd_list.append(passwd)
last = 48000
start = 0
while last >= 6000:
threading.Thread(target=run, args=[passwd_list[start:start+6000],"data"+str(start)]).start()
start += 6000
last -= 6000
if last > 0:
threading.Thread(target=run, args=[passwd_list[start:start + last], "data" + str(start)]).start()