-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_gen.py
223 lines (191 loc) · 8.49 KB
/
data_gen.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
import copy
import json
def get_spos(dic_single, spo_list, text_count):
dic_single_copy = copy.deepcopy(dic_single)
for spo in spo_list:
h = spo['h']
t = spo['t']
relation = spo['relation']
arr_h = []
arr_h.append(h['pos'][0])
arr_h.append(h['pos'][1])
arr_h.append(h['name'])
arr_t = []
arr_t.append(t['pos'][0])
arr_t.append(t['pos'][1])
arr_t.append(t['name'])
arr_spo = []
arr_spo.append(arr_h)
arr_spo.append(relation)
arr_spo.append(arr_t)
dic_single_copy['spos'].append((arr_spo))
spos_new = sorted(dic_single_copy['spos'], key=lambda x: x[0])
spos_new = sorted(spos_new, key=lambda x: x[2])
spos_new1 = []
s_idx = 0
e_idx = 0
for s in text_count[:-1]:
s_idx+=len(s)
for e in text_count:
e_idx += len(e)
# print(spos_new)
for spo in spos_new:
# print(spo)
if spo[0][0] >= s_idx and spo[-1][1] <= e_idx:
spo[0][0] = spo[0][0] - s_idx
spo[0][1] = spo[0][1] - s_idx
spo[2][0] = spo[2][0] - s_idx
spo[2][1] = spo[2][1] - s_idx
spos_new1.append(spo)
else:
continue
# print(spos_new1)
# exit()
return spos_new1
def train_generator(file_train_bdci, file_train):
with open(file_train_bdci, 'r', encoding='utf-8') as f:
lines = f.readlines()
result_arr = []
with open(file_train, 'w', encoding='utf-8') as fw:
for line in lines:
line = line.strip()
if line == "":
continue
dic_single = {}
text_count = []
line = json.loads(line)
line_id = line['ID']
line_text = line['text']
spo_list = line['spo_list']
dic_single['id'] = line_id
dic_single['text'] = line_text
dic_single['spos'] = []
if line_text in result_arr:
continue
if len(line_text) > 200:
line_arr = []
text = ''
for l in range(len(line_text)):
text += line_text[l]
if line_text[l] in [',', '。', '!', '?', '、']:
line_arr.append(text)
text = ''
if l == len(line_text) - 1 and text != line_arr[-1]:
line_arr.append(text)
text_new = ''
n = 0
for i in range(len(line_arr)):
dic_single_new = {}
text_original = text_new
text_new += line_arr[i]
if len(text_new) <= 200:
if i == len(line_arr) - 1:
# id_new = line_id + '_' + str(n)
# out = {'id': id_new, 'text': text_new}
text_count.append(text_new)
spos_new1 = get_spos(dic_single, spo_list, text_count)
dic_single_new['id'] = line_id
dic_single_new['text'] = text_new
dic_single_new['spos'] = spos_new1
result_arr.append(dic_single_new)
else:
continue
else:
# id_new = line_id + '_' + str(n)
# out = {'id': id_new, 'text': text_original}
text_count.append(text_original)
spos_new1 = get_spos(dic_single, spo_list, text_count)
dic_single_new['id'] = line_id
dic_single_new['text'] = text_original
dic_single_new['spos'] = spos_new1
result_arr.append(dic_single_new)
text_new = line_arr[i]
n += 1
if i == len(line_arr) - 1:
# id_new = line_id + '_' + str(n)
# out = {'id': id_new, 'text': text_new}
text_count.append(text_new)
spos_new1 = get_spos(dic_single, spo_list, text_count)
dic_single_new['id'] = line_id
dic_single_new['text'] = text_new
dic_single_new['spos'] = spos_new1
result_arr.append(dic_single_new)
else:
for spo in spo_list:
h = spo['h']
t = spo['t']
relation = spo['relation']
arr_h = []
arr_h.append(h['pos'][0])
arr_h.append(h['pos'][1])
arr_h.append(h['name'])
arr_t = []
arr_t.append(t['pos'][0])
arr_t.append(t['pos'][1])
arr_t.append(t['name'])
arr_spo = []
arr_spo.append(arr_h)
arr_spo.append(relation)
arr_spo.append(arr_t)
dic_single['spos'].append(arr_spo)
result_arr.append(dic_single)
# print(result_arr[0])
# print('============')
# exit()
print('train:', len(result_arr))
result_json = json.dumps(result_arr, ensure_ascii=False, indent=2)
fw.write(result_json)
def test_generator(file_evalA, file_test):
with open(file_evalA, 'r', encoding='utf-8') as f:
lines = f.readlines()
result_arr = []
with open(file_test, 'w', encoding='utf-8') as fw:
for line in lines:
line = json.loads(line)
line_id = line['ID']
line_text = line['text']
if len(line_text) > 200:
line_arr = []
text = ''
for l in range(len(line_text)):
text+=line_text[l]
if line_text[l] in [',', '。', '!', '?', '、']:
line_arr.append(text)
text = ''
if l == len(line_text) - 1 and text != line_arr[-1]:
line_arr.append(text)
text_new = ''
n = 0
for i in range(len(line_arr)):
text_original = text_new
text_new+=line_arr[i]
if len(text_new) <= 200:
if i == len(line_arr) - 1:
id_new = line_id + '_' + str(n)
out = {'id': id_new, 'text': text_new}
result_arr.append(out)
else:
continue
else:
id_new = line_id + '_' + str(n)
out = {'id':id_new, 'text':text_original}
result_arr.append(out)
text_new = line_arr[i]
n+=1
if i == len(line_arr) - 1:
id_new = line_id + '_' + str(n)
out = {'id': id_new, 'text': text_new}
result_arr.append(out)
else:
out = {'id': line_id, 'text': line_text}
result_arr.append(out)
print('test:', len(result_arr))
result_json = json.dumps(result_arr, ensure_ascii=False, indent=2)
fw.write(result_json)
if __name__ == '__main__':
file_train_bdci = './data/train.json'
file_train = './data/train2.json'
file_evalA = './data/evalA.json'
file_test = './data/evalA2.json'
train_generator(file_train_bdci, file_train)
test_generator(file_evalA, file_test)