Delete booking_remapper.py
Browse files- booking_remapper.py +0 -266
booking_remapper.py
DELETED
@@ -1,266 +0,0 @@
|
|
1 |
-
|
2 |
-
slot_name_map = {
|
3 |
-
'addr': "address",
|
4 |
-
'post': "postcode",
|
5 |
-
'pricerange': "price range",
|
6 |
-
'arrive': "arrive by",
|
7 |
-
'arriveby': "arrive by",
|
8 |
-
'leave': "leave at",
|
9 |
-
'leaveat': "leave at",
|
10 |
-
'depart': "departure",
|
11 |
-
'dest': "destination",
|
12 |
-
'fee': "entrance fee",
|
13 |
-
'open': 'open hours',
|
14 |
-
'car': "type",
|
15 |
-
'car type': "type",
|
16 |
-
'ticket': 'price',
|
17 |
-
'trainid': 'train id',
|
18 |
-
'id': 'train id',
|
19 |
-
'people': 'book people',
|
20 |
-
'stay': 'book stay',
|
21 |
-
'none': '',
|
22 |
-
'attraction': {
|
23 |
-
'price': 'entrance fee'
|
24 |
-
},
|
25 |
-
'hospital': {},
|
26 |
-
'hotel': {
|
27 |
-
'day': 'book day', 'price': "price range"
|
28 |
-
},
|
29 |
-
'restaurant': {
|
30 |
-
'day': 'book day', 'time': 'book time', 'price': "price range"
|
31 |
-
},
|
32 |
-
'taxi': {},
|
33 |
-
'train': {
|
34 |
-
'day': 'day', 'time': "duration"
|
35 |
-
},
|
36 |
-
'police': {},
|
37 |
-
'booking': {}
|
38 |
-
}
|
39 |
-
|
40 |
-
|
41 |
-
class BookingActRemapper:
|
42 |
-
|
43 |
-
def __init__(self, ontology):
|
44 |
-
self.ontology = ontology
|
45 |
-
self.reset()
|
46 |
-
|
47 |
-
def reset(self):
|
48 |
-
self.current_domains_user = []
|
49 |
-
self.current_domains_system = []
|
50 |
-
self.booked_domains = []
|
51 |
-
|
52 |
-
def retrieve_current_domain_from_user(self, turn_id, ori_dialog):
|
53 |
-
prev_user_turn = ori_dialog[turn_id - 1]
|
54 |
-
|
55 |
-
dialog_acts = prev_user_turn.get('dialog_act', [])
|
56 |
-
keyword_domains_user = get_keyword_domains(prev_user_turn)
|
57 |
-
current_domains_temp = get_current_domains_from_act(dialog_acts)
|
58 |
-
self.current_domains_user = current_domains_temp if current_domains_temp else self.current_domains_user
|
59 |
-
next_user_domains = get_next_user_act_domains(ori_dialog, turn_id)
|
60 |
-
|
61 |
-
return keyword_domains_user, next_user_domains
|
62 |
-
|
63 |
-
def retrieve_current_domain_from_system(self, turn_id, ori_dialog):
|
64 |
-
|
65 |
-
system_turn = ori_dialog[turn_id]
|
66 |
-
dialog_acts = system_turn.get('dialog_act', [])
|
67 |
-
keyword_domains_system = get_keyword_domains(system_turn)
|
68 |
-
current_domains_temp = get_current_domains_from_act(dialog_acts)
|
69 |
-
self.current_domains_system = current_domains_temp if current_domains_temp else self.current_domains_system
|
70 |
-
booked_domain_current = self.check_domain_booked(system_turn)
|
71 |
-
|
72 |
-
return keyword_domains_system, booked_domain_current
|
73 |
-
|
74 |
-
def remap(self, turn_id, ori_dialog):
|
75 |
-
|
76 |
-
keyword_domains_user, next_user_domains = self.retrieve_current_domain_from_user(turn_id, ori_dialog)
|
77 |
-
keyword_domains_system, booked_domain_current = self.retrieve_current_domain_from_system(turn_id, ori_dialog)
|
78 |
-
|
79 |
-
# only need to remap if there is a dialog action labelled
|
80 |
-
dialog_acts = ori_dialog[turn_id].get('dialog_act', [])
|
81 |
-
spans = ori_dialog[turn_id].get('span_info', [])
|
82 |
-
if dialog_acts:
|
83 |
-
|
84 |
-
flattened_acts = flatten_acts(dialog_acts)
|
85 |
-
flattened_spans = flatten_span_acts(spans)
|
86 |
-
remapped_acts, error_local = remap_acts(flattened_acts, self.current_domains_user,
|
87 |
-
booked_domain_current, keyword_domains_user,
|
88 |
-
keyword_domains_system, self.current_domains_system,
|
89 |
-
next_user_domains, self.ontology)
|
90 |
-
|
91 |
-
remapped_spans, _ = remap_acts(flattened_spans, self.current_domains_user,
|
92 |
-
booked_domain_current, keyword_domains_user,
|
93 |
-
keyword_domains_system, self.current_domains_system,
|
94 |
-
next_user_domains, self.ontology)
|
95 |
-
|
96 |
-
deflattened_remapped_acts = deflat_acts(remapped_acts)
|
97 |
-
deflattened_remapped_spans = deflat_span_acts(remapped_spans)
|
98 |
-
|
99 |
-
return deflattened_remapped_acts, deflattened_remapped_spans
|
100 |
-
else:
|
101 |
-
return dialog_acts, spans
|
102 |
-
|
103 |
-
def check_domain_booked(self, turn):
|
104 |
-
|
105 |
-
booked_domain_current = None
|
106 |
-
for domain in turn['metadata']:
|
107 |
-
if turn['metadata'][domain]["book"]["booked"] and domain not in self.booked_domains:
|
108 |
-
booked_domain_current = domain.capitalize()
|
109 |
-
self.booked_domains.append(domain)
|
110 |
-
return booked_domain_current
|
111 |
-
|
112 |
-
|
113 |
-
def get_keyword_domains(turn):
|
114 |
-
keyword_domains = []
|
115 |
-
text = turn['text']
|
116 |
-
for d in ["Hotel", "Restaurant", "Train"]:
|
117 |
-
if d.lower() in text.lower():
|
118 |
-
keyword_domains.append(d)
|
119 |
-
return keyword_domains
|
120 |
-
|
121 |
-
|
122 |
-
def get_current_domains_from_act(dialog_acts):
|
123 |
-
|
124 |
-
current_domains_temp = []
|
125 |
-
for dom_int in dialog_acts:
|
126 |
-
domain, intent = dom_int.split('-')
|
127 |
-
if domain in ["general", "Booking"]:
|
128 |
-
continue
|
129 |
-
if domain not in current_domains_temp:
|
130 |
-
current_domains_temp.append(domain)
|
131 |
-
|
132 |
-
return current_domains_temp
|
133 |
-
|
134 |
-
|
135 |
-
def get_next_user_act_domains(ori_dialog, turn_id):
|
136 |
-
domains = []
|
137 |
-
try:
|
138 |
-
next_user_act = ori_dialog[turn_id + 1]['dialog_act']
|
139 |
-
domains = get_current_domains_from_act(next_user_act)
|
140 |
-
except:
|
141 |
-
# will fail if system act is the last act of the dialogue
|
142 |
-
pass
|
143 |
-
return domains
|
144 |
-
|
145 |
-
|
146 |
-
def flatten_acts(dialog_acts):
|
147 |
-
flattened_acts = []
|
148 |
-
for dom_int in dialog_acts:
|
149 |
-
domain, intent = dom_int.split('-')
|
150 |
-
for slot_value in dialog_acts[dom_int]:
|
151 |
-
slot = slot_value[0]
|
152 |
-
value = slot_value[1]
|
153 |
-
flattened_acts.append((domain, intent, slot, value))
|
154 |
-
|
155 |
-
return flattened_acts
|
156 |
-
|
157 |
-
|
158 |
-
def flatten_span_acts(span_acts):
|
159 |
-
|
160 |
-
flattened_acts = []
|
161 |
-
for span_act in span_acts:
|
162 |
-
domain, intent = span_act[0].split("-")
|
163 |
-
flattened_acts.append((domain, intent, span_act[1], span_act[2:]))
|
164 |
-
return flattened_acts
|
165 |
-
|
166 |
-
|
167 |
-
def deflat_acts(flattened_acts):
|
168 |
-
|
169 |
-
dialog_acts = dict()
|
170 |
-
|
171 |
-
for act in flattened_acts:
|
172 |
-
domain, intent, slot, value = act
|
173 |
-
if f"{domain}-{intent}" not in dialog_acts.keys():
|
174 |
-
dialog_acts[f"{domain}-{intent}"] = [[slot, value]]
|
175 |
-
else:
|
176 |
-
dialog_acts[f"{domain}-{intent}"].append([slot, value])
|
177 |
-
|
178 |
-
return dialog_acts
|
179 |
-
|
180 |
-
|
181 |
-
def deflat_span_acts(flattened_acts):
|
182 |
-
|
183 |
-
dialog_span_acts = []
|
184 |
-
for act in flattened_acts:
|
185 |
-
domain, intent, slot, value = act
|
186 |
-
if value == 'none':
|
187 |
-
continue
|
188 |
-
new_act = [f"{domain}-{intent}", slot]
|
189 |
-
new_act.extend(value)
|
190 |
-
dialog_span_acts.append(new_act)
|
191 |
-
|
192 |
-
return dialog_span_acts
|
193 |
-
|
194 |
-
|
195 |
-
def remap_acts(flattened_acts, current_domains, booked_domain=None, keyword_domains_user=None,
|
196 |
-
keyword_domains_system=None, current_domain_system=None, next_user_domain=None, ontology=None):
|
197 |
-
|
198 |
-
# We now look for all cases that can happen: Booking domain, Booking within a domain or taxi-inform-car for booking
|
199 |
-
error = 0
|
200 |
-
remapped_acts = []
|
201 |
-
|
202 |
-
# if there is more than one current domain or none at all, we try to get booked domain differently
|
203 |
-
if len(current_domains) != 1 and booked_domain:
|
204 |
-
current_domains = [booked_domain]
|
205 |
-
elif len(current_domains) != 1 and len(keyword_domains_user) == 1:
|
206 |
-
current_domains = keyword_domains_user
|
207 |
-
elif len(current_domains) != 1 and len(keyword_domains_system) == 1:
|
208 |
-
current_domains = keyword_domains_system
|
209 |
-
elif len(current_domains) != 1 and len(current_domain_system) == 1:
|
210 |
-
current_domains = current_domain_system
|
211 |
-
elif len(current_domains) != 1 and len(next_user_domain) == 1:
|
212 |
-
current_domains = next_user_domain
|
213 |
-
|
214 |
-
for act in flattened_acts:
|
215 |
-
try:
|
216 |
-
domain, intent, slot, value = act
|
217 |
-
if f"{domain}-{intent}-{slot}" == "Booking-Book-Ref":
|
218 |
-
# We need to remap that booking act now
|
219 |
-
potential_domain = current_domains[0]
|
220 |
-
remapped_acts.append((potential_domain, "Book", "none", "none"))
|
221 |
-
if ontology_check(potential_domain, slot, ontology):
|
222 |
-
remapped_acts.append((potential_domain, "Inform", "Ref", value))
|
223 |
-
elif domain == "Booking" and intent == "Book" and slot != "Ref":
|
224 |
-
# the book intent is here actually an inform intent according to the data
|
225 |
-
potential_domain = current_domains[0]
|
226 |
-
if ontology_check(potential_domain, slot, ontology):
|
227 |
-
remapped_acts.append((potential_domain, "Inform", slot, value))
|
228 |
-
elif domain == "Booking" and intent == "Inform":
|
229 |
-
# the inform intent is here actually a request intent according to the data
|
230 |
-
potential_domain = current_domains[0]
|
231 |
-
if ontology_check(potential_domain, slot, ontology):
|
232 |
-
remapped_acts.append((potential_domain, "OfferBook", slot, value))
|
233 |
-
elif domain == "Booking" and intent in ["NoBook", "Request"]:
|
234 |
-
potential_domain = current_domains[0]
|
235 |
-
if ontology_check(potential_domain, slot, ontology):
|
236 |
-
remapped_acts.append((potential_domain, intent, slot, value))
|
237 |
-
elif f"{domain}-{intent}-{slot}" == "Taxi-Inform-Car":
|
238 |
-
# taxi-inform-car actually triggers the booking and informs on a car
|
239 |
-
remapped_acts.append((domain, "Book", "none", "none"))
|
240 |
-
remapped_acts.append((domain, intent, slot, value))
|
241 |
-
elif f"{domain}-{intent}-{slot}" in ["Train-Inform-Ref", "Train-OfferBooked-Ref"]:
|
242 |
-
# train-inform/offerbooked-ref actually triggers the booking and informs on the reference number
|
243 |
-
remapped_acts.append((domain, "Book", "none", "none"))
|
244 |
-
remapped_acts.append((domain, "Inform", slot, value))
|
245 |
-
elif domain == "Train" and intent == "OfferBooked" and slot != "Ref":
|
246 |
-
# this is actually an inform act
|
247 |
-
remapped_acts.append((domain, "Inform", slot, value))
|
248 |
-
else:
|
249 |
-
remapped_acts.append(act)
|
250 |
-
except Exception as e:
|
251 |
-
print("Error detected:", e)
|
252 |
-
error += 1
|
253 |
-
|
254 |
-
return remapped_acts, error
|
255 |
-
|
256 |
-
|
257 |
-
def ontology_check(domain_, slot_, init_ontology):
|
258 |
-
|
259 |
-
domain = domain_.lower()
|
260 |
-
slot = slot_.lower()
|
261 |
-
if slot not in init_ontology['domains'][domain]['slots']:
|
262 |
-
if slot in slot_name_map:
|
263 |
-
slot = slot_name_map[slot]
|
264 |
-
elif slot in slot_name_map[domain]:
|
265 |
-
slot = slot_name_map[domain][slot]
|
266 |
-
return slot in init_ontology['domains'][domain]['slots']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|