/
mat_data.py
318 lines (256 loc) · 11.5 KB
/
mat_data.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
# -*- coding: utf-8 -*-
import os
import copy
import time
import pickle
import argparse
import json
import math
import numpy as np
import dataset_walker
cur_dir = os.path.dirname(os.path.abspath(__file__))
ontology_path = os.path.join(cur_dir, "config/ontology_dstc2.json")
vocab_path = os.path.join(cur_dir, 'vocab_matNN.dict')
# TODO !!! For consistency, this should be the ONLY place for loading ontology and vocab, and modifying them.
# !!! Other files should import these data from here.
vocab = pickle.load(open(vocab_path,'rb'))
vocab_path2 = os.path.join(cur_dir, 'vocab_actN.dict')
# TODO !!! For consistency, this should be the ONLY place for loading ontology and vocab, and modifying them.
# !!! Other files should import these data from here.
vocab1 = pickle.load(open(vocab_path2,'rb'))
ontologyDict = json.load(open(ontology_path, 'r'))
for key in ontologyDict[u'informable']:
ontologyDict[u'informable'][key].append('dontcare')
ontologyDict[u'informable'][key].append('none')
# TODO
#max_tag_index = 5
#for i in xrange(1, max_tag_index+1):
# ontologyDict['informable']['food'].append('#food%d#'%i)
# ontologyDict['informable']['name'].append('#name%d#'%i)
# ##################################
# TODO replace un-informable values with tags, for example:
# a machine act is "inform(addr='alibaba qijian dian')", then it is replaced as "inform(addr=<addr>)"
# default to [] to disable any such replacement
#replace_un_informable_slots = ['phone', 'postcode', 'addr']
replace_un_informable_slots = []
# 在对句子中的slotval值进行tagging处理时,不同slot的处理顺序
label_slot_order = ['food', 'pricerange', 'name', 'area']
def label2vec(labelDict, method, reqList):
'''
Parameters:
1. goal
2. method
3. requests
Return Value:
1. resIdx
'''
resIdx = list()
for slot in label_slot_order:
if slot in labelDict and labelDict[slot] in ontologyDict['informable'][slot]:
resIdx.append(ontologyDict['informable'][slot].index(labelDict[slot]))
else:
# the max index is for the special value: "none"
resIdx.append(len(ontologyDict['informable'][slot]))
resIdx.append(ontologyDict['method'].index(method))
reqVec = [0.0] * len(ontologyDict['requestable'])
for req in reqList:
reqVec[ontologyDict['requestable'].index(req)] = 1
resIdx.append(reqVec)
return resIdx
def genTurnData_nbest(turn, labelJson):
turnData = dict()
# process user_input : exp scores
user_input = turn["input"]["live"]["asr-hyps"]
ssum=0.
for asr_pair in user_input:
ssum+= math.exp(float(asr_pair['score']))
for asr_pair in user_input:
asr_pair['score'] = math.exp(float(asr_pair['score']))/ssum
# process machine_output : replace un-informable value with tags
machine_output = turn["output"]["dialog-acts"]
for slot in replace_un_informable_slots :
for act in machine_output:
for pair in act["slots"]:
if len(pair) >= 2 and pair[0] == slot:
pair[1] = '<%s>' % slot
# generate labelIdx
labelIdx = label2vec(labelJson['goal-labels'], labelJson['method-label'], labelJson['requested-slots'])
turnData["user_input"] = user_input
turnData["machine_output"] = machine_output
turnData["labelIdx"] = labelIdx
return turnData
###############################
def tagTurnData(turnData, ontology):
"""将一个turn的数据进行tag替换"""
tagged_turnData = copy.deepcopy(turnData)
tag_dict = {}
for slot in ["food", "name"]:
val_ind = 1
for slot_val in ontology["informable"][slot]:
if slot_val.startswith("#%s"%slot):
continue
cur_tag = "#%s%d#" % (slot, val_ind)
replace_flag = False
# process user_input
for i in xrange(len(tagged_turnData["user_input"])):
sentence = tagged_turnData["user_input"][i]['asr-hyp']
tag_sentence = sentence.replace(slot_val, cur_tag)
if tag_sentence != sentence:
tagged_turnData["user_input"][i]['asr-hyp'] = tag_sentence
tag_dict[cur_tag] = slot_val
replace_flag = True
# process machine_output
for act in tagged_turnData["machine_output"]:
for pair in act["slots"]:
if len(pair) >= 2 and pair[0] == slot and pair[1] == slot_val:
pair[1] = cur_tag
tag_dict[cur_tag] = slot_val
replace_flag = True
if replace_flag:
val_ind += 1
if val_ind > max_tag_index:
break
# process labelIdx
val_ind_dict = {ontology["informable"][slot].index(v):ontology["informable"][slot].index(k)
for k, v in tag_dict.items() if k.startswith("#%s"%slot)}
labelIdx_ind = label_slot_order.index(slot)
labelIdx = tagged_turnData["labelIdx"][labelIdx_ind]
if labelIdx in val_ind_dict:
tagged_turnData["labelIdx"][labelIdx_ind] = val_ind_dict[labelIdx]
# add tag_dict to tagged_turnData
tagged_turnData["tag_dict"] = tag_dict
return tagged_turnData
###################################
def tagTurnData_matFS(turnData, ontology):
"""将一个turn的数据进行tag替换"""
tagged_turnData = copy.deepcopy(turnData)
tag_dict = {}
for slot in ["food", "name"]:
# val_ind = 1
for slot_val in ontology["informable"][slot]:
if slot_val.startswith("#%s"%slot):
continue
cur_tag = "#%s#" % (slot,)
replace_flag = False
# process user_input
for i in xrange(len(tagged_turnData["user_input"])):
sentence = tagged_turnData["user_input"][i]['asr-hyp']
tag_sentence = sentence.replace(slot_val, cur_tag)
if tag_sentence != sentence:
tagged_turnData["user_input"][i]['asr-hyp'] = tag_sentence
tag_dict[cur_tag] = slot_val
replace_flag = True
# process machine_output
for act in tagged_turnData["machine_output"]:
for pair in act["slots"]:
if len(pair) >= 2 and pair[0] == slot and pair[1] == slot_val:
pair[0] = "#slot#"
pair[1] = cur_tag
tag_dict[cur_tag] = slot_val
replace_flag = True
#if replace_flag:
# val_ind += 1
#if val_ind > max_tag_index:
# break
for i in xrange(len(tagged_turnData["user_input"])):
sentence = tagged_turnData["user_input"][i]['asr-hyp']
tag_sentence = sentence.replace(slot, "#slot#")
# # process labelIdx
# val_ind_dict = {ontology["informable"][slot].index(v):ontology["informable"][slot].index(k)
# for k, v in tag_dict.items() if k.startswith("#%s"%slot)}
# labelIdx_ind = label_slot_order.index(slot)
# labelIdx = tagged_turnData["labelIdx"][labelIdx_ind]
# if labelIdx in val_ind_dict:
# tagged_turnData["labelIdx"][labelIdx_ind] = val_ind_dict[labelIdx]
# # add tag_dict to tagged_turnData
# tagged_turnData["tag_dict"] = tag_dict
return tagged_turnData
def genTurnData_nbest_tagged(turn, labelJson):
turnData = genTurnData_nbest(turn, labelJson)
turnData = tagTurnData_matFS(turnData, ontologyDict)
return turnData
# def genTurnData_matFV(turn,labelJson):
# tagged_turnData =genTurnData_nbest(turn, labelJson)
# tag_dict = {}
# for slot in ["food", "name"]:
# val_ind = 1
# for slot_val in ontology["informable"][slot]:
# if slot_val.startswith("#%s"%slot):
# continue
# cur_tag = "#value%d#" % (val_ind)
# replace_flag = False
# # process user_input
# for i in xrange(len(tagged_turnData["user_input"])):
# sentence = tagged_turnData["user_input"][i]['asr-hyp']
# tag_sentence = sentence.replace(slot_val, cur_tag)
# if tag_sentence != sentence:
# tagged_turnData["user_input"][i]['asr-hyp'] = tag_sentence
# tag_dict[cur_tag] = slot_val
# replace_flag = True
# # process machine_output
# for act in tagged_turnData["machine_output"]:
# for pair in act["slots"]:
# if len(pair) >= 2 and pair[0] == slot and pair[1] == slot_val:
# pair[1] = cur_tag
# tag_dict[cur_tag] = slot_val
# replace_flag = True
# if replace_flag:
# val_ind += 1
# if val_ind > max_tag_index:
# break
# # process labelIdx
# val_ind_dict = {ontology["informable"][slot].index(v):ontology["informable"][slot].index(k)
# for k, v in tag_dict.items() if k.startswith("#%s"%slot)}
# labelIdx_ind = label_slot_order.index(slot)
# labelIdx = tagged_turnData["labelIdx"][labelIdx_ind]
# if labelIdx in val_ind_dict:
# tagged_turnData["labelIdx"][labelIdx_ind] = val_ind_dict[labelIdx]
# # add tag_dict to tagged_turnData
# tagged_turnData["tag_dict"] = tag_dict
def gen_resdata(dataset,output_type):
def gen_data(func_genTurnData):
data = []
for call in dataset:
fileData = dict()
fileData["session-id"] = call.log["session-id"]
fileData["turns"] = list()
#print {"session-id":call.log["session-id"]}
for turn, labelJson in call:
turnData = func_genTurnData(turn, labelJson)
fileData["turns"].append(turnData)
data.append(fileData)
return data
# different output type
if output_type == 'nbest':
res_data = gen_data(genTurnData_nbest)
elif output_type == 'nbest_tagged':
data = []
res_data1 = gen_data(genTurnData_nbest)
data.append(res_data1)
res_data2 = gen_data(genTurnData_nbest_tagged)
data.append(res_data2)
res_data = data
return res_data
# return tagged_turnData
# ##################################
def main():
parser = argparse.ArgumentParser(description='Simple hand-crafted dialog state tracker baseline.')
parser.add_argument('--dataset', dest='dataset', action='store', metavar='DATASET', required=True,
help='The dataset to analyze')
parser.add_argument('--dataroot',dest='dataroot',action='store',required=True,metavar='PATH',
help='Will look for corpus in <destroot>/<dataset>/...')
parser.add_argument('--output_type',dest='output_type',action='store',default='nbest',
help='the type of output json')
args = parser.parse_args()
dataset = dataset_walker.dataset_walker(args.dataset, dataroot=args.dataroot, labels=True)
res_data=gen_resdata(dataset,args.output_type)
# write to json file
file_prefix = args.dataset.split('_')[-1]
res_file = "%s_%s.json" % (file_prefix, args.output_type)
with open(res_file, "w") as fw:
fw.write(json.dumps(res_data, indent=2))
if __name__ == '__main__':
start_time = time.time()
main()
end_time = time.time()
print 'time: ', end_time - start_time, 's'