forked from twjiang/MIMO_CFE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_center.py
330 lines (278 loc) · 10.1 KB
/
data_center.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
import sys, os, io
import random
import torch
import logging
import argparse
class Instance(object):
"""docstring for Instance"""
def __init__(self, paper_id, stmt_id, multi_input, multi_output):
super(Instance, self).__init__()
self.paper_id = paper_id
self.stmt_id = stmt_id
self.multi_input = multi_input
self.multi_output = multi_output
self.SENTENCE = None
self.POSTAG = None
self.CAP = None
self.LM_SENTENCE = None
self.OUT = None
class DataCenter(object):
"""docstring for Instance"""
def __init__(self, train_file, eval_file):
super(DataCenter, self).__init__()
self.POS2ID, self.ID2POS = self.getTag2ID('./resources/PosTag2ID.txt')
self.CAP2ID, self.ID2CAP = self.getTag2ID('./resources/CAPTag2ID.txt')
self.Tag2ID_fact, self.ID2Tag_fact = self.getTag2ID('./resources/OutTag2ID_fact.txt')
self.Tag2ID_condition, self.ID2Tag_condition = self.getTag2ID('./resources/OutTag2ID_condition.txt')
self.Tag2Num = dict()
self.TRAIN_SENTENCEs = []
self.TRAIN_POSTAGs = []
self.TRAIN_CAPs = []
self.TRAIN_OUTs = []
self.ex_TRAIN_SENTENCEs = []
self.ex_TRAIN_POSTAGs = []
self.ex_TRAIN_CAPs = []
self.ex_TRAIN_OUTs = []
self.EVAL_SENTENCEs = []
self.EVAL_POSTAGs = []
self.EVAL_CAPs = []
self.EVAL_OUTs = []
self.max_outpus = 0
self.instance_TRAIN = []
self.instance_EVAL = []
self.instance_ex_TRAIN = []
self.paper_id_set_TRAIN = set()
self.paper_id_set_EVAL = set()
self.paper_id_set_ex_TRAIN = set()
self.loading_dataset(train_file, eval_file)
def getTag2ID(self, fileName):
tag2ID = dict()
ID2Tag = dict()
with open(fileName, 'r') as f:
for line in f:
tag, _id = line.strip().split(' ')
tag2ID[tag] = int(_id)
ID2Tag[int(_id)] = tag
return tag2ID, ID2Tag
def _add_instance(self, dataset_type, paper_id, stmt_id, multi_input, multi_output, attr_tuple):
if len(multi_input) == 0:
return
SENTENCEs, POSTAGs, CAPs, OUTs = attr_tuple
instance = Instance(paper_id, stmt_id, multi_input, multi_output)
senLen = len(instance.multi_input[0][-1])
# print(instance.multi_input[0][-1])
for _input in instance.multi_input:
seq_name = _input[0]
seq = _input[1]
if seq_name == 'WORD':
sentence = []
for word in seq:
sentence.append(word.lower())
assert len(sentence) == senLen
SENTENCEs.append(sentence)
instance.SENTENCE = seq
# formatted_anno_file.write('WORD:\t'+'\t'.join(sentence)+'\n')
elif seq_name == 'POSTAG':
assert len(seq) == senLen
POSTAGs.append(seq)
instance.POSTAG = seq
# formatted_anno_file.write('POSTAG:\t'+'\t'.join(seq)+'\n')
else:
assert len(seq) == senLen
CAPs.append(seq)
instance.CAP = seq
# formatted_anno_file.write('CAP:\t'+'\t'.join(seq)+'\n')
# print 'OUT:'
facts_out = [self.Tag2ID_fact['O']] * senLen
conditions_out = [self.Tag2ID_condition['O']] * senLen
predicate_fact = dict()
predicate_cond = dict()
for _output in instance.multi_output:
key = _output[0]
seq = _output[1]
if key.startswith('f'):
if 'B-f2P' in seq:
p_index = seq.index('B-f2P')
if p_index not in predicate_fact:
predicate_fact[p_index] = []
predicate_fact[p_index].append([self.Tag2ID_fact[tag] for tag in seq])
# print('F_G: ', seq)
else:
if -1 not in predicate_fact:
predicate_fact[-1] = []
# print('F_G: ', seq)
predicate_fact[-1].append([self.Tag2ID_fact[tag] for tag in seq])
else:
if 'B-c2P' in seq:
p_index = seq.index('B-c2P')
if p_index not in predicate_cond:
predicate_cond[p_index] = []
# print('C_G: ', seq)
predicate_cond[p_index].append([self.Tag2ID_condition[tag] for tag in seq])
else:
if -1 not in predicate_cond:
predicate_cond[-1] = []
# print('C_G: ', seq)
predicate_cond[-1].append([self.Tag2ID_condition[tag] for tag in seq])
for index in range(len(seq)):
tag = seq[index]
# print tag,
if key.startswith('f'):
if tag != 'O':
facts_out[index] = self.Tag2ID_fact[tag]
else:
if tag != 'O':
conditions_out[index] = self.Tag2ID_condition[tag]
for index in range(len(facts_out)):
tag_id_fact = facts_out[index]
tag_id_condition = conditions_out[index]
if dataset_type == 'TRAIN':
self.count_tag(self.ID2Tag_fact[tag_id_fact])
self.count_tag(self.ID2Tag_condition[tag_id_condition])
multi_facts = [facts_out, ]
multi_conds = [conditions_out, ]
# print([self.ID2Tag_fact[tag_id] for tag_id in facts_out])
# print([self.ID2Tag_condition[tag_id] for tag_id in conditions_out])
# print(predicate_fact.keys())
for p_index in sorted(predicate_fact):
facts_out = [self.Tag2ID_fact['O']] * senLen
for seq in predicate_fact[p_index]:
for index in range(len(seq)):
tag_id = seq[index]
if tag_id != 0:
facts_out[index] = tag_id
# print([self.ID2Tag_fact[tag_id] for tag_id in facts_out])
multi_facts.append(facts_out)
for p_index in sorted(predicate_cond):
conds_out = [self.Tag2ID_condition['O']] * senLen
for seq in predicate_cond[p_index]:
for index in range(len(seq)):
tag_id = seq[index]
if tag_id != 0:
conds_out[index] = tag_id
# print([self.ID2Tag_condition[tag_id] for tag_id in conds_out])
multi_conds.append(conds_out)
# print('===========================================================')
assert len(facts_out) == len(conditions_out) == senLen
# print(multi_facts)
# print(multi_conds)
outs = [multi_facts, multi_conds]
OUTs.append(outs)
instance.OUT = outs
assert len(SENTENCEs) == len(POSTAGs) ==len(CAPs) == len(OUTs)
if len(SENTENCEs) % 10000 == 0:
print(len(SENTENCEs), 'done')
instance_list = getattr(self, 'instance_'+dataset_type)
instance_list.append(instance)
def count_tag(self, tag):
if tag not in self.Tag2Num:
self.Tag2Num[tag] = 0
self.Tag2Num[tag] += 1
def _loading_dataset(self, dataset_type, dataFile):
SENTENCEs = getattr(self, dataset_type+'_SENTENCEs')
POSTAGs = getattr(self, dataset_type+'_POSTAGs')
CAPs = getattr(self, dataset_type+'_CAPs')
OUTs = getattr(self, dataset_type+'_OUTs')
instance_list = getattr(self, 'instance_'+dataset_type)
del SENTENCEs[:]
del POSTAGs[:]
del CAPs[:]
del OUTs[:]
del instance_list[:]
attr_tuple = (SENTENCEs, POSTAGs, CAPs, OUTs)
logging.debug('loading '+dataset_type+' data from '+dataFile)
paper_id_set = getattr(self, 'paper_id_set_'+dataset_type)
paper_id = 'none'
stmt_id = '0'
multi_input = []
multi_output = []
previous = False
# formatted_anno_file = open(dataFile.replace('.tsv', '_formatted.tsv'), 'w')
with open(dataFile, 'r') as fd:
for line in fd:
if line.startswith('=====') or line.startswith('#'):
# conclude the previous instance
if previous:
self._add_instance(dataset_type, paper_id, stmt_id, multi_input, multi_output, attr_tuple)
# start a new instance
if not line.startswith('====='):
continue
paper_id = line.strip().split('===== ')[-1].split(' stmt')[0]
paper_id_set.add(paper_id)
stmt_id = line.split('stmt')[-1].split(' =====')[0]
# logging.debug('doing the paper '+paper_id+', stmt '+stmt_id)
multi_input = []
multi_output = []
previous = True
# formatted_anno_file.write(line)
continue
line_list = line.strip('\n').split('\t')
seq_name = line_list[0]
seq = line_list[1:]
if seq_name in ['WORD', 'POSTAG', 'CAP']:
multi_input.append((seq_name, seq))
else:
multi_output.append((seq_name, seq))
# formatted_anno_file.close()
instance_list = getattr(self, 'instance_'+dataset_type)
print(len(SENTENCEs), len(POSTAGs), len(CAPs))
print('done.')
# def loading_dataset(self, trainFile, validFile, testFile):
def loading_dataset(self, trainFile, evalFile, exTrainFile=None):
if trainFile != None:
self._loading_dataset('TRAIN', trainFile)
if evalFile != None:
self._loading_dataset('EVAL', evalFile)
if exTrainFile != None:
self._loading_dataset('ex_TRAIN', exTrainFile)
def get_evaluation(self, valid_prop, dataset_type='EVAL'):
VALID_SENTENCEs = []
VALID_POSTAGs = []
VALID_CAPs = []
VALID_OUTs = []
VALID_instances = []
TEST_SENTENCEs = []
TEST_POSTAGs = []
TEST_CAPs = []
TEST_OUTs = []
TEST_instances = []
SENTENCEs = getattr(self, dataset_type+'_SENTENCEs')
POSTAGs = getattr(self, dataset_type+'_POSTAGs')
CAPs = getattr(self, dataset_type+'_CAPs')
OUTs = getattr(self, dataset_type+'_OUTs')
instance_list = getattr(self, 'instance_'+dataset_type)
#print range(len(self.EVAL_SENTENCEs))
id_list = random.sample(range(len(SENTENCEs)), int(len(SENTENCEs)*valid_prop))
#print id_list
for index in range(len(SENTENCEs)):
if index not in id_list:
TEST_SENTENCEs.append(SENTENCEs[index])
TEST_POSTAGs.append(POSTAGs[index])
TEST_CAPs.append(CAPs[index])
TEST_OUTs.append(OUTs[index])
TEST_instances.append(instance_list[index])
else:
VALID_SENTENCEs.append(SENTENCEs[index])
VALID_POSTAGs.append(POSTAGs[index])
VALID_CAPs.append(CAPs[index])
VALID_OUTs.append(OUTs[index])
VALID_instances.append(instance_list[index])
VALID_DATA = (VALID_SENTENCEs, VALID_POSTAGs, VALID_CAPs, VALID_OUTs, VALID_instances)
TEST_DATA = (TEST_SENTENCEs, TEST_POSTAGs, TEST_CAPs, TEST_OUTs, TEST_instances)
return VALID_DATA, TEST_DATA
def get_trainning_data(self, is_semi=False):
SENTENCEs = getattr(self, 'TRAIN_SENTENCEs')
POSTAGs = getattr(self, 'TRAIN_POSTAGs')
CAPs = getattr(self, 'TRAIN_CAPs')
OUTs = getattr(self, 'TRAIN_OUTs')
if is_semi:
ex_SENTENCEs = getattr(self, 'ex_TRAIN_SENTENCEs')
ex_POSTAGs = getattr(self, 'ex_TRAIN_POSTAGs')
ex_CAPs = getattr(self, 'ex_TRAIN_CAPs')
ex_OUTs = getattr(self, 'ex_TRAIN_OUTs')
SENTENCEs.extend(ex_SENTENCEs)
POSTAGs.extend(ex_POSTAGs)
CAPs.extend(ex_CAPs)
OUTs.extend(ex_OUTs)
print(len(SENTENCEs), len(POSTAGs), len(CAPs), len(OUTs))
return SENTENCEs, POSTAGs, CAPs, OUTs