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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import copy
|
2 |
+
import re
|
3 |
+
from zipfile import ZipFile, ZIP_DEFLATED
|
4 |
+
from shutil import copy2, rmtree
|
5 |
+
import json
|
6 |
+
import os
|
7 |
+
from tqdm import tqdm
|
8 |
+
from collections import Counter
|
9 |
+
from pprint import pprint
|
10 |
+
from nltk.tokenize import TreebankWordTokenizer, PunktSentenceTokenizer
|
11 |
+
|
12 |
+
ontology = {
|
13 |
+
"domains": { # descriptions are adapted from multiwoz22, but is_categorical may be different
|
14 |
+
"attraction": {
|
15 |
+
"description": "find an attraction",
|
16 |
+
"slots": {
|
17 |
+
"area": {
|
18 |
+
"description": "area to search for attractions",
|
19 |
+
"is_categorical": True,
|
20 |
+
"possible_values": [
|
21 |
+
"centre",
|
22 |
+
"east",
|
23 |
+
"north",
|
24 |
+
"south",
|
25 |
+
"west"
|
26 |
+
]
|
27 |
+
},
|
28 |
+
"name": {
|
29 |
+
"description": "name of the attraction",
|
30 |
+
"is_categorical": False,
|
31 |
+
"possible_values": []
|
32 |
+
},
|
33 |
+
"type": {
|
34 |
+
"description": "type of the attraction",
|
35 |
+
"is_categorical": True,
|
36 |
+
"possible_values": [
|
37 |
+
"architecture",
|
38 |
+
"boat",
|
39 |
+
"cinema",
|
40 |
+
"college",
|
41 |
+
"concerthall",
|
42 |
+
"entertainment",
|
43 |
+
"museum",
|
44 |
+
"multiple sports",
|
45 |
+
"nightclub",
|
46 |
+
"park",
|
47 |
+
"swimmingpool",
|
48 |
+
"theatre"
|
49 |
+
]
|
50 |
+
},
|
51 |
+
"entrance fee": {
|
52 |
+
"description": "how much is the entrance fee",
|
53 |
+
"is_categorical": False,
|
54 |
+
"possible_values": []
|
55 |
+
},
|
56 |
+
"open hours": {
|
57 |
+
"description": "open hours of the attraction",
|
58 |
+
"is_categorical": False,
|
59 |
+
"possible_values": []
|
60 |
+
},
|
61 |
+
"address": {
|
62 |
+
"description": "address of the attraction",
|
63 |
+
"is_categorical": False,
|
64 |
+
"possible_values": []
|
65 |
+
},
|
66 |
+
"phone": {
|
67 |
+
"description": "phone number of the attraction",
|
68 |
+
"is_categorical": False,
|
69 |
+
"possible_values": []
|
70 |
+
},
|
71 |
+
"postcode": {
|
72 |
+
"description": "postcode of the attraction",
|
73 |
+
"is_categorical": False,
|
74 |
+
"possible_values": []
|
75 |
+
},
|
76 |
+
"choice": {
|
77 |
+
"description": "number of attractions that meet the requirement",
|
78 |
+
"is_categorical": False,
|
79 |
+
"possible_values": []
|
80 |
+
}
|
81 |
+
}
|
82 |
+
},
|
83 |
+
"hotel": {
|
84 |
+
"description": "find and book a hotel",
|
85 |
+
"slots": {
|
86 |
+
"internet": {
|
87 |
+
"description": "whether the hotel has internet",
|
88 |
+
"is_categorical": True,
|
89 |
+
"possible_values": [
|
90 |
+
"free",
|
91 |
+
"no",
|
92 |
+
"yes"
|
93 |
+
]
|
94 |
+
},
|
95 |
+
"parking": {
|
96 |
+
"description": "whether the hotel has parking",
|
97 |
+
"is_categorical": True,
|
98 |
+
"possible_values": [
|
99 |
+
"free",
|
100 |
+
"no",
|
101 |
+
"yes"
|
102 |
+
]
|
103 |
+
},
|
104 |
+
"area": {
|
105 |
+
"description": "area or place of the hotel",
|
106 |
+
"is_categorical": True,
|
107 |
+
"possible_values": [
|
108 |
+
"centre",
|
109 |
+
"east",
|
110 |
+
"north",
|
111 |
+
"south",
|
112 |
+
"west"
|
113 |
+
]
|
114 |
+
},
|
115 |
+
"stars": {
|
116 |
+
"description": "star rating of the hotel",
|
117 |
+
"is_categorical": True,
|
118 |
+
"possible_values": [
|
119 |
+
"0",
|
120 |
+
"1",
|
121 |
+
"2",
|
122 |
+
"3",
|
123 |
+
"4",
|
124 |
+
"5"
|
125 |
+
]
|
126 |
+
},
|
127 |
+
"price range": {
|
128 |
+
"description": "price budget of the hotel",
|
129 |
+
"is_categorical": True,
|
130 |
+
"possible_values": [
|
131 |
+
"expensive",
|
132 |
+
"cheap",
|
133 |
+
"moderate"
|
134 |
+
]
|
135 |
+
},
|
136 |
+
"type": {
|
137 |
+
"description": "what is the type of the hotel",
|
138 |
+
"is_categorical": False,
|
139 |
+
"possible_values": [
|
140 |
+
"guesthouse",
|
141 |
+
"hotel"
|
142 |
+
]
|
143 |
+
},
|
144 |
+
"name": {
|
145 |
+
"description": "name of the hotel",
|
146 |
+
"is_categorical": False,
|
147 |
+
"possible_values": []
|
148 |
+
},
|
149 |
+
"book people": {
|
150 |
+
"description": "number of people for the hotel booking",
|
151 |
+
"is_categorical": False,
|
152 |
+
"possible_values": []
|
153 |
+
},
|
154 |
+
"book stay": {
|
155 |
+
"description": "length of stay at the hotel",
|
156 |
+
"is_categorical": False,
|
157 |
+
"possible_values": []
|
158 |
+
},
|
159 |
+
"book day": {
|
160 |
+
"description": "day of the hotel booking",
|
161 |
+
"is_categorical": True,
|
162 |
+
"possible_values": [
|
163 |
+
"monday",
|
164 |
+
"tuesday",
|
165 |
+
"wednesday",
|
166 |
+
"thursday",
|
167 |
+
"friday",
|
168 |
+
"saturday",
|
169 |
+
"sunday"
|
170 |
+
]
|
171 |
+
},
|
172 |
+
"phone": {
|
173 |
+
"description": "phone number of the hotel",
|
174 |
+
"is_categorical": False,
|
175 |
+
"possible_values": []
|
176 |
+
},
|
177 |
+
"postcode": {
|
178 |
+
"description": "postcode of the hotel",
|
179 |
+
"is_categorical": False,
|
180 |
+
"possible_values": []
|
181 |
+
},
|
182 |
+
"address": {
|
183 |
+
"description": "address of the hotel",
|
184 |
+
"is_categorical": False,
|
185 |
+
"possible_values": []
|
186 |
+
},
|
187 |
+
"ref": {
|
188 |
+
"description": "reference number of the hotel booking",
|
189 |
+
"is_categorical": False,
|
190 |
+
"possible_values": []
|
191 |
+
},
|
192 |
+
"choice": {
|
193 |
+
"description": "number of hotels that meet the requirement",
|
194 |
+
"is_categorical": False,
|
195 |
+
"possible_values": []
|
196 |
+
}
|
197 |
+
}
|
198 |
+
},
|
199 |
+
"taxi": {
|
200 |
+
"description": "rent taxi to travel",
|
201 |
+
"slots": {
|
202 |
+
"destination": {
|
203 |
+
"description": "destination of taxi",
|
204 |
+
"is_categorical": False,
|
205 |
+
"possible_values": []
|
206 |
+
},
|
207 |
+
"departure": {
|
208 |
+
"description": "departure location of taxi",
|
209 |
+
"is_categorical": False,
|
210 |
+
"possible_values": []
|
211 |
+
},
|
212 |
+
"leave at": {
|
213 |
+
"description": "leaving time of taxi",
|
214 |
+
"is_categorical": False,
|
215 |
+
"possible_values": []
|
216 |
+
},
|
217 |
+
"arrive by": {
|
218 |
+
"description": "arrival time of taxi",
|
219 |
+
"is_categorical": False,
|
220 |
+
"possible_values": []
|
221 |
+
},
|
222 |
+
"phone": {
|
223 |
+
"description": "phone number of the taxi",
|
224 |
+
"is_categorical": False,
|
225 |
+
"possible_values": []
|
226 |
+
},
|
227 |
+
"type": {
|
228 |
+
"description": "car type of the taxi",
|
229 |
+
"is_categorical": False,
|
230 |
+
"possible_values": []
|
231 |
+
}
|
232 |
+
}
|
233 |
+
},
|
234 |
+
"restaurant": {
|
235 |
+
"description": "find and book a restaurant",
|
236 |
+
"slots": {
|
237 |
+
"price range": {
|
238 |
+
"description": "price budget for the restaurant",
|
239 |
+
"is_categorical": True,
|
240 |
+
"possible_values": [
|
241 |
+
"cheap",
|
242 |
+
"expensive",
|
243 |
+
"moderate"
|
244 |
+
]
|
245 |
+
},
|
246 |
+
"area": {
|
247 |
+
"description": "area or place of the restaurant",
|
248 |
+
"is_categorical": True,
|
249 |
+
"possible_values": [
|
250 |
+
"centre",
|
251 |
+
"east",
|
252 |
+
"north",
|
253 |
+
"south",
|
254 |
+
"west"
|
255 |
+
]
|
256 |
+
},
|
257 |
+
"food": {
|
258 |
+
"description": "the cuisine of the restaurant",
|
259 |
+
"is_categorical": False,
|
260 |
+
"possible_values": []
|
261 |
+
},
|
262 |
+
"name": {
|
263 |
+
"description": "name of the restaurant",
|
264 |
+
"is_categorical": False,
|
265 |
+
"possible_values": []
|
266 |
+
},
|
267 |
+
"address": {
|
268 |
+
"description": "address of the restaurant",
|
269 |
+
"is_categorical": False,
|
270 |
+
"possible_values": []
|
271 |
+
},
|
272 |
+
"postcode": {
|
273 |
+
"description": "postcode of the restaurant",
|
274 |
+
"is_categorical": False,
|
275 |
+
"possible_values": []
|
276 |
+
},
|
277 |
+
"phone": {
|
278 |
+
"description": "phone number of the restaurant",
|
279 |
+
"is_categorical": False,
|
280 |
+
"possible_values": []
|
281 |
+
},
|
282 |
+
"book people": {
|
283 |
+
"description": "number of people for the restaurant booking",
|
284 |
+
"is_categorical": False,
|
285 |
+
"possible_values": []
|
286 |
+
},
|
287 |
+
"book time": {
|
288 |
+
"description": "time of the restaurant booking",
|
289 |
+
"is_categorical": False,
|
290 |
+
"possible_values": []
|
291 |
+
},
|
292 |
+
"book day": {
|
293 |
+
"description": "day of the restaurant booking",
|
294 |
+
"is_categorical": True,
|
295 |
+
"possible_values": [
|
296 |
+
"monday",
|
297 |
+
"tuesday",
|
298 |
+
"wednesday",
|
299 |
+
"thursday",
|
300 |
+
"friday",
|
301 |
+
"saturday",
|
302 |
+
"sunday"
|
303 |
+
]
|
304 |
+
},
|
305 |
+
"ref": {
|
306 |
+
"description": "reference number of the restaurant booking",
|
307 |
+
"is_categorical": False,
|
308 |
+
"possible_values": []
|
309 |
+
},
|
310 |
+
"choice": {
|
311 |
+
"description": "number of restaurants that meet the requirement",
|
312 |
+
"is_categorical": False,
|
313 |
+
"possible_values": []
|
314 |
+
}
|
315 |
+
}
|
316 |
+
},
|
317 |
+
"train": {
|
318 |
+
"description": "find a train to travel",
|
319 |
+
"slots": {
|
320 |
+
"destination": {
|
321 |
+
"description": "destination of the train",
|
322 |
+
"is_categorical": False,
|
323 |
+
"possible_values": []
|
324 |
+
},
|
325 |
+
"arrive by": {
|
326 |
+
"description": "arrival time of the train",
|
327 |
+
"is_categorical": False,
|
328 |
+
"possible_values": []
|
329 |
+
},
|
330 |
+
"departure": {
|
331 |
+
"description": "departure location of the train",
|
332 |
+
"is_categorical": False,
|
333 |
+
"possible_values": []
|
334 |
+
},
|
335 |
+
"leave at": {
|
336 |
+
"description": "leaving time for the train",
|
337 |
+
"is_categorical": False,
|
338 |
+
"possible_values": []
|
339 |
+
},
|
340 |
+
"duration": {
|
341 |
+
"description": "duration of the travel",
|
342 |
+
"is_categorical": False,
|
343 |
+
"possible_values": []
|
344 |
+
},
|
345 |
+
"book people": {
|
346 |
+
"description": "number of people booking for train",
|
347 |
+
"is_categorical": False,
|
348 |
+
"possible_values": []
|
349 |
+
},
|
350 |
+
"day": {
|
351 |
+
"description": "day of the train",
|
352 |
+
"is_categorical": True,
|
353 |
+
"possible_values": [
|
354 |
+
"monday",
|
355 |
+
"tuesday",
|
356 |
+
"wednesday",
|
357 |
+
"thursday",
|
358 |
+
"friday",
|
359 |
+
"saturday",
|
360 |
+
"sunday"
|
361 |
+
]
|
362 |
+
},
|
363 |
+
"ref": {
|
364 |
+
"description": "reference number of the train booking",
|
365 |
+
"is_categorical": False,
|
366 |
+
"possible_values": []
|
367 |
+
},
|
368 |
+
"price": {
|
369 |
+
"description": "price of the train ticket",
|
370 |
+
"is_categorical": False,
|
371 |
+
"possible_values": []
|
372 |
+
},
|
373 |
+
"train id": {
|
374 |
+
"description": "id of the train",
|
375 |
+
"is_categorical": False
|
376 |
+
},
|
377 |
+
"choice": {
|
378 |
+
"description": "number of trains that meet the requirement",
|
379 |
+
"is_categorical": False,
|
380 |
+
"possible_values": []
|
381 |
+
}
|
382 |
+
}
|
383 |
+
},
|
384 |
+
"police": {
|
385 |
+
"description": "find a police station for help",
|
386 |
+
"slots": {
|
387 |
+
"name": {
|
388 |
+
"description": "name of the police station",
|
389 |
+
"is_categorical": False,
|
390 |
+
"possible_values": []
|
391 |
+
},
|
392 |
+
"address": {
|
393 |
+
"description": "address of the police station",
|
394 |
+
"is_categorical": False,
|
395 |
+
"possible_values": []
|
396 |
+
},
|
397 |
+
"postcode": {
|
398 |
+
"description": "postcode of the police station",
|
399 |
+
"is_categorical": False,
|
400 |
+
"possible_values": []
|
401 |
+
},
|
402 |
+
"phone": {
|
403 |
+
"description": "phone number of the police station",
|
404 |
+
"is_categorical": False,
|
405 |
+
"possible_values": []
|
406 |
+
}
|
407 |
+
}
|
408 |
+
},
|
409 |
+
"hospital": {
|
410 |
+
"description": "find a hospital for help",
|
411 |
+
"slots": {
|
412 |
+
"department": {
|
413 |
+
"description": "specific department of the hospital",
|
414 |
+
"is_categorical": False,
|
415 |
+
"possible_values": []
|
416 |
+
},
|
417 |
+
"address": {
|
418 |
+
"description": "address of the hospital",
|
419 |
+
"is_categorical": False,
|
420 |
+
"possible_values": []
|
421 |
+
},
|
422 |
+
"phone": {
|
423 |
+
"description": "phone number of the hospital",
|
424 |
+
"is_categorical": False,
|
425 |
+
"possible_values": []
|
426 |
+
},
|
427 |
+
"postcode": {
|
428 |
+
"description": "postcode of the hospital",
|
429 |
+
"is_categorical": False,
|
430 |
+
"possible_values": []
|
431 |
+
}
|
432 |
+
}
|
433 |
+
},
|
434 |
+
"general": {
|
435 |
+
"description": "general domain without slots",
|
436 |
+
"slots": {}
|
437 |
+
}
|
438 |
+
},
|
439 |
+
"intents": {
|
440 |
+
"inform": {
|
441 |
+
"description": "inform the value of a slot"
|
442 |
+
},
|
443 |
+
"request": {
|
444 |
+
"description": "ask for the value of a slot"
|
445 |
+
},
|
446 |
+
"nobook": {
|
447 |
+
"description": "inform the user that the booking is failed"
|
448 |
+
},
|
449 |
+
"reqmore": {
|
450 |
+
"description": "ask the user for more instructions"
|
451 |
+
},
|
452 |
+
"book": {
|
453 |
+
"description": "book something for the user"
|
454 |
+
},
|
455 |
+
"bye": {
|
456 |
+
"description": "say goodbye to the user and end the conversation"
|
457 |
+
},
|
458 |
+
"thank": {
|
459 |
+
"description": "thanks for the help"
|
460 |
+
},
|
461 |
+
"welcome": {
|
462 |
+
"description": "you're welcome"
|
463 |
+
},
|
464 |
+
"greet": {
|
465 |
+
"description": "express greeting"
|
466 |
+
},
|
467 |
+
"recommend": {
|
468 |
+
"description": "recommend a choice to the user"
|
469 |
+
},
|
470 |
+
"select": {
|
471 |
+
"description": "provide several choices for the user"
|
472 |
+
},
|
473 |
+
"offerbook": {
|
474 |
+
"description": "ask the user if he or she needs booking"
|
475 |
+
},
|
476 |
+
"offerbooked": {
|
477 |
+
"description": "provide information about the booking"
|
478 |
+
},
|
479 |
+
"nooffer": {
|
480 |
+
"description": "inform the user that there is no result satisfies user requirements"
|
481 |
+
}
|
482 |
+
},
|
483 |
+
"state": {
|
484 |
+
"attraction": {
|
485 |
+
"type": "",
|
486 |
+
"name": "",
|
487 |
+
"area": ""
|
488 |
+
},
|
489 |
+
"hotel": {
|
490 |
+
"name": "",
|
491 |
+
"area": "",
|
492 |
+
"parking": "",
|
493 |
+
"price range": "",
|
494 |
+
"stars": "",
|
495 |
+
"internet": "",
|
496 |
+
"type": "",
|
497 |
+
"book stay": "",
|
498 |
+
"book day": "",
|
499 |
+
"book people": ""
|
500 |
+
},
|
501 |
+
"restaurant": {
|
502 |
+
"food": "",
|
503 |
+
"price range": "",
|
504 |
+
"name": "",
|
505 |
+
"area": "",
|
506 |
+
"book time": "",
|
507 |
+
"book day": "",
|
508 |
+
"book people": ""
|
509 |
+
},
|
510 |
+
"taxi": {
|
511 |
+
"leave at": "",
|
512 |
+
"destination": "",
|
513 |
+
"departure": "",
|
514 |
+
"arrive by": ""
|
515 |
+
},
|
516 |
+
"train": {
|
517 |
+
"leave at": "",
|
518 |
+
"destination": "",
|
519 |
+
"day": "",
|
520 |
+
"arrive by": "",
|
521 |
+
"departure": "",
|
522 |
+
"book people": ""
|
523 |
+
},
|
524 |
+
"hospital": {
|
525 |
+
"department": ""
|
526 |
+
}
|
527 |
+
},
|
528 |
+
"dialogue_acts": {
|
529 |
+
"categorical": {},
|
530 |
+
"non-categorical": {},
|
531 |
+
"binary": {}
|
532 |
+
}
|
533 |
+
}
|
534 |
+
|
535 |
+
slot_name_map = {
|
536 |
+
'addr': "address",
|
537 |
+
'post': "postcode",
|
538 |
+
'pricerange': "price range",
|
539 |
+
'arrive': "arrive by",
|
540 |
+
'arriveby': "arrive by",
|
541 |
+
'leave': "leave at",
|
542 |
+
'leaveat': "leave at",
|
543 |
+
'depart': "departure",
|
544 |
+
'dest': "destination",
|
545 |
+
'fee': "entrance fee",
|
546 |
+
'open': 'open hours',
|
547 |
+
'car': "type",
|
548 |
+
'car type': "type",
|
549 |
+
'ticket': 'price',
|
550 |
+
'trainid': 'train id',
|
551 |
+
'id': 'train id',
|
552 |
+
'people': 'book people',
|
553 |
+
'stay': 'book stay',
|
554 |
+
'none': '',
|
555 |
+
'attraction': {
|
556 |
+
'price': 'entrance fee'
|
557 |
+
},
|
558 |
+
'hospital': {},
|
559 |
+
'hotel': {
|
560 |
+
'day': 'book day', 'price': "price range"
|
561 |
+
},
|
562 |
+
'restaurant': {
|
563 |
+
'day': 'book day', 'time': 'book time', 'price': "price range"
|
564 |
+
},
|
565 |
+
'taxi': {},
|
566 |
+
'train': {
|
567 |
+
'day': 'day', 'time': "duration"
|
568 |
+
},
|
569 |
+
'police': {},
|
570 |
+
'booking': {}
|
571 |
+
}
|
572 |
+
|
573 |
+
reverse_da_slot_name_map = {
|
574 |
+
'address': 'Addr',
|
575 |
+
'postcode': 'Post',
|
576 |
+
'price range': 'Price',
|
577 |
+
'arrive by': 'Arrive',
|
578 |
+
'leave at': 'Leave',
|
579 |
+
'departure': 'Depart',
|
580 |
+
'destination': 'Dest',
|
581 |
+
'entrance fee': 'Fee',
|
582 |
+
'open hours': 'Open',
|
583 |
+
'price': 'Ticket',
|
584 |
+
'train id': 'Id',
|
585 |
+
'book people': 'People',
|
586 |
+
'book stay': 'Stay',
|
587 |
+
'book day': 'Day',
|
588 |
+
'book time': 'Time',
|
589 |
+
'duration': 'Time',
|
590 |
+
'taxi': {
|
591 |
+
'type': 'Car',
|
592 |
+
'phone': 'Phone'
|
593 |
+
}
|
594 |
+
}
|
595 |
+
|
596 |
+
digit2word = {
|
597 |
+
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five',
|
598 |
+
'6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten'
|
599 |
+
}
|
600 |
+
|
601 |
+
cnt_domain_slot = Counter()
|
602 |
+
|
603 |
+
|
604 |
+
class BookingActRemapper:
|
605 |
+
|
606 |
+
def __init__(self, ontology):
|
607 |
+
self.ontology = ontology
|
608 |
+
self.reset()
|
609 |
+
|
610 |
+
def reset(self):
|
611 |
+
self.current_domains_user = []
|
612 |
+
self.current_domains_system = []
|
613 |
+
self.booked_domains = []
|
614 |
+
|
615 |
+
def retrieve_current_domain_from_user(self, turn_id, ori_dialog):
|
616 |
+
prev_user_turn = ori_dialog[turn_id - 1]
|
617 |
+
|
618 |
+
dialog_acts = prev_user_turn.get('dialog_act', [])
|
619 |
+
keyword_domains_user = get_keyword_domains(prev_user_turn)
|
620 |
+
current_domains_temp = get_current_domains_from_act(dialog_acts)
|
621 |
+
self.current_domains_user = current_domains_temp if current_domains_temp else self.current_domains_user
|
622 |
+
next_user_domains = get_next_user_act_domains(ori_dialog, turn_id)
|
623 |
+
|
624 |
+
return keyword_domains_user, next_user_domains
|
625 |
+
|
626 |
+
def retrieve_current_domain_from_system(self, turn_id, ori_dialog):
|
627 |
+
|
628 |
+
system_turn = ori_dialog[turn_id]
|
629 |
+
dialog_acts = system_turn.get('dialog_act', [])
|
630 |
+
keyword_domains_system = get_keyword_domains(system_turn)
|
631 |
+
current_domains_temp = get_current_domains_from_act(dialog_acts)
|
632 |
+
self.current_domains_system = current_domains_temp if current_domains_temp else self.current_domains_system
|
633 |
+
booked_domain_current = self.check_domain_booked(system_turn)
|
634 |
+
|
635 |
+
return keyword_domains_system, booked_domain_current
|
636 |
+
|
637 |
+
def remap(self, turn_id, ori_dialog):
|
638 |
+
|
639 |
+
keyword_domains_user, next_user_domains = self.retrieve_current_domain_from_user(
|
640 |
+
turn_id, ori_dialog)
|
641 |
+
keyword_domains_system, booked_domain_current = self.retrieve_current_domain_from_system(
|
642 |
+
turn_id, ori_dialog)
|
643 |
+
|
644 |
+
# only need to remap if there is a dialog action labelled
|
645 |
+
dialog_acts = ori_dialog[turn_id].get('dialog_act', [])
|
646 |
+
spans = ori_dialog[turn_id].get('span_info', [])
|
647 |
+
if dialog_acts:
|
648 |
+
|
649 |
+
flattened_acts = flatten_acts(dialog_acts)
|
650 |
+
flattened_spans = flatten_span_acts(spans)
|
651 |
+
remapped_acts, error_local = remap_acts(flattened_acts, self.current_domains_user,
|
652 |
+
booked_domain_current, keyword_domains_user,
|
653 |
+
keyword_domains_system, self.current_domains_system,
|
654 |
+
next_user_domains, self.ontology)
|
655 |
+
|
656 |
+
remapped_spans, _ = remap_acts(flattened_spans, self.current_domains_user,
|
657 |
+
booked_domain_current, keyword_domains_user,
|
658 |
+
keyword_domains_system, self.current_domains_system,
|
659 |
+
next_user_domains, self.ontology)
|
660 |
+
|
661 |
+
deflattened_remapped_acts = deflat_acts(remapped_acts)
|
662 |
+
deflattened_remapped_spans = deflat_span_acts(remapped_spans)
|
663 |
+
|
664 |
+
return deflattened_remapped_acts, deflattened_remapped_spans
|
665 |
+
else:
|
666 |
+
return dialog_acts, spans
|
667 |
+
|
668 |
+
def check_domain_booked(self, turn):
|
669 |
+
|
670 |
+
booked_domain_current = None
|
671 |
+
return booked_domain_current
|
672 |
+
|
673 |
+
# workaround
|
674 |
+
for domain in turn['metadata']:
|
675 |
+
if turn['metadata'][domain]["book"]["booked"] and domain not in self.booked_domains:
|
676 |
+
booked_domain_current = domain.capitalize()
|
677 |
+
self.booked_domains.append(domain)
|
678 |
+
return booked_domain_current
|
679 |
+
|
680 |
+
|
681 |
+
def get_keyword_domains(turn):
|
682 |
+
keyword_domains = []
|
683 |
+
text = turn['text']
|
684 |
+
for d in ["Hotel", "Restaurant", "Train"]:
|
685 |
+
if d.lower() in text.lower():
|
686 |
+
keyword_domains.append(d)
|
687 |
+
return keyword_domains
|
688 |
+
|
689 |
+
|
690 |
+
def get_current_domains_from_act(dialog_acts):
|
691 |
+
|
692 |
+
current_domains_temp = []
|
693 |
+
for dom_int in dialog_acts:
|
694 |
+
domain, intent = dom_int.split('-')
|
695 |
+
if domain in ["general", "Booking"]:
|
696 |
+
continue
|
697 |
+
if domain not in current_domains_temp:
|
698 |
+
current_domains_temp.append(domain)
|
699 |
+
|
700 |
+
return current_domains_temp
|
701 |
+
|
702 |
+
|
703 |
+
def get_next_user_act_domains(ori_dialog, turn_id):
|
704 |
+
domains = []
|
705 |
+
try:
|
706 |
+
next_user_act = ori_dialog[turn_id + 1]['dialog_act']
|
707 |
+
domains = get_current_domains_from_act(next_user_act)
|
708 |
+
except:
|
709 |
+
# will fail if system act is the last act of the dialogue
|
710 |
+
pass
|
711 |
+
return domains
|
712 |
+
|
713 |
+
|
714 |
+
def flatten_acts(dialog_acts):
|
715 |
+
flattened_acts = []
|
716 |
+
for dom_int in dialog_acts:
|
717 |
+
domain, intent = dom_int.split('-')
|
718 |
+
for slot_value in dialog_acts[dom_int]:
|
719 |
+
slot = slot_value[0]
|
720 |
+
value = slot_value[1]
|
721 |
+
flattened_acts.append((domain, intent, slot, value))
|
722 |
+
|
723 |
+
return flattened_acts
|
724 |
+
|
725 |
+
|
726 |
+
def flatten_span_acts(span_acts):
|
727 |
+
|
728 |
+
flattened_acts = []
|
729 |
+
for span_act in span_acts:
|
730 |
+
domain, intent = span_act[0].split("-")
|
731 |
+
flattened_acts.append((domain, intent, span_act[1], span_act[2:]))
|
732 |
+
return flattened_acts
|
733 |
+
|
734 |
+
|
735 |
+
def deflat_acts(flattened_acts):
|
736 |
+
|
737 |
+
dialog_acts = dict()
|
738 |
+
|
739 |
+
for act in flattened_acts:
|
740 |
+
domain, intent, slot, value = act
|
741 |
+
if f"{domain}-{intent}" not in dialog_acts.keys():
|
742 |
+
dialog_acts[f"{domain}-{intent}"] = [[slot, value]]
|
743 |
+
else:
|
744 |
+
dialog_acts[f"{domain}-{intent}"].append([slot, value])
|
745 |
+
|
746 |
+
return dialog_acts
|
747 |
+
|
748 |
+
|
749 |
+
def deflat_span_acts(flattened_acts):
|
750 |
+
|
751 |
+
dialog_span_acts = []
|
752 |
+
for act in flattened_acts:
|
753 |
+
domain, intent, slot, value = act
|
754 |
+
if value == 'none':
|
755 |
+
continue
|
756 |
+
new_act = [f"{domain}-{intent}", slot]
|
757 |
+
new_act.extend(value)
|
758 |
+
dialog_span_acts.append(new_act)
|
759 |
+
|
760 |
+
return dialog_span_acts
|
761 |
+
|
762 |
+
|
763 |
+
def remap_acts(flattened_acts, current_domains, booked_domain=None, keyword_domains_user=None,
|
764 |
+
keyword_domains_system=None, current_domain_system=None, next_user_domain=None, ontology=None):
|
765 |
+
|
766 |
+
# We now look for all cases that can happen: Booking domain, Booking within a domain or taxi-inform-car for booking
|
767 |
+
error = 0
|
768 |
+
remapped_acts = []
|
769 |
+
|
770 |
+
# if there is more than one current domain or none at all, we try to get booked domain differently
|
771 |
+
if len(current_domains) != 1 and booked_domain:
|
772 |
+
current_domains = [booked_domain]
|
773 |
+
elif len(current_domains) != 1 and len(keyword_domains_user) == 1:
|
774 |
+
current_domains = keyword_domains_user
|
775 |
+
elif len(current_domains) != 1 and len(keyword_domains_system) == 1:
|
776 |
+
current_domains = keyword_domains_system
|
777 |
+
elif len(current_domains) != 1 and len(current_domain_system) == 1:
|
778 |
+
current_domains = current_domain_system
|
779 |
+
elif len(current_domains) != 1 and len(next_user_domain) == 1:
|
780 |
+
current_domains = next_user_domain
|
781 |
+
|
782 |
+
for act in flattened_acts:
|
783 |
+
try:
|
784 |
+
domain, intent, slot, value = act
|
785 |
+
if f"{domain}-{intent}-{slot}" == "Booking-Book-Ref":
|
786 |
+
# We need to remap that booking act now
|
787 |
+
potential_domain = current_domains[0]
|
788 |
+
remapped_acts.append(
|
789 |
+
(potential_domain, "Book", "none", "none"))
|
790 |
+
if ontology_check(potential_domain, slot, ontology):
|
791 |
+
remapped_acts.append(
|
792 |
+
(potential_domain, "Inform", "Ref", value))
|
793 |
+
elif domain == "Booking" and intent == "Book" and slot != "Ref":
|
794 |
+
# the book intent is here actually an inform intent according to the data
|
795 |
+
potential_domain = current_domains[0]
|
796 |
+
if ontology_check(potential_domain, slot, ontology):
|
797 |
+
remapped_acts.append(
|
798 |
+
(potential_domain, "Inform", slot, value))
|
799 |
+
elif domain == "Booking" and intent == "Inform":
|
800 |
+
# the inform intent is here actually a request intent according to the data
|
801 |
+
potential_domain = current_domains[0]
|
802 |
+
if ontology_check(potential_domain, slot, ontology):
|
803 |
+
remapped_acts.append(
|
804 |
+
(potential_domain, "OfferBook", slot, value))
|
805 |
+
elif domain == "Booking" and intent in ["NoBook", "Request"]:
|
806 |
+
potential_domain = current_domains[0]
|
807 |
+
if ontology_check(potential_domain, slot, ontology):
|
808 |
+
remapped_acts.append(
|
809 |
+
(potential_domain, intent, slot, value))
|
810 |
+
elif f"{domain}-{intent}-{slot}" == "Taxi-Inform-Car":
|
811 |
+
# taxi-inform-car actually triggers the booking and informs on a car
|
812 |
+
remapped_acts.append((domain, "Book", "none", "none"))
|
813 |
+
remapped_acts.append((domain, intent, slot, value))
|
814 |
+
elif f"{domain}-{intent}-{slot}" in ["Train-Inform-Ref", "Train-OfferBooked-Ref"]:
|
815 |
+
# train-inform/offerbooked-ref actually triggers the booking and informs on the reference number
|
816 |
+
remapped_acts.append((domain, "Book", "none", "none"))
|
817 |
+
remapped_acts.append((domain, "Inform", slot, value))
|
818 |
+
elif domain == "Train" and intent == "OfferBooked" and slot != "Ref":
|
819 |
+
# this is actually an inform act
|
820 |
+
remapped_acts.append((domain, "Inform", slot, value))
|
821 |
+
else:
|
822 |
+
remapped_acts.append(act)
|
823 |
+
except Exception as e:
|
824 |
+
print("Error detected:", e)
|
825 |
+
error += 1
|
826 |
+
|
827 |
+
return remapped_acts, error
|
828 |
+
|
829 |
+
|
830 |
+
def ontology_check(domain_, slot_, init_ontology):
|
831 |
+
|
832 |
+
domain = domain_.lower()
|
833 |
+
slot = slot_.lower()
|
834 |
+
if slot not in init_ontology['domains'][domain]['slots']:
|
835 |
+
if slot in slot_name_map:
|
836 |
+
slot = slot_name_map[slot]
|
837 |
+
elif slot in slot_name_map[domain]:
|
838 |
+
slot = slot_name_map[domain][slot]
|
839 |
+
return slot in init_ontology['domains'][domain]['slots']
|
840 |
+
|
841 |
+
|
842 |
+
def reverse_da(dialogue_acts):
|
843 |
+
global reverse_da_slot_name_map
|
844 |
+
das = {}
|
845 |
+
for da_type in dialogue_acts:
|
846 |
+
for da in dialogue_acts[da_type]:
|
847 |
+
intent, domain, slot, value = da['intent'], da['domain'], da['slot'], da.get(
|
848 |
+
'value', '')
|
849 |
+
if domain == 'general':
|
850 |
+
Domain_Intent = '-'.join([domain, intent])
|
851 |
+
elif intent == 'nooffer':
|
852 |
+
Domain_Intent = '-'.join([domain.capitalize(), 'NoOffer'])
|
853 |
+
elif intent == 'nobook':
|
854 |
+
Domain_Intent = '-'.join([domain.capitalize(), 'NoBook'])
|
855 |
+
elif intent == 'offerbook':
|
856 |
+
Domain_Intent = '-'.join([domain.capitalize(), 'OfferBook'])
|
857 |
+
else:
|
858 |
+
Domain_Intent = '-'.join([domain.capitalize(),
|
859 |
+
intent.capitalize()])
|
860 |
+
das.setdefault(Domain_Intent, [])
|
861 |
+
if slot in reverse_da_slot_name_map:
|
862 |
+
Slot = reverse_da_slot_name_map[slot]
|
863 |
+
elif domain in reverse_da_slot_name_map and slot in reverse_da_slot_name_map[domain]:
|
864 |
+
Slot = reverse_da_slot_name_map[domain][slot]
|
865 |
+
else:
|
866 |
+
Slot = slot.capitalize()
|
867 |
+
if value == '':
|
868 |
+
if intent == 'request':
|
869 |
+
value = '?'
|
870 |
+
else:
|
871 |
+
value = 'none'
|
872 |
+
if Slot == '':
|
873 |
+
Slot = 'none'
|
874 |
+
das[Domain_Intent].append([Slot, value])
|
875 |
+
return das
|
876 |
+
|
877 |
+
|
878 |
+
def normalize_domain_slot_value(domain, slot, value):
|
879 |
+
global ontology, slot_name_map
|
880 |
+
domain = domain.lower()
|
881 |
+
slot = slot.lower()
|
882 |
+
value = value.strip()
|
883 |
+
if value in ['do nt care', "do n't care"]:
|
884 |
+
value = 'dontcare'
|
885 |
+
if value in ['?', 'none', 'not mentioned']:
|
886 |
+
value = ""
|
887 |
+
if domain not in ontology['domains']:
|
888 |
+
raise Exception(f'{domain} not in ontology')
|
889 |
+
if slot not in ontology['domains'][domain]['slots']:
|
890 |
+
if slot in slot_name_map:
|
891 |
+
slot = slot_name_map[slot]
|
892 |
+
elif slot in slot_name_map[domain]:
|
893 |
+
slot = slot_name_map[domain][slot]
|
894 |
+
else:
|
895 |
+
raise Exception(f'{domain}-{slot} not in ontology')
|
896 |
+
assert slot == '' or slot in ontology['domains'][domain][
|
897 |
+
'slots'], f'{(domain, slot, value)} not in ontology'
|
898 |
+
return domain, slot, value
|
899 |
+
|
900 |
+
|
901 |
+
def convert_da(da_dict, utt, sent_tokenizer, word_tokenizer):
|
902 |
+
'''
|
903 |
+
convert multiwoz dialogue acts to required format
|
904 |
+
:param da_dict: dict[(intent, domain, slot, value)] = [word_start, word_end]
|
905 |
+
:param utt: user or system utt
|
906 |
+
'''
|
907 |
+
global ontology, digit2word, cnt_domain_slot
|
908 |
+
|
909 |
+
converted_da = {
|
910 |
+
'categorical': [],
|
911 |
+
'non-categorical': [],
|
912 |
+
'binary': []
|
913 |
+
}
|
914 |
+
sentences = sent_tokenizer.tokenize(utt)
|
915 |
+
sent_spans = sent_tokenizer.span_tokenize(utt)
|
916 |
+
tokens = [
|
917 |
+
token for sent in sentences for token in word_tokenizer.tokenize(sent)]
|
918 |
+
token_spans = [(sent_span[0] + token_span[0], sent_span[0] + token_span[1]) for sent, sent_span in
|
919 |
+
zip(sentences, sent_spans) for token_span in word_tokenizer.span_tokenize(sent)]
|
920 |
+
# assert len(tokens) == len(token_spans)
|
921 |
+
# for token, span in zip(tokens, token_spans):
|
922 |
+
# if utt[span[0]:span[1]] != '"':
|
923 |
+
# assert utt[span[0]:span[1]] == token
|
924 |
+
|
925 |
+
for (intent, domain, slot, value), span in da_dict.items():
|
926 |
+
if intent == 'request' or slot == '' or value == '':
|
927 |
+
# binary dialog acts
|
928 |
+
assert value == ''
|
929 |
+
converted_da['binary'].append({
|
930 |
+
'intent': intent,
|
931 |
+
'domain': domain,
|
932 |
+
'slot': slot
|
933 |
+
})
|
934 |
+
elif ontology['domains'][domain]['slots'][slot]['is_categorical']:
|
935 |
+
# categorical dialog acts
|
936 |
+
converted_da['categorical'].append({
|
937 |
+
'intent': intent,
|
938 |
+
'domain': domain,
|
939 |
+
'slot': slot,
|
940 |
+
'value': value
|
941 |
+
})
|
942 |
+
else:
|
943 |
+
# non-categorical dialog acts
|
944 |
+
converted_da['non-categorical'].append({
|
945 |
+
'intent': intent,
|
946 |
+
'domain': domain,
|
947 |
+
'slot': slot,
|
948 |
+
'value': value
|
949 |
+
})
|
950 |
+
# correct some value and try to give char level span
|
951 |
+
match = False
|
952 |
+
value = value.lower()
|
953 |
+
if span and span[0] <= span[1]:
|
954 |
+
# use original span annotation, but tokenizations are different
|
955 |
+
start_word, end_word = span
|
956 |
+
if end_word >= len(tokens):
|
957 |
+
# due to different tokenization, sometimes will out of index
|
958 |
+
delta = end_word - len(tokens) + 1
|
959 |
+
start_word -= delta
|
960 |
+
end_word -= delta
|
961 |
+
start_char, end_char = token_spans[start_word][0], token_spans[end_word][1]
|
962 |
+
value_span = utt[start_char:end_char].lower()
|
963 |
+
match = True
|
964 |
+
if value_span == value:
|
965 |
+
cnt_domain_slot['span match'] += 1
|
966 |
+
elif value.isdigit() and value in digit2word and digit2word[value] == value_span:
|
967 |
+
# !!!CHANGE VALUE: value is digit but value span is word
|
968 |
+
cnt_domain_slot['digit value match'] += 1
|
969 |
+
elif ''.join(value.split()) == ''.join(value_span.split()):
|
970 |
+
# !!!CHANGE VALUE: equal when remove blank
|
971 |
+
cnt_domain_slot['remove blank'] += 1
|
972 |
+
elif value in value_span:
|
973 |
+
# value in value_span
|
974 |
+
start_char += value_span.index(value)
|
975 |
+
end_char = start_char + len(value)
|
976 |
+
assert utt[start_char:end_char].lower(
|
977 |
+
) == value, f'{[value, utt[start_char:end_char], utt]}'
|
978 |
+
cnt_domain_slot['value in span'] += 1
|
979 |
+
elif ':' in value and value == '0' + value_span:
|
980 |
+
# !!!CHANGE VALUE: time x:xx == 0x:xx
|
981 |
+
cnt_domain_slot['x:xx == 0x:xx'] += 1
|
982 |
+
else:
|
983 |
+
# span mismatch, search near 1-2 words
|
984 |
+
for window in range(1, 3):
|
985 |
+
start = max(0, start_word - window)
|
986 |
+
end = min(len(token_spans) - 1, end_word + window)
|
987 |
+
large_span = utt[token_spans[start]
|
988 |
+
[0]:token_spans[end][1]].lower()
|
989 |
+
if value in large_span:
|
990 |
+
start_char = token_spans[start][0] + \
|
991 |
+
large_span.index(value)
|
992 |
+
end_char = start_char + len(value)
|
993 |
+
assert utt[
|
994 |
+
start_char:end_char].lower() == value, f'{[value, utt[start_char:end_char], utt]}'
|
995 |
+
cnt_domain_slot[f'window={window}'] += 1
|
996 |
+
break
|
997 |
+
else:
|
998 |
+
# still not found
|
999 |
+
match = False
|
1000 |
+
|
1001 |
+
if match:
|
1002 |
+
converted_da['non-categorical'][-1]['value'] = utt[start_char:end_char]
|
1003 |
+
converted_da['non-categorical'][-1]['start'] = start_char
|
1004 |
+
converted_da['non-categorical'][-1]['end'] = end_char
|
1005 |
+
cnt_domain_slot['have span'] += 1
|
1006 |
+
else:
|
1007 |
+
cnt_domain_slot['no span'] += 1
|
1008 |
+
return converted_da
|
1009 |
+
|
1010 |
+
|
1011 |
+
def preprocess():
|
1012 |
+
original_data_dir = 'emowoz'
|
1013 |
+
new_data_dir = 'data'
|
1014 |
+
|
1015 |
+
if not os.path.exists(original_data_dir):
|
1016 |
+
original_data_zip = 'MultiWOZ_2.1.zip'
|
1017 |
+
if not os.path.exists(original_data_zip):
|
1018 |
+
raise FileNotFoundError(
|
1019 |
+
f'cannot find original data {original_data_zip} in multiwoz21/, should manually download MultiWOZ_2.1.zip from https://github.com/budzianowski/multiwoz/blob/master/data/MultiWOZ_2.1.zip')
|
1020 |
+
else:
|
1021 |
+
archive = ZipFile(original_data_zip)
|
1022 |
+
archive.extractall()
|
1023 |
+
|
1024 |
+
os.makedirs(new_data_dir, exist_ok=True)
|
1025 |
+
for filename in os.listdir(original_data_dir):
|
1026 |
+
if 'db' in filename:
|
1027 |
+
copy2(f'{original_data_dir}/{filename}', new_data_dir)
|
1028 |
+
|
1029 |
+
# how about emowoz-dialmage
|
1030 |
+
original_data = json.load(
|
1031 |
+
open(f'{original_data_dir}/emowoz-multiwoz.json'))
|
1032 |
+
global ontology, cnt_domain_slot
|
1033 |
+
|
1034 |
+
data_split = json.load(open(f'{original_data_dir}/data_split.json'))
|
1035 |
+
val_list = data_split["dev"]["multiwoz"]
|
1036 |
+
test_list = data_split["test"]["multiwoz"]
|
1037 |
+
# val_list = set(open(f'{original_data_dir}/valListFile.txt').read().split())
|
1038 |
+
# test_list = set(open(f'{original_data_dir}/testListFile.txt').read().split())
|
1039 |
+
dataset = 'multiwoz21'
|
1040 |
+
splits = ['train', 'validation', 'test']
|
1041 |
+
dialogues_by_split = {split: [] for split in splits}
|
1042 |
+
sent_tokenizer = PunktSentenceTokenizer()
|
1043 |
+
word_tokenizer = TreebankWordTokenizer()
|
1044 |
+
booking_remapper = BookingActRemapper(ontology)
|
1045 |
+
for ori_dialog_id, ori_dialog in tqdm(original_data.items()):
|
1046 |
+
if ori_dialog_id in val_list:
|
1047 |
+
split = 'validation'
|
1048 |
+
elif ori_dialog_id in test_list:
|
1049 |
+
split = 'test'
|
1050 |
+
else:
|
1051 |
+
split = 'train'
|
1052 |
+
dialogue_id = f'{dataset}-{split}-{len(dialogues_by_split[split])}'
|
1053 |
+
|
1054 |
+
# get user goal and involved domains
|
1055 |
+
cur_domains = []
|
1056 |
+
|
1057 |
+
dialogue = {
|
1058 |
+
'dataset': dataset,
|
1059 |
+
'data_split': split,
|
1060 |
+
'dialogue_id': dialogue_id,
|
1061 |
+
'original_id': ori_dialog_id,
|
1062 |
+
'domains': cur_domains, # will be updated by dialog_acts and state
|
1063 |
+
'goal': "",
|
1064 |
+
'turns': []
|
1065 |
+
}
|
1066 |
+
|
1067 |
+
booking_remapper.reset()
|
1068 |
+
belief_domains = ['attraction', 'restaurant',
|
1069 |
+
'train', 'hotel', 'taxi', 'hospital']
|
1070 |
+
entity_booked_dict = dict((domain, False) for domain in belief_domains)
|
1071 |
+
for turn_id, turn in enumerate(ori_dialog['log']):
|
1072 |
+
# correct some grammar errors in the text, mainly following `tokenization.md` in MultiWOZ_2.1
|
1073 |
+
text = turn['text']
|
1074 |
+
text = re.sub(" Im ", " I'm ", text)
|
1075 |
+
text = re.sub(" im ", " i'm ", text)
|
1076 |
+
text = re.sub(r"^Im ", "I'm ", text)
|
1077 |
+
text = re.sub(r"^im ", "i'm ", text)
|
1078 |
+
text = re.sub("theres", "there's", text)
|
1079 |
+
text = re.sub("dont", "don't", text)
|
1080 |
+
text = re.sub("whats", "what's", text)
|
1081 |
+
text = re.sub('thats', "that's", text)
|
1082 |
+
utt = text
|
1083 |
+
speaker = 'user' if turn_id % 2 == 0 else 'system'
|
1084 |
+
|
1085 |
+
das = turn.get('dialog_act', [])
|
1086 |
+
spans = turn.get('span_info', [])
|
1087 |
+
|
1088 |
+
if speaker == 'system':
|
1089 |
+
das, spans = booking_remapper.remap(turn_id, ori_dialog['log'])
|
1090 |
+
|
1091 |
+
da_dict = {}
|
1092 |
+
# transform DA
|
1093 |
+
for Domain_Intent in das:
|
1094 |
+
domain, intent = Domain_Intent.lower().split('-')
|
1095 |
+
assert intent in ontology['intents'], f'{ori_dialog_id}:{turn_id}:da\t{intent} not in ontology'
|
1096 |
+
for Slot, value in das[Domain_Intent]:
|
1097 |
+
domain, slot, value = normalize_domain_slot_value(
|
1098 |
+
domain, Slot, value)
|
1099 |
+
if domain not in cur_domains:
|
1100 |
+
# update original cur_domains
|
1101 |
+
cur_domains.append(domain)
|
1102 |
+
da_dict[(intent, domain, slot, value,)] = []
|
1103 |
+
|
1104 |
+
for span in spans:
|
1105 |
+
Domain_Intent, Slot, value, start_word, end_word = span
|
1106 |
+
domain, intent = Domain_Intent.lower().split('-')
|
1107 |
+
domain, slot, value = normalize_domain_slot_value(
|
1108 |
+
domain, Slot, value)
|
1109 |
+
assert (intent, domain, slot, value,) in da_dict
|
1110 |
+
da_dict[(intent, domain, slot, value,)] = [
|
1111 |
+
start_word, end_word]
|
1112 |
+
|
1113 |
+
dialogue_acts = convert_da(
|
1114 |
+
da_dict, utt, sent_tokenizer, word_tokenizer)
|
1115 |
+
|
1116 |
+
# reverse_das = reverse_da(dialogue_acts)
|
1117 |
+
# das_list = sorted([(Domain_Intent, Slot, ''.join(value.split()).lower()) for Domain_Intent in das for Slot, value in das[Domain_Intent]])
|
1118 |
+
# reverse_das_list = sorted([(Domain_Intent, Slot, ''.join(value.split()).lower()) for Domain_Intent in reverse_das for Slot, value in reverse_das[Domain_Intent]])
|
1119 |
+
# if das_list != reverse_das_list:
|
1120 |
+
# print(das_list)
|
1121 |
+
# print(reverse_das_list)
|
1122 |
+
# print()
|
1123 |
+
# print()
|
1124 |
+
|
1125 |
+
dialogue['turns'].append({
|
1126 |
+
'speaker': speaker,
|
1127 |
+
'utterance': utt,
|
1128 |
+
'utt_idx': turn_id,
|
1129 |
+
'dialogue_acts': dialogue_acts,
|
1130 |
+
'emotion': turn['emotion']
|
1131 |
+
})
|
1132 |
+
|
1133 |
+
# add to dialogue_acts dictionary in the ontology
|
1134 |
+
for da_type in dialogue_acts:
|
1135 |
+
das = dialogue_acts[da_type]
|
1136 |
+
for da in das:
|
1137 |
+
ontology["dialogue_acts"][da_type].setdefault(
|
1138 |
+
(da['intent'], da['domain'], da['slot']), {})
|
1139 |
+
ontology["dialogue_acts"][da_type][(
|
1140 |
+
da['intent'], da['domain'], da['slot'])][speaker] = True
|
1141 |
+
|
1142 |
+
if speaker == 'system':
|
1143 |
+
# add state to last user turn
|
1144 |
+
# add empty db_results
|
1145 |
+
# turn_state = turn['metadata']
|
1146 |
+
cur_state = copy.deepcopy(ontology['state'])
|
1147 |
+
booked = {}
|
1148 |
+
# for domain in turn_state:
|
1149 |
+
# if domain not in cur_state:
|
1150 |
+
# continue
|
1151 |
+
# for subdomain in ['semi', 'book']:
|
1152 |
+
# for slot, value in turn_state[domain][subdomain].items():
|
1153 |
+
# if slot == 'ticket':
|
1154 |
+
# continue
|
1155 |
+
# elif slot == 'booked':
|
1156 |
+
# assert domain in ontology['domains']
|
1157 |
+
# booked[domain] = value
|
1158 |
+
# continue
|
1159 |
+
# _, slot, value = normalize_domain_slot_value(
|
1160 |
+
# domain, slot, value)
|
1161 |
+
# cur_state[domain][slot] = value
|
1162 |
+
dialogue['turns'][-2]['state'] = cur_state
|
1163 |
+
# entity_booked_dict, booked = fix_entity_booked_info(
|
1164 |
+
# entity_booked_dict, booked)
|
1165 |
+
dialogue['turns'][-1]['booked'] = booked
|
1166 |
+
dialogues_by_split[split].append(dialogue)
|
1167 |
+
# pprint(cnt_domain_slot.most_common())
|
1168 |
+
dialogues = []
|
1169 |
+
for split in splits:
|
1170 |
+
dialogues += dialogues_by_split[split]
|
1171 |
+
for da_type in ontology['dialogue_acts']:
|
1172 |
+
ontology["dialogue_acts"][da_type] = sorted([str(
|
1173 |
+
{'user': speakers.get('user', False), 'system': speakers.get('system', False), 'intent': da[0],
|
1174 |
+
'domain': da[1], 'slot': da[2]}) for da, speakers in ontology["dialogue_acts"][da_type].items()])
|
1175 |
+
json.dump(dialogues[:10], open(f'dummy_data.json', 'w',
|
1176 |
+
encoding='utf-8'), indent=2, ensure_ascii=False)
|
1177 |
+
json.dump(ontology, open(f'{new_data_dir}/ontology.json',
|
1178 |
+
'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
1179 |
+
json.dump(dialogues, open(f'{new_data_dir}/dialogues.json',
|
1180 |
+
'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
1181 |
+
with ZipFile('data.zip', 'w', ZIP_DEFLATED) as zf:
|
1182 |
+
for filename in os.listdir(new_data_dir):
|
1183 |
+
zf.write(f'{new_data_dir}/{filename}')
|
1184 |
+
# rmtree(original_data_dir)
|
1185 |
+
# rmtree(new_data_dir)
|
1186 |
+
return dialogues, ontology
|
1187 |
+
|
1188 |
+
|
1189 |
+
def fix_entity_booked_info(entity_booked_dict, booked):
|
1190 |
+
for domain in entity_booked_dict:
|
1191 |
+
if not entity_booked_dict[domain] and booked[domain]:
|
1192 |
+
entity_booked_dict[domain] = True
|
1193 |
+
booked[domain] = []
|
1194 |
+
return entity_booked_dict, booked
|
1195 |
+
|
1196 |
+
|
1197 |
+
if __name__ == '__main__':
|
1198 |
+
preprocess()
|