-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxEntTokernizor.py
416 lines (363 loc) · 13.7 KB
/
maxEntTokernizor.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# 基于最大熵模型及字标注的分词工具4-tag && 6-tag
import codecs
import sys
from maxent import MaxentModel
# 标注训练集6-tag
def tag6_training_set(training_file, tag_training_set_file):
fin = codecs.open(training_file, 'r', 'utf-8')
contents = fin.read()
contents = contents.replace(u'\r', u'')
contents = contents.replace(u'\n', u'')
words = contents.split(' ')
print len(words)
tag_words_list = []
i = 0
for word in words:
i += 1
if(i % 100 == 0):
tag_words_list.append(u'\r')
if(len(word) == 0):
continue
if(len(word) == 1):
tag_word = word + '/S'
elif (len(word) == 2):
tag_word = word[0] + '/B' + word[1] + '/E'
elif (len(word) == 3):
tag_word = word[0] + '/B' + word[1] + '/C' + word[2] + '/E'
elif (len(word) == 4):
tag_word = word[0] + '/B' + word[1] + '/C' + word[2] + '/D' + word[3] + '/E'
else:
tag_word = word[0] + '/B' + word[1] + '/C' + word[2] + '/D'
mid_words = word[3:-1]
for mid_word in mid_words:
tag_word += (mid_word + '/M')
tag_word += (word[-1] + '/E')
tag_words_list.append(tag_word)
tag_words = ''.join(tag_words_list)
fout = codecs.open(tag_training_set_file, 'w', 'utf-8')
fout.write(tag_words)
fout.close()
return (words, tag_words_list)
# 标注训练集4-tag
def tag4_training_set(training_file, tag_training_set_file):
fin = codecs.open(training_file, 'r', 'utf-8')
contents = fin.read()
contents = contents.replace(u'\r', u'')
contents = contents.replace(u'\n', u'')
words = contents.split(' ')
print len(words)
tag_words_list = []
i = 0
for word in words:
i += 1
if(i % 100 == 0):
tag_words_list.append(u'\r')
if(len(word) == 0):
continue
if(len(word) == 1):
tag_word = word + '/S'
elif (len(word) == 2):
tag_word = word[0] + '/B' + word[1] + '/E'
else:
tag_word = word[0] + '/B' + word[1] + '/C' + word[2] + '/D'
mid_words = word[1:-1]
for mid_word in mid_words:
tag_word += (mid_word + '/M')
tag_word += (word[-1] + '/E')
tag_words_list.append(tag_word)
tag_words = ''.join(tag_words_list)
fout = codecs.open(tag_training_set_file, 'w', 'utf-8')
fout.write(tag_words)
fout.close()
return (words, tag_words_list)
def get_near_char(contents, i, times):
words_len = len(contents) / times;
if (i < 0 or i > words_len - 1):
return '_'
else:
return contents[i*times]
def get_near_tag(contents, i ,times):
words_len = len(contents) / times;
if (i < 0 or i > words_len - 1):
return '_'
else:
return contents[i*times*2]
def isPu(char):
punctuation = [u',', u'。', u'?', u'!', u';', u'--', u'、', u'——', u'(', u')', u'《', u'》', u':', u'“', u'”', u'’', u'‘']
if char in punctuation:
return '1'
else:
return '0'
def get_class(char):
zh_num = [u'零', u'○', u'一', u'二', u'三', u'四', u'五', u'六', u'七', u'八', u'九', u'十', u'百', u'千', u'万']
ar_num = [u'0', u'1', u'2', u'3', u'4', u'5', u'6', u'7', u'8', u'9', u'.', u'0',u'1',u'2',u'3',u'4',u'5',u'6',u'7',u'8',u'9']
date = [u'日', u'年', u'月']
letter = ['a', 'b', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
if char in zh_num or char in ar_num:
return '1'
elif char in date:
return '2'
elif char in letter:
return '3'
else:
return '4'
# 获取训练集特征
def get_event(tag_file_path, event_file_path):
f = codecs.open(tag_file_path, 'r', 'utf-8')
contents = f.read()
contents = contents.replace(u'\r', u'')
contents = contents.replace(u'\n', u'')
words_len = len(contents)/3
event_list = []
index = range(0, words_len)
for i in index:
pre_char = get_near_char(contents, i-1, 3)
pre_pre_char = get_near_char(contents, i-2, 3)
cur_char = get_near_char(contents, i, 3)
next_char = get_near_char(contents, i+1, 3)
next_next_char = get_near_char(contents, i+2, 3)
event_list.append(
contents[i*3+2] + ' '
+ 'C-2='+pre_pre_char + ' ' + 'C-1='+pre_char + ' '
+ ' ' + 'C0=' + cur_char + ' '
+ 'C1=' + next_char + ' ' + 'C2=' + next_next_char + ' '
+ 'C-2=' + pre_pre_char + 'C-1=' + pre_char + ' '
+ 'C-1=' + pre_char + 'C0=' + cur_char + ' '
+ 'C0=' + cur_char + 'C1=' + next_char + ' '
+ 'C1=' + next_char + 'C2=' + next_next_char + ' '
+ 'C-1=' + pre_char + 'C1=' + next_char + ' '
+ 'C-2=' + pre_pre_char + 'C-1=' + pre_char + 'C0=' + cur_char + ' '
+ 'C-1=' + pre_char + 'C0=' + cur_char + 'C1=' + next_char + ' '
+ 'C0=' + cur_char + 'C1=' + next_char + 'C2=' + next_next_char + ' '
+ 'Pu=' + isPu(cur_char) + ' '
+ 'Tc-2=' + get_class(pre_pre_char) + 'Tc-1=' + get_class(pre_char)
+ 'Tc0=' + get_class(cur_char) + 'Tc1=' + get_class(next_char)
+ 'Tc2=' + get_class(next_next_char) + ' '
+ '\r')
# events = ''.join(event_list)
fout = codecs.open(event_file_path, 'w', 'utf-8')
for event in event_list:
fout.write(event)
fout.close()
return event_list
# 测试集生成特征
def get_feature(test_file_path, feature_file_path):
f = codecs.open(test_file_path, 'r', 'utf-8')
contents = f.read()
contents_list = contents.split('\r\n')
contents_list.remove('')
contents_list.remove('')
fout = codecs.open(feature_file_path, 'w', 'utf-8')
for line in contents_list:
words_len = len(line)
feature_list = []
index = range(0, words_len)
for i in index:
pre_char = get_near_char(line, i-1, 1)
pre_pre_char = get_near_char(line, i-2, 1)
cur_char = get_near_char(line, i, 1)
next_char = get_near_char(line, i+1, 1)
next_next_char = get_near_char(line, i+2, 1)
feature_list.append(
'C-2=' + pre_pre_char + ' ' + 'C-1=' + pre_char + ' '
+ 'C0=' + cur_char + ' '
+ 'C1=' + next_char + ' ' + 'C2=' + next_next_char + ' '
+ 'C-2=' + pre_pre_char + 'C-1=' + pre_char + ' '
+ 'C-1=' + pre_char + 'C0=' + cur_char + ' '
+ 'C0=' + cur_char + 'C1=' + next_char + ' '
+ 'C1=' + next_char + 'C2=' + next_next_char + ' '
+ 'C-1=' + pre_char + 'C1=' + next_char + ' '
+ 'C-2=' + pre_pre_char + 'C-1=' + pre_char + 'C0=' + cur_char + ' '
+ 'C-1=' + pre_char + 'C0=' + cur_char + 'C1=' + next_char + ' '
+ 'C0=' + cur_char + 'C1=' + next_char + 'C2=' + next_next_char + ' '
+ 'Pu=' + isPu(cur_char) + ' '
+ 'Tc-2=' + get_class(pre_pre_char) + 'Tc-1=' + get_class(pre_char)
+ 'Tc0=' + get_class(cur_char) + 'Tc1=' + get_class(next_char)
+ 'Tc2=' + get_class(next_next_char) + ' '
+ '\r')
for item in feature_list:
fout.write(item)
fout.write('split\r\n')
fout.close()
return feature_list
#
def split_by_blank(line):
line_list = []
line_len = len(line)
i = 0
while i < line_len:
line_list.append(line[i])
i += 2
return line_list
# 训练模型
def training(feature_file_path, trained_model_file, times):
m = MaxentModel()
fin = codecs.open(feature_file_path, 'r', 'utf-8')
all_list = []
m.begin_add_event()
for line in fin:
line = line.rstrip()
line_list = line.split(' ')
str_list = []
for item in line_list:
str_list.append(item.encode('utf-8'))
all_list.append(str_list)
m.add_event(str_list[1:], str_list[0], 1)
m.end_add_event()
print 'begin training'
m.train(times, "lbfgs")
print 'end training'
m.save(trained_model_file)
return all_list
#
def max_prob(label_prob_list):
max_prob = 0
max_prob_label = ''
for label_prob in label_prob_list:
if label_prob[1] > max_prob:
max_prob = label_prob[1]
max_prob_label = label_prob[0]
return max_prob_label
# 标注测试集
def tag_test(test_feature_file, trained_model_file, tag_test_set_file):
fin = codecs.open(test_feature_file, 'r', 'utf-8')
fout = codecs.open(tag_test_set_file, 'w', 'utf-8')
m = MaxentModel()
m.load(trained_model_file)
contents = fin.read()
feature_list = contents.split('\r')
feature_list.remove('\n')
for feature in feature_list:
if (feature == 'split'):
fout.write('\n\n\n')
continue
str_feature = []
u_feature = feature.split(' ')
for item in u_feature:
str_feature.append(item.encode('utf-8'))
label_prob_list = m.eval_all(str_feature)
label = max_prob(label_prob_list)
try:
new_tag = str_feature[2].split('=')[1] + '/' + label
except IndexError:
print str_feature
fout.write(new_tag.decode('utf-8'))
pre_tag = label
return feature_list
# 获取最终结果6-tag
def tag6_to_words(tag_training_set_file, result_file):
fin = codecs.open(tag_training_set_file, 'r', 'utf-8')
fout = codecs.open(result_file, 'w', 'utf-8')
contents = fin.read()
words_len = len(contents) / 3
result = []
i = 0
while (i < words_len):
cur_word_label = contents[i*3+2]
cur_word = contents[i*3]
if (cur_word_label == 'S'):
result.append(cur_word + ' ')
elif (cur_word_label == 'B'):
result.append(cur_word)
elif (cur_word_label == 'C'):
result.append(cur_word)
elif (cur_word_label == 'D'):
result.append(cur_word)
elif (cur_word_label == 'M'):
result.append(cur_word)
elif (cur_word_label == 'E'):
result.append(cur_word + ' ')
else:
result.append(cur_word)
i += 1
fout.write(''.join(result))
# 获取最终结果4-tag
def tag4_to_words(tag_training_set_file, result_file):
fin = codecs.open(tag_training_set_file, 'r', 'utf-8')
fout = codecs.open(result_file, 'w', 'utf-8')
contents = fin.read()
words_len = len(contents) / 3
result = []
i = 0
while (i < words_len):
cur_word_label = contents[i*3+2]
cur_word = contents[i*3]
if (cur_word_label == 'S'):
result.append(cur_word + ' ')
elif (cur_word_label == 'B'):
result.append(cur_word)
elif (cur_word_label == 'M'):
result.append(cur_word)
elif (cur_word_label == 'E'):
result.append(cur_word + ' ')
else:
result.append(cur_word)
i += 1
fout.write(''.join(result))
#
def main():
args = sys.argv[1:]
if len(args) < 3:
print 'Usage: python ' + sys.argv[0] + ' training_file test_file result_file'
exit(-1)
training_file = args[0]
test_file = args[1]
result_file = args[2]
# 标注训练集
tag_training_set_file = training_file + ".tag"
#tag4_training_set(training_file, tag_training_set_file)
tag6_training_set(training_file, tag_training_set_file)
print 'tag train set succeed'
# 获取训练集特征
feature_file_path = training_file + ".feature"
get_event(tag_training_set_file, feature_file_path)
print 'get training set feature succeed'
# 测试集生成特征
test_feature_file = test_file + ".feature"
get_feature(test_file, test_feature_file)
print 'get test set features succeed'
# 训练模型
times = [10]
for time in times:
trained_model_file = training_file + '.' + str(time) + ".model"
training(feature_file_path, trained_model_file, time)
print 'training model succeed: ' + str(time)
# 标注测试集
tag_test_set_file = test_file + ".tag"
tag_test(test_feature_file, trained_model_file, tag_test_set_file)
print 'tag test set succeed'
# 获取最终结果
#tag4_to_words(tag_test_set_file, result_file + '.' + str(time))
tag6_to_words(tag_test_set_file, result_file + '.' + str(time))
print 'get final result succeed ' + result_file + '.' + str(time)
'''
def main():
args = sys.argv[1:]
if len(args) < 3:
print 'Usage: python ' + sys.argv[0] + ' trainen_model_file test_file result_file'
exit(-1)
trained_model_file = args[0]
test_file = args[1]
result_file = args[2]
# 测试集生成特征
test_feature_file = test_file + ".feature"
get_feature(test_file, test_feature_file)
print 'get test set features succeed'
# 训练模型
times = [10]
for time in times:
#trained_model_file = training_file + '.' + str(time) + ".model"
#training(feature_file_path, trained_model_file, time)
#print 'training model succeed: ' + str(time)
# 标注测试集
tag_test_set_file = test_file + ".tag"
tag_test(test_feature_file, trained_model_file, tag_test_set_file)
print 'tag test set succeed'
# 获取最终结果
tag_to_words(tag_test_set_file, result_file + '.' + str(time))
print 'get final result succeed ' + result_file + '.' + str(time)
'''
if __name__ == "__main__":
main()