-
Notifications
You must be signed in to change notification settings - Fork 69
/
dm.py
320 lines (247 loc) · 12.6 KB
/
dm.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import multiprocessing
import select
import time
import random
import urllib2
import json
from collections import deque
from alex.components.slu.da import DialogueAct, DialogueActItem, DialogueActConfusionNetwork
from alex.components.hub.messages import Command, SLUHyp, DMDA
from alex.components.dm.common import dm_factory, get_dm_type
from alex.components.dm.exceptions import DMException
from alex.utils.procname import set_proc_name
class DM(multiprocessing.Process):
"""DM accepts N-best list hypothesis or a confusion network generated by an SLU component.
The result of this component is an output dialogue act.
When the component receives an SLU hypothesis then it immediately responds with an dialogue act.
This component is a wrapper around multiple dialogue managers which handles multiprocessing
communication.
"""
def __init__(self, cfg, commands, slu_hypotheses_in, dialogue_act_out, close_event):
multiprocessing.Process.__init__(self)
self.cfg = cfg
self.commands = commands
self.slu_hypotheses_in = slu_hypotheses_in
self.dialogue_act_out = dialogue_act_out
self.close_event = close_event
self.last_user_da_time = time.time()
self.last_user_diff_time = time.time()
self.epilogue_state = None
dm_type = get_dm_type(cfg)
self.dm = dm_factory(dm_type, cfg)
self.dm.new_dialogue()
self.codes = deque(["%04d" % i for i in range(0, 10000)])
# random.seed(self.cfg['DM']['epilogue']['code_seed'])
random.shuffle(self.codes)
self.test_code_server_connection()
def process_pending_commands(self):
"""Process all pending commands.
Available commands:
stop() - stop processing and exit the process
flush() - flush input buffers.
Now it only flushes the input connection.
Return True if the process should terminate.
"""
while self.commands.poll():
command = self.commands.recv()
if self.cfg['DM']['debug']:
self.cfg['Logging']['system_logger'].debug(command)
if isinstance(command, Command):
if command.parsed['__name__'] == 'stop':
return True
if command.parsed['__name__'] == 'flush':
# discard all data in in input buffers
while self.slu_hypotheses_in.poll():
data_in = self.slu_hypotheses_in.recv()
self.dm.end_dialogue()
self.commands.send(Command("flushed()", 'DM', 'HUB'))
return False
if command.parsed['__name__'] == 'prepare_new_dialogue':
self.dm.new_dialogue()
if command.parsed['__name__'] == 'new_dialogue':
self.epilogue_state = None
self.cfg['Logging']['session_logger'].turn("system")
self.dm.log_state()
# I should generate the first DM output
da = self.dm.da_out()
if self.cfg['DM']['debug']:
s = []
s.append("DM Output")
s.append("-"*60)
s.append(unicode(da))
s.append("")
s = '\n'.join(s)
self.cfg['Logging']['system_logger'].debug(s)
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
return False
if command.parsed['__name__'] == 'end_dialogue':
self.dm.end_dialogue()
return False
if command.parsed['__name__'] == 'timeout':
# check whether there is a looong silence
# if yes then inform the DM
silence_time = command.parsed['silence_time']
cn = DialogueActConfusionNetwork()
cn.add(1.0, DialogueActItem('silence','time', silence_time))
# process the input DA
self.dm.da_in(cn)
self.cfg['Logging']['session_logger'].turn("system")
self.dm.log_state()
if self.epilogue_state and float(silence_time) > 5.0:
# a user was silent for too long, therefore hung up
self.cfg['Logging']['session_logger'].dialogue_act("system", self.epilogue_da)
self.commands.send(DMDA(self.epilogue_da, 'DM', 'HUB'))
self.commands.send(Command('hangup()', 'DM', 'HUB'))
else:
da = self.dm.da_out()
if self.cfg['DM']['debug']:
s = []
s.append("DM Output")
s.append("-"*60)
s.append(unicode(da))
s.append("")
s = '\n'.join(s)
self.cfg['Logging']['system_logger'].debug(s)
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
if da.has_dat("bye"):
self.commands.send(Command('hangup()', 'DM', 'HUB'))
return False
return False
def epilogue_final_question(self):
da = DialogueAct('say(text="{text}")'.format(text=self.cfg['DM']['epilogue']['final_question']))
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
def epilogue_final_apology(self):
# apology for not reaching minimum number of turns
text = self.cfg['DM']['epilogue']['final_code_text_min_turn_count_not_reached']
da = DialogueAct('say(text="{text}")'.format(text=text))
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
def epilogue_final_code(self):
data = None
attempts = 0
url_template = self.cfg['DM']['epilogue']['final_code_url']
system_logger = self.cfg['Logging']['system_logger']
# store a code on the server (try several times if not successful)
while attempts < 10 and not data or not data['response'] or data['response'] != 'success':
code = self.codes.popleft()
self.codes.append(code) # put the code back to the end of the queue for reuse
attempts += 1
# pull the URL
url = url_template.format(code=code, logdir=system_logger.get_session_dir_name())
data = urllib2.urlopen(url).read()
data = json.loads(data, encoding='UTF-8')
if attempts >= 10:
# This shouldn't happen
text = 'I am sorry. A valid code could not be generated'
else:
text = [c for c in code]
text = ", ".join(text)
text = self.cfg['DM']['epilogue']['final_code_text'].format(code=text)
text = [text, ] * 3
text = self.cfg['DM']['epilogue']['final_code_text_repeat'].join(text)
da = DialogueAct('say(text="{text}")'.format(text=text))
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
self.final_code_given = True
def epilogue(self):
""" Gives the user last information before hanging up.
:return the name of the activity or None
"""
if self.cfg['DM']['epilogue']['final_question']:
self.epilogue_final_question()
return 'final_question'
elif self.cfg['DM']['epilogue']['final_code_url']:
if self.dm.dialogue_state.turn_number < self.cfg['DM']['epilogue']['final_code_min_turn_count']:
self.epilogue_final_apology()
else:
self.epilogue_final_code()
return None
def read_slu_hypotheses_write_dialogue_act(self):
# read SLU hypothesis
if self.slu_hypotheses_in.poll():
# read SLU hypothesis
data_slu = self.slu_hypotheses_in.recv()
if self.epilogue_state:
# we have got another turn, now we can hang up.
self.cfg['Logging']['session_logger'].turn("system")
self.dm.log_state()
self.cfg['Logging']['session_logger'].dialogue_act("system", self.epilogue_da)
self.commands.send(DMDA(self.epilogue_da, 'DM', 'HUB'))
self.commands.send(Command('hangup()', 'DM', 'HUB'))
elif isinstance(data_slu, SLUHyp):
# reset measuring of the user silence
self.last_user_da_time = time.time()
self.last_user_diff_time = time.time()
# process the input DA
self.dm.da_in(data_slu.hyp, utterance=data_slu.asr_hyp)
self.cfg['Logging']['session_logger'].turn("system")
self.dm.log_state()
da = self.dm.da_out()
# do not communicate directly with the NLG, let the HUB decide
# to do work. The generation of the output must by synchronised with the input.
if da.has_dat("bye"):
self.epilogue_state = self.epilogue()
self.epilogue_da = da
if not self.epilogue_state:
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
self.commands.send(Command('hangup()', 'DM', 'HUB'))
else:
if self.cfg['DM']['debug']:
s = []
s.append("DM Output")
s.append("-"*60)
s.append(unicode(da))
s.append("")
s = '\n'.join(s)
self.cfg['Logging']['system_logger'].debug(s)
self.cfg['Logging']['session_logger'].dialogue_act("system", da)
self.commands.send(DMDA(da, 'DM', 'HUB'))
elif isinstance(data_slu, Command):
self.cfg['Logging']['system_logger'].info(data_slu)
else:
raise DMException('Unsupported input.')
def run(self):
try:
set_proc_name("Alex_DM")
self.cfg['Logging']['session_logger'].cancel_join_thread()
while 1:
# Check the close event.
if self.close_event.is_set():
print 'Received close event in: %s' % multiprocessing.current_process().name
return
select.select([self.commands, self.slu_hypotheses_in], [], [], 1)
s = (time.time(), time.clock())
# process all pending commands
if self.process_pending_commands():
return
# process the incoming SLU hypothesis
self.read_slu_hypotheses_write_dialogue_act()
# Print out the execution time if it took longer than the threshold.
d = (time.time() - s[0], time.clock() - s[1])
if d[0] > 0.200:
print "EXEC Time inner loop: DM t = {t:0.4f} c = {c:0.4f}\n".format(t=d[0], c=d[1])
except KeyboardInterrupt:
print 'KeyboardInterrupt exception in: %s' % multiprocessing.current_process().name
self.close_event.set()
return
except:
self.cfg['Logging']['system_logger'].exception('Uncaught exception in the DM process.')
self.close_event.set()
raise
print 'Exiting: %s. Setting close event' % multiprocessing.current_process().name
self.close_event.set()
def test_code_server_connection(self):
""" this opens a test connection to our code server, content of the response is not important
if our server is down this call will fail and the VM will crash. this is more sensible to CF people,
otherwise CF contributor would do the job without getting paid.
"""
if self.cfg['DM']['epilogue']['final_question'] is None and self.cfg['DM']['epilogue']['final_code_url'] is not None:
url = self.cfg['DM']['epilogue']['final_code_url'].format(code='test', logdir='')
urllib2.urlopen(url)