-
Notifications
You must be signed in to change notification settings - Fork 98
/
Copy pathextract_txt.py
84 lines (62 loc) · 1.61 KB
/
extract_txt.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
"""
把tmx(xml)的数据解开,分词,然后保存到data.pkl
"""
# import re
import sys
import pickle
sys.path.append('..')
def read_txt(path):
"""读取一个txt文件的NER标注数据"""
x_data, y_data = [], []
x, y = [], []
for line in open(path, 'r'):
line = line.strip()
line = line.split(' ')
if len(line) == 2:
x.append(line[0])
y.append(line[1])
else:
if x and y:
x_data.append(x)
y_data.append(y)
x, y = [], []
return x_data, y_data
def main(limit=100):
"""执行程序
Args:
limit: 只输出句子长度小于limit的句子
"""
from word_sequence import WordSequence
x_data, y_data = [], []
x, y = read_txt('train.txt')
x_data += x
y_data += y
x, y = read_txt('validation.txt')
x_data += x
y_data += y
x, y = read_txt('test.txt')
x_data += x
y_data += y
print(len(x_data))
print(x_data[:10])
print(y_data[:10])
print('tokenize')
data = list(zip(x_data, y_data))
data = [(x, y) for x, y in data if len(x) < limit and len(y) < limit]
x_data, y_data = zip(*data)
print(x_data[:10])
print(y_data[:10])
print(len(x_data), len(y_data))
print('fit word_sequence')
ws_input = WordSequence()
ws_target = WordSequence()
ws_input.fit(x_data, min_count=1)
ws_target.fit(y_data, min_count=1)
print('dump')
pickle.dump(
(x_data, y_data, ws_input, ws_target),
open('ner.pkl', 'wb')
)
print('done')
if __name__ == '__main__':
main()