-
Notifications
You must be signed in to change notification settings - Fork 4
/
slu_preprocess.py
241 lines (225 loc) · 10.1 KB
/
slu_preprocess.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
import numpy as np
import random
from collections import defaultdict
class slu_data():
def __init__(self):
train_nl = open('Data/train/seq.in', 'r')
valid_nl = open('Data/valid/seq.in', 'r')
test_nl = open('Data/test/seq.in', 'r')
train_intent = open('Data/train/intent', 'r')
valid_intent = open('Data/valid/intent', 'r')
test_intent = open('Data/test/intent', 'r')
train_talker = open('Data/train/talker', 'r')
train_info = open('Data/train/info', 'r')
test_info = open('Data/test/info', 'r')
self.train_dist = self.read_info(train_info)
self.test_dist = self.read_info(test_info)
self.intent_act_dict = None
self.intent_attri_dict = None
self.total_intent = None
self.total_word = None
self.train_tourist_indices = list()
self.train_guide_indices = list()
self.train_guide_indices = list()
self.get_talker(train_talker)
self.train_intent = self.convertintent2id(train_intent)
self.valid_intent = self.convertintent2id(valid_intent)
self.test_intent = self.convertintent2id(test_intent)
glove = open('/home/james/joint-model/GloVe/glove.6B.200d.txt', 'r')
self.word2id = defaultdict()
self.id2word = defaultdict()
self.embedding_matrix = None
self.read_GloVe(glove)
self.train_data = self.convertnl2id(train_nl)
self.valid_data = self.convertnl2id(valid_nl)
self.test_data = self.convertnl2id(test_nl)
assert len(self.train_data) == len(self.train_intent)
assert len(self.valid_data) == len(self.valid_intent)
assert len(self.test_data) == len(self.test_intent)
#print 'train data size:', len(self.train_data)
#print 'valid data size:', len(self.valid_data)
#print 'test data size:', len(self.test_data)
self.train_batch_indices = [i for i in range(len(self.train_data))]
self.valid_batch_indices = [i for i in range(len(self.valid_data))]
self.test_indices = [i for i in range(len(self.test_data))] # no shuffle
def read_info(self, data_file):
ret_dist = list()
for line in data_file:
dist = line.split("***next***")[:-1]
ret_dist.append(dist)
return ret_dist
def get_talker(self, data_file):
for idx, line in enumerate(data_file):
talker = line.strip('\n')
if talker == 'Tourist':
self.train_tourist_indices.append(idx)
elif talker == 'Guide':
self.train_guide_indices.append(idx)
else:
print "cannot be here!"
exit(1)
def get_train_batch(self, batch_size, role=None):
""" returns a 3-dim list, where each row is a batch contains histories from tourist and guide"""
if role == None:
random.shuffle(self.train_batch_indices)
batch_indices = self.train_batch_indices[:batch_size]
elif role == 'Tourist':
random.shuffle(self.train_tourist_indices)
batch_indices = self.train_tourist_indices[:batch_size]
elif role == 'Guide':
random.shuffle(self.train_guide_indices)
batch_indices = self.train_guide_indices[:batch_size]
ret_nl_batch = list()
ret_intent_batch = list()
ret_dist_batch = list()
for batch_idx in batch_indices:
nl_sentences = self.train_data[batch_idx]
intent = self.train_intent[batch_idx]
ret_nl_batch.append(nl_sentences)
ret_intent_batch.append(intent)
dist = self.train_dist[batch_idx]
ret_dist_batch.append(dist)
return ret_nl_batch, ret_intent_batch, ret_dist_batch
def get_valid_batch(self, batch_size):
""" returns a 3-dim list, where each row is a batch contains histories from tourist and guide"""
random.shuffle(self.valid_batch_indices)
batch_indices = self.valid_batch_indices[:batch_size]
ret_nl_batch = list()
ret_intent_batch = list()
for batch_idx in batch_indices:
nl_sentences = self.valid_data[batch_idx]
intent = self.valid_intent[batch_idx]
ret_nl_batch.append(nl_sentences)
ret_intent_batch.append(intent)
return ret_nl_batch, ret_intent_batch
def get_test_batch(self):
""" returns a 3-dim list, where each row is a batch contains histories from tourist and guide"""
batch_indices = self.test_indices
ret_nl_batch = list()
ret_intent_batch = list()
ret_dist_batch = list()
for batch_idx in batch_indices:
nl_sentences = self.test_data[batch_idx]
intent = self.test_intent[batch_idx]
ret_nl_batch.append(nl_sentences)
ret_intent_batch.append(intent)
dist = self.test_dist[batch_idx]
ret_dist_batch.append(dist)
return ret_nl_batch, ret_intent_batch, ret_dist_batch
def convertintent2id(self, data_file):
intent_corpus = list()
for line in data_file:
temp_intent = line.strip('\n').split('***next***')[:-1]
temp_intent = map(lambda x:x.strip(' ').lstrip(' '), temp_intent)
intent_corpus.append(temp_intent)
if self.intent_act_dict is None or self.intent_attri_dict is None:
assert self.intent_act_dict is None and self.intent_attri_dict is None
# build intent dict
act_dict = defaultdict()
attri_dict = defaultdict()
for intents in intent_corpus:
for intent in intents:
act_attri = intent.split('-')
act = act_attri[0] # a string
attributes = act_attri[1:] # a list, may contain several attributes
if act not in act_dict:
act_dict[act] = len(act_dict)
for attri in attributes:
if attri not in attri_dict:
attri_dict[attri] = len(attri_dict)
self.intent_act_dict = act_dict
self.intent_attri_dict = attri_dict
self.total_intent = len(act_dict)+len(attri_dict)
# convert act and attributes to id
ret_intent = list()
for intents in intent_corpus:
temp_list = list()
for intent in intents:
act_attri = intent.split('-')
act = act_attri[0] # a string
attributes = act_attri[1:] # a list, may contain several attributes
t = (self.intent_act_dict[act], [self.intent_attri_dict[attri]+len(self.intent_act_dict) for attri in attributes])
temp_list.append(t)
ret_intent.append(temp_list)
return ret_intent
def read_GloVe(self, glove):
# read in GloVe dict
#print "Reading from GloVe..."
embedding_matrix = list()
word2id = defaultdict()
id2word = defaultdict()
for line in glove:
splitLine = line.strip('\n').split(' ')
word = splitLine[0]
word2id[word] = len(word2id)
id2word[word2id[word]] = word
embedding = [float(val) for val in splitLine[1:]]
embedding_matrix.append(embedding)
#print "Done.", len(embedding_matrix)," words loaded from GloVe!"
self.total_word = len(embedding_matrix)
self.word2id = word2id
self.id2word = id2word
self.embedding_matrix = embedding_matrix
def convertnl2id(self, data_file):
# nl_corpus is a list, where one row contains all the cleaned history nl strings list
nl_corpus = list()
for line in data_file:
temp_nl = line.strip('\n').split('***next***')[:-1] # temp_nl contains many sentences
nl = self.clean_nl(temp_nl)
nl_corpus.append(nl)
# start from idx 1, since 0 is for <unk>
data = list()
for nl_sentences in nl_corpus:
one_training_data = list()
for sentence in nl_sentences:
one_utterance = list()
for word in sentence.split(' '):
word_id = None
if word not in self.word2id:
word_id = len(self.word2id) - 1 # the last word in GloVe is <unk>
else:
word_id = self.word2id[word]
one_utterance.append(word_id)
one_training_data.append(one_utterance)
data.append(one_training_data)
return data
def clean_nl(self, temp_nl):
ret = list()
for sentence in temp_nl:
# remove some puntuation marks
temp = sentence.replace('~', '').strip(' ')
# restore abbreviations to their original forms
if '\'m' in temp:
temp = temp.replace('\'m', ' am')
if '\'re' in temp:
temp = temp.replace('\'re', ' are')
if '\'ll' in temp:
temp = temp.replace('\'ll', ' will')
if '\'s' in temp:
temp = temp.replace('\'s', ' is')
if '\'d' in temp:
temp = temp.replace('\'d', ' would')
if '\'ve' in temp:
temp = temp.replace('\'ve', ' have')
if 'don\'t' in temp:
temp = temp.replace('don\'t', 'do not')
if 'doesn\'t' in temp:
temp = temp.replace('doesn\'t', 'does not')
if 'hasn\'t' in temp:
temp = temp.replace('hasn\'t', 'has not')
if 'haven\'t' in temp:
temp = temp.replace('daven\'t', 'have not')
if 'wouldn\'t' in temp:
temp = temp.replace('wouldn\'t', 'would not')
# remove uh, um
if 'uh' in temp:
temp = temp.replace('uh', '')
if 'um' in temp:
temp = temp.replace('um', '')
if ' ' in temp:
temp = temp.replace(' ', ' ')
temp = temp.strip(' ').lstrip(' ')
ret.append(temp)
return ret
if __name__ == '__main__':
slu_data()