forked from SAP-samples/acl2019-commonsense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_processors.py
287 lines (228 loc) · 11.2 KB
/
data_processors.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
import xml.etree.ElementTree
import os
import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from functools import reduce
import numpy as np
tag_dict = {'POS': 0, 'NEG': 1}
class InputItem(object):
"""A single training/test example for simple sequence classification for applications like Winograd Challenge or PDP."""
def __init__(self, guid, text_a, do_lower = True, text_b=None, label=None, groundtruth=None, decoy=None, reference_idx=None, tag=None):
"""Constructs a InputItem.
Args:
guid: string
Unique id for the example.
text_a: string
The untokenized text of the first sequence. For single
sequence tasks, only this sequence must be specified.
do_lower: boolean
Lower case the text, default = True
text_b: (Optional) string.
The untokenized text of the second sequence.
Only must be specified for sequence pair tasks.
label: (Optional) string.
The label of the example.
groundtruth: (Optional) string
True answer word
decoy: (Optional) list
List of distractor words
reference_idx: (Optional) int
Index of the pronoun in the text_b, for WNLI it is 0
tag: (Optional) string
Indicating the class of the item, e.g. POS for positive or NEG for negative
"""
self.guid = guid
self.text_a = text_a.lower().strip()
self.text_b = text_b.lower().strip()
self.label = label
self.groundtruth = groundtruth
self.decoy = decoy
self.reference_idx = reference_idx
self.tag = tag
class DataProcessor(object):
"""Base class for data converters for sequence classification data sets."""
def get_train_items(self, data_dir):
"""Gets a collection of `InputItem`s for the train set."""
raise NotImplementedError()
def get_dev_examples(self, data_dir):
"""Gets a collection of `InputItem`s for the dev set."""
raise NotImplementedError()
def get_labels(self):
"""Gets the list of labels for this data set."""
raise NotImplementedError()
class XMLPDPProcessor(DataProcessor):
"""Processor for the MultiNLI data set (GLUE version)."""
def get_train_items(self, data_dir, select_type, do_lower=True):
"""See base class."""
# WSCollection
# PDPChallenge2016
assert(select_type=='ambigious' or select_type=='resolved'), "Select extraction type either >ambigious< or >resolved<."
return self._create_items( xml.etree.ElementTree.parse(os.path.join(data_dir, "PDPChallenge2016.xml")).getroot(), select_type, do_lower )
def get_labels(self):
"""See base class."""
return ["0", "1"]
def _create_items(self, xml_data, select_type, do_lower):
sentences = []
sent1 = []
sent2 = []
conjs = []
prons = []
choice0 = []
choice1 = []
examples = []
guid_dict = dict()
size = 0
count_pos = 0
count_neg = 0
question = 0
schema_count = 0
for schema in xml_data.findall('schema'):
schema_count+=1
replas = ('\n', ''),('\n', '')
replas2 = ('.', ''), ('.', ''), ('a ', ''), ('an ', ''), ('the ', ''), ('\'s','')
sent1.append(reduce(lambda a, kv: a.replace(*kv), replas, schema[0][0].text.lower().strip()))
sent2.append(reduce(lambda a, kv: a.replace(*kv), replas, schema[0][2].text.lower().strip()))
prons.append(reduce(lambda a, kv: a.replace(*kv), replas, schema[0][1].text.lower().strip()))
choices = []
choices_ = []
answers = len(schema[2])
for i in range(0,len(schema[2])):
choices.append(reduce(lambda a, kv: a.replace(*kv), replas,
schema[2][i].text.lower().strip()))
choices_.append(reduce(lambda a, kv: a.replace(*kv), replas2,
schema[2][i].text.lower().strip()))
ans = reduce(lambda a, kv: a.replace(*kv), replas2,
schema[3].text.lower().strip())
ans_dict = {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g':6}
decoys = []
for i in range(0,answers):
if not(ans_dict[ans] == i):
decoys.append(choices_[i])
if select_type == 'resolved':
for i in range(0,answers):
if ans_dict[ans] == i:
guid = (question)
label = '1'
text_a = sent1[size]
text_b = choices[i] + ' ' + sent2[size]
tag = tag_dict['POS']
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=choices_[i], tag=tag))
guid_dict[guid] = len(examples)-1
count_pos+=1
elif not(ans_dict[ans] == i):
guid = (question)
label = '0'
text_a = sent1[size]
text_b = choices[i] + ' ' + sent2[size]
tag = tag_dict['NEG']
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=choices_[ans_dict[ans]], tag=tag))
guid_dict[guid] = len(examples)-1
count_neg+=1
elif select_type == 'ambigious':
guid = (question)
label = '1'
text_a = sent1[size]
text_b = prons[size] + ' ' + sent2[size]
tag = -1
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=choices_[ans_dict[ans]], decoy=decoys, tag=tag, reference_idx=0))
guid_dict[guid] = len(examples)-1
size += 1
question += 1
if select_type == 'resolved':
print('Pos: '+str(count_pos) + ' Neg: '+str(count_neg)+ ' Sum: '+str(count_neg+count_pos))
return examples, guid_dict
class XMLMnliProcessor(DataProcessor):
"""Processor for the MultiNLI data set (GLUE version)."""
def get_train_items(self, data_dir, select_type, do_lower=True):
"""See base class."""
# WSCollection
# PDPChallenge2016
assert(select_type=='ambigious' or select_type=='resolved'), "Select extraction type either >ambigious< or >resolved<."
return self._create_items( xml.etree.ElementTree.parse(os.path.join(data_dir, "WSCollection.xml")).getroot(), select_type, do_lower )
def get_labels(self):
"""See base class."""
return ["0", "1"]
def _create_items(self, xml_data, select_type, do_lower):
sentences = []
sent1 = []
sent2 = []
conjs = []
prons = []
choice0 = []
choice1 = []
examples = []
guid_dict = dict()
size = 0
count_pos = 0
count_neg = 0
question = 0
for schema in xml_data.findall('schema'):
replas = (('\n', ''),('\n', ''))
replas2 = (('.', ''), ('.', ''),(' a ', ''), ('\n', ''), (' an ', ''), ('the ', ''))
sent1.append(schema[0][0].text.lower().strip())
sent2.append(schema[0][2].text.lower().strip())
prons.append(schema[0][1].text)
c0 = reduce(lambda a, kv: a.replace(*kv), replas,
schema[2][0].text.lower().strip())
c1 = reduce(lambda a, kv: a.replace(*kv), replas,
schema[2][1].text.lower().strip())
c0_ = reduce(lambda a, kv: a.replace(*kv), replas2,
schema[2][0].text.lower().strip())
c1_ = reduce(lambda a, kv: a.replace(*kv), replas2,
schema[2][1].text.lower().strip())
ans = reduce(lambda a, kv: a.replace(*kv), replas2,
schema[3].text.lower().strip())
if ans == 'a':
if select_type == 'resolved':
guid = (question)
label = '1'
text_a = sent1[size]
text_b = c0 + ' ' + sent2[size]
tag = tag_dict['POS']
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=c0_, tag=tag))
guid_dict[guid] = len(examples)-1
count_pos+=1
elif select_type == 'ambigious':
guid = (question)
label = '1'
text_a = sent1[size]
text_b = prons[size] + ' ' + sent2[size]
tag = -1
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=c0_, decoy=[c1_], tag=tag, reference_idx=0))
guid_dict[guid] = len(examples)-1
else:
if select_type == 'resolved':
guid = (question)
label = '1'
tag = tag_dict['POS']
text_a = sent1[size]
text_b = c1 + ' ' + sent2[size]
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=c1_, tag=tag))
guid_dict[guid] = len(examples)-1
count_pos+=1
elif select_type == 'ambigious':
guid = (question)
label = '1'
text_a = sent1[size]
text_b = prons[size] + ' ' + sent2[size]
tag = -1
examples.append(InputItem(guid=guid, text_a=text_a, text_b=text_b, label=label, groundtruth=c1_, decoy=[c0_], tag=tag, reference_idx=0))
guid_dict[guid] = len(examples)-1
if select_type == 'resolved':
is_noun = lambda pos: pos[:2] == 'NN'
tokenized = nltk.word_tokenize(text_a)
nouns = [word for (word, pos) in nltk.pos_tag(tokenized) if is_noun(pos)]
nouns = list(filter(lambda a: a not in nltk.word_tokenize(c0_+' '+c1_), nouns))
for k in range(0,len(nouns)):
guid = (question)
tag =tag_dict['NEG']
tmp = nouns[k] + ' ' + sent2[size]
examples.append(InputItem(guid=guid, text_a=text_a, text_b=tmp, label='0', tag=tag))
guid_dict[guid] = len(examples)-1
count_neg +=1
size += 1
question += 1
if select_type == 'resolved':
print('Pos: '+str(count_pos) + ' Neg: '+str(count_neg)+ ' Sum: '+str(count_neg+count_pos))
return examples, guid_dict