-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathprepare.py
374 lines (326 loc) · 9.61 KB
/
prepare.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python
# Script to prepare datasets
from xml.dom import minidom
import xml.etree.cElementTree as ET
from utilities import *
from keras.preprocessing import sequence
import numpy as np
from tqdm import tqdm
import string
import nltk
import os
import re
dataset = 'Restaurants'
mode = 'term'
train_sentences = []
test_sentences = []
aspectTerms = []
aspectCats = []
words = []
toy = False
one_hot = False
from string import punctuation
def strip_punctuation(s):
return s.translate(string.maketrans("",""), string.punctuation)
# return ''.join(c for c in s if c not in punctuation)
def process_text(x):
x = x.lower()
x = re.sub('[^A-Za-z0-9]+', ' ', x)
x = x.split(' ')
x = [strip_punctuation(y) for y in x]
# ptxt = nltk.word_tokenize(ptxt)
return x
# Parse Training File
tree = ET.ElementTree(file='./datasets/{}_Train.xml'.format(dataset))
for index, sentence in enumerate(tree.iter(tag='sentence')):
s = {}
for elem in sentence.iter():
if(elem.tag=='text'):
# ptxt = strip_punctuation(elem.text.lower())
# # ptxt = nltk.word_tokenize(ptxt)
# ptxt = ptxt.split(' ')
ptxt = process_text(elem.text)
s['text'] = ptxt
words += ptxt
elif(elem.tag=='aspectTerms' and mode=='term'):
s['aspectTerms'] = []
for at in elem.iter():
attr = at.attrib
if('term' not in attr):
continue
txt = process_text(at.attrib['term'])
print(txt)
words += txt
s['aspectTerms'].append([txt, at.attrib])
aspectTerms.append(txt)
elif(elem.tag=='aspectCategories' and mode=='aspect'):
s['aspectCats'] = []
for ac in elem.iter():
attr = ac.attrib
if('category' not in attr):
continue
s['aspectCats'].append([attr['category'],attr])
aspectCats.append(attr['category'])
train_sentences.append(s)
all_text = []
# Parse Testing File
tree = ET.ElementTree(file='./datasets/{}_Test.xml'.format(dataset))
for index, sentence in enumerate(tree.iter(tag='sentence')):
s = {}
for elem in sentence.iter():
if(elem.tag=='text'):
ptxt = process_text(elem.text)
all_text.append(ptxt)
s['text'] = ptxt
words += ptxt
elif(elem.tag=='aspectTerms' and mode=='term'):
s['aspectTerms'] = []
for at in elem.iter():
attr = at.attrib
if('term' not in attr):
continue
txt = process_text(at.attrib['term'])
print(txt)
words += txt
s['aspectTerms'].append([txt, at.attrib])
aspectTerms.append(txt)
elif(elem.tag=='aspectCategories' and mode=='aspect'):
s['aspectCats'] = []
for ac in elem.iter():
attr = ac.attrib
if('category' not in attr):
continue
s['aspectCats'].append([attr['category'],attr])
test_sentences.append(s)
# aspectTerms = list(set(aspectTerms))
aspectCats = list(set(aspectCats))
if(mode=='term'):
term_lens = [len(x) for x in aspectTerms]
# words += aspectTerms
words += aspectCats
words = list(set(words))
all_lens = [len(x) for x in all_text]
max_len, avg_len, min_len = np.max(all_lens), np.mean(all_lens),np.min(all_lens)
if(mode=='term'):
max_term_len = np.max(term_lens)
print("{} aspect terms".format(len(aspectTerms)))
print("{} max len for aspect terms".format(np.max(term_lens)))
if(mode=='aspect'):
print("{} aspect categories".format(len(aspectCats)))
print("{} unique words".format(len(words)))
print("{} train sentences".format(len(train_sentences)))
print("{} test sentences".format(len(test_sentences)))
print("max sent={} avg sent={} min sent={}".format(max_len, avg_len, min_len))
# Building vocab indices
index_word = {index+2:word for index,word in enumerate(words)}
word_index = {word:index+2 for index,word in enumerate(words)}
index_word[0], index_word[1] = '<pad>','<unk>'
word_index['<pad>'], word_index['unk'] = 0,1
if(mode=='aspect'):
index_cat = {index:word for index, word in enumerate(aspectCats)}
cat_index = {word:index for index, word in enumerate(aspectCats)}
# Avoid using 0 incase we want to pad zero vectors
sentiment_map = {
'positive':2,
'neutral':1,
'negative':0
}
def split_terms(tokens, terms):
''' Split tokens based on terms
Returns useful meta information regarding positions etc.
'''
if(len(terms)==1):
# split at term
term = terms[0]
if(term not in tokens):
return None
start_pos = tokens.index(term)
end_pos = start_pos
left = tokens[:end_pos+1]
right = tokens[start_pos:]
else:
start = terms[0]
end = terms[-1]
if(start not in tokens or end not in tokens):
print('====================')
print(tokens)
print([index_word[x] for x in tokens])
print([index_word[x] for x in terms])
return None
start_pos = tokens.index(start)
end_pos = tokens.index(end)
# Overlap the terms
left = tokens[:end_pos+1]
right = tokens[start_pos:]
return [[left, right], [start_pos, end_pos]]
def make_terms(txt, pair):
attr, value = pair[0],pair[1]
term_len = len(attr)
tokenized_terms = [word_index[x] for x in attr]
target_positions = split_terms(txt, tokenized_terms)
if(target_positions is None):
print("[Warning] Target not found in text!")
return None
left, right = target_positions[0][0], target_positions[0][1]
if(len(left)==0 or len(right)==0):
print("[Warning] left or right==0")
return None
# tokenized_terms = sequence.pad_sequences([tokenized_terms],maxlen=max_term_len)[0]
polarity = value['polarity']
if(polarity=='conflict'):
return None
polarity = sentiment_map[polarity]
if(one_hot==True):
vec = np.zeros(3)
vec[polarity] = 1
polarity = vec
tmp = {
'tokenized_txt':tokenized_txt,
'actual_len':actual_len,
'term_id':tokenized_terms,
'term_len':term_len,
'polarity':polarity,
'left':left,
'right':right,
'pointers':target_positions[1]
}
# tmp = [tokenized_txt, actual_len, tokenized_terms, term_len, polarity]
return tmp
def make_aspects(txt, pair):
attr, value = pair[0],pair[1]
attr_id = cat_index[attr]
polarity = value['polarity']
if(polarity=='conflict'):
return None
polarity = sentiment_map[polarity]
if(one_hot==True):
vec = np.zeros(3)
vec[polarity] = 1
polarity = vec
tmp = {
'tokenized_txt':tokenized_txt,
'actual_len':actual_len,
'aspect_id':[attr_id],
'aspect_len':1,
'polarity':polarity
}
# tmp = [tokenized_txt, actual_len, [attr_id], 0, polarity]
return tmp
# Converting to Model Friendly Format (Categories)
training_set, testing_set = [], []
if(toy==True):
print("Using TOY mode")
train_sentences = train_sentences[:500]
test_sentences = test_sentences[:100]
for sent in train_sentences:
txt = sent['text']
tokenized_txt = [word_index[x] for x in txt]
actual_len = len(tokenized_txt)
# tokenized_txt = sequence.pad_sequences([tokenized_txt],maxlen=max_len)[0]
if('aspectCats' in sent and mode=='aspect'):
for pair in sent['aspectCats']:
tmp = make_aspects(tokenized_txt, pair)
if(tmp is not None):
training_set.append(tmp)
training_set.append(tmp)
elif('aspectTerms' in sent and mode=='term'):
for pair in sent['aspectTerms']:
tmp = make_terms(tokenized_txt, pair)
if(tmp is not None):
training_set.append(tmp)
for sent in test_sentences:
txt = sent['text']
tokenized_txt = [word_index[x] for x in txt]
actual_len = len(tokenized_txt)
# tokenized_txt = sequence.pad_sequences([tokenized_txt],maxlen=max_len)[0]
if('aspectCats' in sent):
for pair in sent['aspectCats']:
tmp = make_aspects(txt, pair)
if(tmp is not None):
testing_set.append(tmp)
testing_set.append(tmp)
elif('aspectTerms' in sent):
for pair in sent['aspectTerms']:
tmp = make_terms(tokenized_txt, pair)
# print("Adding to test set")
if(tmp is not None):
testing_set.append(tmp)
print("Partioning into Dev Set")
import random
# random.shuffle(training_set)
# dev_set = training_set[:500]
dev_set = []
# training_set = training_set[500:]
env = {
'index_word':index_word,
'word_index':word_index,
'train':training_set,
'dev':dev_set,
'test':testing_set,
'max_len':max_len
}
if(mode=='aspect'):
env['index_cat'] = index_cat
env['cat_index'] = cat_index
elif(mode=='term'):
env['term_len'] = max_term_len
print("Training set={}".format(len(training_set))) # actual->3518
print("Development Set={}".format(len(dev_set)))
print("Testing set={}".format(len(testing_set))) # actual->973
glove = {}
import cPickle as pickle
dimensions = 300
glove_path = './embeddings/glove_{}_{}.pkl'.format(dataset,mode)
if(os.path.isfile(glove_path)):
print("Reusing glove dictionary to save time")
with open(glove_path,'r') as f:
glove = pickle.load(f)
save = False
else:
# Load word embeddings
with open('../glove_embeddings/glove.840B.{}d.txt'.format(dimensions),'r') as f:
lines = f.readlines()
for l in tqdm(lines):
vec = l.split(' ')
word = vec[0].lower()
vec = vec[1:]
#print(word)
#print(len(vec))
glove[word] = np.array(vec)
print('glove size={}'.format(len(glove)))
save = True
print("Finished making glove dictionary")
matrix = np.zeros((2, dimensions))
#print(matrix.shape)
oov = 0
filtered_glove = {}
for i in tqdm(range(2, len(word_index))):
word = index_word[i]
if(word in glove):
vec = glove[word]
if(save==True):
filtered_glove[word] = glove[word]
# print(vec.shape)
matrix = np.vstack((matrix,vec))
else:
random_init = np.random.uniform(low=-0.01,high=0.01, size=(1,dimensions))
# print(random_init)
matrix = np.vstack((matrix,random_init))
oov +=1
# print(word)
if(save==True):
with open(glove_path,'w+') as f:
pickle.dump(filtered_glove, f)
print("Saving glove dict to file")
print(matrix.shape)
print(len(word_index))
print("oov={}".format(oov))
print("Saving glove vectors")
env['glove'] = matrix
if(toy):
file_path = './store/{}_{}_toy.pkl'.format(dataset, mode)
else:
file_path = './store/{}_{}.pkl'.format(dataset, mode)
with open(file_path,'w+') as f:
pickle.dump(env, f)
# dictToFile(env,'./store/{}_Cat.json.gz'.format(dataset))