-
Notifications
You must be signed in to change notification settings - Fork 2
/
prepare_data.py
165 lines (136 loc) · 4.75 KB
/
prepare_data.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
import json
def get_split_sentence(raw_data_file, split_data_file):
sentence = []
with open(raw_data_file, 'r', encoding='utf-8') as f:
split_word = []
for line in f.readlines():
item = line.strip().split()
if len(item) == 10:
word = item[1]
split_word.append(word)
else:
sentence.append(split_word)
split_word = []
f.close()
with open(split_data_file, 'a', encoding='utf-8') as fs:
for s in sentence:
new_s = ' '.join(s)
fs.writelines(new_s)
fs.writelines('\n')
fs.close()
def get_data(raw_data_file, new_data_file):
sentence = []
with open(raw_data_file, 'r', encoding='utf-8') as f:
word, pos, head, label = [], [], [], []
for line in f:
item = line.strip().split()
if len(item) == 10:
word.append(item[1])
pos.append(item[4])
head.append(int(item[6]))
label.append(item[7])
else:
sentence.append({
'word': word,
'pos': pos,
'head': head,
'label': label
})
word, pos, head, label = [], [], [], []
f.close()
with open(new_data_file, 'a', encoding='utf-8') as fs:
for s in sentence:
json.dump(s, fs, ensure_ascii=False)
fs.writelines('\n')
fs.close()
print(len(sentence))
def get_vocab(data_file, vocab_file):
words = [
"<UNK>", "<ROOT>", "<NULL>", "<P><UNK>", "<P><NULL>", "<P><ROOT>",
"<l><NULL>"
]
words_num, pos_num, label_num = 0, 0, 0
with open(data_file, 'r', encoding='utf-8') as f:
for line in f:
data = json.loads(line)
word = data["word"]
pos = data["pos"]
label = data["label"]
for w in word:
if w not in words:
words.append(w)
words_num += 1
for p in pos:
if "<p>" + p not in words:
words.append("<p>" + p)
pos_num += 1
for l in label:
if "<l>" + l not in words:
words.append("<l>" + l)
label_num += 1
f.close()
words2id = {j: i for i, j in enumerate(words)}
with open(vocab_file, 'w', encoding='utf-8') as fs:
fs.write(json.dumps(words2id, ensure_ascii=False, indent=4))
fs.close()
print("words size [%d]" % words_num)
print("pos size [%d]" % pos_num)
print("label size [%d]" % label_num)
'''
words size [34571]
pos size [31]
label size [11]
'''
def data2id(data_file, data2id_file):
with open("./raw_data/vocab.json", 'r', encoding='utf-8') as f:
vocab = json.load(f)
f.close()
data2id = []
with open(data_file, 'r', encoding='utf-8') as fs:
for line in fs:
data = json.loads(line)
word = [vocab["<ROOT>"]] + [
vocab[w] if w in vocab else vocab["<UNK>"]
for w in data["word"]
]
pos = [vocab["<P><ROOT>"]] + [
vocab["<p>" + p] if "<p>" + p in vocab else vocab["<P><UNK>"]
for p in data["pos"]
]
head = [-1] + data["head"]
label = [-1] + [
vocab["<l>" + l] if "<l>" + l in vocab else -1
for l in data["label"]
]
data2id.append({
"word": word,
"pos": pos,
"head": head,
"label": label
})
fs.close()
with open(data2id_file, 'a', encoding="utf-8") as fw:
for item in data2id:
json.dump(item, fw, ensure_ascii=False)
fw.write("\n")
fw.close()
if __name__ == '__main__':
raw_data_file = './data/train.conll'
split_data_file = './data/split_sentence.txt'
#get_split_sentence(raw_data_file,split_data_file)
new_train_data_file = './raw_data/train.json'
#get_data(raw_data_file,new_train_data_file)
vocab_file = './raw_data/vocab.json'
#get_vocab(new_train_data_file,vocab_file)
train_data2id_file = "./data/train.json"
#data2id(new_train_data_file,train_data2id_file)
raw_dev_file = './raw_data/dev.conll'
new_dev_file = './raw_data/dev.json'
#get_data(raw_dev_file, new_dev_file)
raw_test_file = './raw_data/test.conll'
new_test_file = './raw_data/test.json'
#get_data(raw_test_file, new_test_file)
data2id_dev_file = './data/dev.json'
data2id_test_file = './data/test.json'
#data2id(new_dev_file, data2id_dev_file)
data2id(new_test_file, data2id_test_file)