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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from zipfile import ZipFile, ZIP_DEFLATED
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
from pprint import pprint
|
| 5 |
+
from copy import deepcopy
|
| 6 |
+
from collections import Counter
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
from shutil import rmtree
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
digit2word = {
|
| 12 |
+
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five',
|
| 13 |
+
'6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten'
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
match = {
|
| 17 |
+
'0': 0,
|
| 18 |
+
'1': 0,
|
| 19 |
+
'>1': 0,
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def pharse_in_sen(phrase, sen):
|
| 24 |
+
'''
|
| 25 |
+
match value in the sentence
|
| 26 |
+
:param phrase: str
|
| 27 |
+
:param sen: str
|
| 28 |
+
:return: start, end if matched, else None, None
|
| 29 |
+
'''
|
| 30 |
+
assert isinstance(phrase, str)
|
| 31 |
+
pw = '(^|[\s,\.:\?!-])(?P<v>{})([\s,\.:\?!-]|$)'
|
| 32 |
+
pn = '(^|[\s\?!-]|\D[,\.:])(?P<v>{})($|[\s\?!-]|[,\.:]\D|[,\.:]$)'
|
| 33 |
+
if phrase.isdigit():
|
| 34 |
+
pattern = pn
|
| 35 |
+
else:
|
| 36 |
+
pattern = pw
|
| 37 |
+
p = re.compile(pattern.format(re.escape(phrase)), re.I)
|
| 38 |
+
m = re.search(p, sen)
|
| 39 |
+
if m:
|
| 40 |
+
num = len(re.findall(p, sen))
|
| 41 |
+
if num > 1:
|
| 42 |
+
match['>1'] += 1
|
| 43 |
+
else:
|
| 44 |
+
match['1'] += 1
|
| 45 |
+
return m.span('v'), num
|
| 46 |
+
if phrase.isdigit() and phrase in digit2word:
|
| 47 |
+
phrase = digit2word[phrase]
|
| 48 |
+
p = re.compile(pw.format(re.escape(phrase)), re.I)
|
| 49 |
+
m = re.search(p, sen)
|
| 50 |
+
if m:
|
| 51 |
+
num = len(re.findall(p, sen))
|
| 52 |
+
if num > 1:
|
| 53 |
+
match['>1'] += 1
|
| 54 |
+
else:
|
| 55 |
+
match['1'] += 1
|
| 56 |
+
return m.span('v'), num
|
| 57 |
+
match['0'] += 1
|
| 58 |
+
return (None, None), 0
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
def sys_intent():
|
| 62 |
+
"""from original data README.md"""
|
| 63 |
+
return {
|
| 64 |
+
"inform": {"description": "Inform the value for a slot to the user."},
|
| 65 |
+
"request": {"description": "Request the value of a slot from the user."},
|
| 66 |
+
"confirm": {"description": "Confirm the value of a slot before making a transactional service call."},
|
| 67 |
+
"offer": {"description": "Offer a certain value for a slot to the user."},
|
| 68 |
+
"notify_success": {"description": "Inform the user that their request was successful."},
|
| 69 |
+
"notify_failure": {"description": "Inform the user that their request failed."},
|
| 70 |
+
"inform_count": {"description": "Inform the number of items found that satisfy the user's request."},
|
| 71 |
+
"offer_intent": {"description": "Offer a new intent to the user."},
|
| 72 |
+
"req_more": {"description": "Asking the user if they need anything else."},
|
| 73 |
+
"goodbye": {"description": "End the dialogue."},
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def usr_intent():
|
| 78 |
+
"""from original data README.md"""
|
| 79 |
+
return {
|
| 80 |
+
"inform_intent": {"description": "Express the desire to perform a certain task to the system."},
|
| 81 |
+
"negate_intent": {"description": "Negate the intent which has been offered by the system."},
|
| 82 |
+
"affirm_intent": {"description": "Agree to the intent which has been offered by the system."},
|
| 83 |
+
"inform": {"description": "Inform the value of a slot to the system."},
|
| 84 |
+
"request": {"description": "Request the value of a slot from the system."},
|
| 85 |
+
"affirm": {"description": "Agree to the system's proposition. "},
|
| 86 |
+
"negate": {"description": "Deny the system's proposal."},
|
| 87 |
+
"select": {"description": "Select a result being offered by the system."},
|
| 88 |
+
"request_alts": {"description": "Ask for more results besides the ones offered by the system."},
|
| 89 |
+
"thank_you": {"description": "Thank the system."},
|
| 90 |
+
"goodbye": {"description": "End the dialogue."},
|
| 91 |
+
}
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def get_intent():
|
| 95 |
+
"""merge sys & usr intent"""
|
| 96 |
+
return {
|
| 97 |
+
"inform": {"description": "Inform the value for a slot."},
|
| 98 |
+
"request": {"description": "Request the value of a slot."},
|
| 99 |
+
"confirm": {"description": "Confirm the value of a slot before making a transactional service call."},
|
| 100 |
+
"offer": {"description": "Offer a certain value for a slot to the user."},
|
| 101 |
+
"notify_success": {"description": "Inform the user that their request was successful."},
|
| 102 |
+
"notify_failure": {"description": "Inform the user that their request failed."},
|
| 103 |
+
"inform_count": {"description": "Inform the number of items found that satisfy the user's request."},
|
| 104 |
+
"offer_intent": {"description": "Offer a new intent to the user."},
|
| 105 |
+
"req_more": {"description": "Asking the user if they need anything else."},
|
| 106 |
+
"goodbye": {"description": "End the dialogue."},
|
| 107 |
+
"inform_intent": {"description": "Express the desire to perform a certain task to the system."},
|
| 108 |
+
"negate_intent": {"description": "Negate the intent which has been offered by the system."},
|
| 109 |
+
"affirm_intent": {"description": "Agree to the intent which has been offered by the system."},
|
| 110 |
+
"affirm": {"description": "Agree to the system's proposition. "},
|
| 111 |
+
"negate": {"description": "Deny the system's proposal."},
|
| 112 |
+
"select": {"description": "Select a result being offered by the system."},
|
| 113 |
+
"request_alts": {"description": "Ask for more results besides the ones offered by the system."},
|
| 114 |
+
"thank_you": {"description": "Thank the system."},
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def preprocess():
|
| 119 |
+
original_data_dir = 'dstc8-schema-guided-dialogue-master'
|
| 120 |
+
new_data_dir = 'data'
|
| 121 |
+
|
| 122 |
+
if not os.path.exists(original_data_dir):
|
| 123 |
+
original_data_zip = 'dstc8-schema-guided-dialogue-master.zip'
|
| 124 |
+
if not os.path.exists(original_data_zip):
|
| 125 |
+
raise FileNotFoundError(f'cannot find original data {original_data_zip} in sgd/, should manually download dstc8-schema-guided-dialogue-master.zip from https://github.com/google-research-datasets/dstc8-schema-guided-dialogue')
|
| 126 |
+
else:
|
| 127 |
+
archive = ZipFile(original_data_zip)
|
| 128 |
+
archive.extractall()
|
| 129 |
+
|
| 130 |
+
os.makedirs(new_data_dir, exist_ok=True)
|
| 131 |
+
|
| 132 |
+
dialogues = []
|
| 133 |
+
ontology = {'domains': {},
|
| 134 |
+
'intents': get_intent(),
|
| 135 |
+
'state': {},
|
| 136 |
+
'dialogue_acts': {
|
| 137 |
+
"categorical": {},
|
| 138 |
+
"non-categorical": {},
|
| 139 |
+
"binary": {}
|
| 140 |
+
}}
|
| 141 |
+
splits = ['train', 'validation', 'test']
|
| 142 |
+
dataset_name = 'sgd'
|
| 143 |
+
for data_split in splits:
|
| 144 |
+
data_dir = os.path.join(original_data_dir, data_split if data_split != 'validation' else 'dev')
|
| 145 |
+
# schema => ontology
|
| 146 |
+
with open(os.path.join(data_dir, 'schema.json')) as f:
|
| 147 |
+
data = json.load(f)
|
| 148 |
+
for schema in data:
|
| 149 |
+
domain = schema['service_name']
|
| 150 |
+
ontology['domains'].setdefault(domain, {})
|
| 151 |
+
ontology['domains'][domain]['description'] = schema['description']
|
| 152 |
+
ontology['domains'][domain].setdefault('slots', {})
|
| 153 |
+
ontology['domains'][domain]['active_intents'] = schema['intents']
|
| 154 |
+
ontology['state'].setdefault(domain, {})
|
| 155 |
+
for slot in schema['slots']:
|
| 156 |
+
ontology['domains'][domain]['slots'][slot['name']] = {
|
| 157 |
+
"description": slot['description'],
|
| 158 |
+
"is_categorical": slot['is_categorical'],
|
| 159 |
+
"possible_values": slot['possible_values']
|
| 160 |
+
}
|
| 161 |
+
ontology['state'][domain][slot['name']] = ''
|
| 162 |
+
# add 'count' slot
|
| 163 |
+
ontology['domains'][domain]['slots']['count'] = {
|
| 164 |
+
"description": "the number of items found that satisfy the user's request.",
|
| 165 |
+
"is_categorical": False,
|
| 166 |
+
"possible_values": []
|
| 167 |
+
}
|
| 168 |
+
|
| 169 |
+
# dialog
|
| 170 |
+
cnt = 0
|
| 171 |
+
for root, dirs, files in os.walk(data_dir):
|
| 172 |
+
fs = sorted([x for x in files if 'dialogues' in x])
|
| 173 |
+
for f in tqdm(fs, desc='processing schema-guided-{}'.format(data_split)):
|
| 174 |
+
data = json.load(open(os.path.join(data_dir, f)))
|
| 175 |
+
for d in data:
|
| 176 |
+
dialogue = {
|
| 177 |
+
"dataset": dataset_name,
|
| 178 |
+
"data_split": data_split,
|
| 179 |
+
"dialogue_id": f'{dataset_name}-{data_split}-{cnt}',
|
| 180 |
+
"original_id": d['dialogue_id'],
|
| 181 |
+
"domains": d['services'],
|
| 182 |
+
"goal": { # no goal
|
| 183 |
+
'description': '',
|
| 184 |
+
'inform': {},
|
| 185 |
+
'request': {}
|
| 186 |
+
},
|
| 187 |
+
"turns": []
|
| 188 |
+
}
|
| 189 |
+
cnt += 1
|
| 190 |
+
init_state = {}
|
| 191 |
+
for domain in dialogue['domains']:
|
| 192 |
+
init_state.setdefault(domain, deepcopy(ontology['state'][domain]))
|
| 193 |
+
|
| 194 |
+
for utt_idx, t in enumerate(d['turns']):
|
| 195 |
+
speaker = t['speaker'].lower()
|
| 196 |
+
turn = {
|
| 197 |
+
'speaker': speaker,
|
| 198 |
+
'utterance': t['utterance'],
|
| 199 |
+
'utt_idx': utt_idx,
|
| 200 |
+
'dialogue_acts': {
|
| 201 |
+
'binary': [],
|
| 202 |
+
'categorical': [],
|
| 203 |
+
'non-categorical': [],
|
| 204 |
+
},
|
| 205 |
+
}
|
| 206 |
+
for frame in t['frames']:
|
| 207 |
+
domain = frame['service']
|
| 208 |
+
for action in frame['actions']:
|
| 209 |
+
intent = action['act'].lower() # lowercase intent
|
| 210 |
+
assert intent in ontology['intents'], [intent]
|
| 211 |
+
slot = action['slot']
|
| 212 |
+
value_list = action['values']
|
| 213 |
+
if action['act'] in ['REQ_MORE', 'AFFIRM', 'NEGATE', 'THANK_YOU', 'GOODBYE']:
|
| 214 |
+
# Slot and values are always empty
|
| 215 |
+
assert slot == "" and len(value_list) == 0
|
| 216 |
+
turn['dialogue_acts']['binary'].append({
|
| 217 |
+
"intent": intent,
|
| 218 |
+
"domain": '',
|
| 219 |
+
"slot": ''
|
| 220 |
+
})
|
| 221 |
+
elif action['act'] in ['NOTIFY_SUCCESS', 'NOTIFY_FAILURE', 'REQUEST_ALTS', 'AFFIRM_INTENT', 'NEGATE_INTENT']:
|
| 222 |
+
# Slot and values are always empty
|
| 223 |
+
assert slot == "" and len(value_list) == 0
|
| 224 |
+
turn['dialogue_acts']['binary'].append({
|
| 225 |
+
"intent": intent,
|
| 226 |
+
"domain": domain,
|
| 227 |
+
"slot": ''
|
| 228 |
+
})
|
| 229 |
+
elif action['act'] in ['OFFER_INTENT', 'INFORM_INTENT']:
|
| 230 |
+
# slot containing the intent being offered.
|
| 231 |
+
assert slot == 'intent' and len(value_list) == 1
|
| 232 |
+
turn['dialogue_acts']['binary'].append({
|
| 233 |
+
"intent": intent,
|
| 234 |
+
"domain": domain,
|
| 235 |
+
"slot": value_list[0]
|
| 236 |
+
})
|
| 237 |
+
elif action['act'] in ['REQUEST'] and len(value_list) == 0:
|
| 238 |
+
# always contains a slot, but values are optional.
|
| 239 |
+
assert slot in ontology['domains'][domain]['slots'], f'{domain}-{slot}'
|
| 240 |
+
turn['dialogue_acts']['binary'].append({
|
| 241 |
+
"intent": intent,
|
| 242 |
+
"domain": domain,
|
| 243 |
+
"slot": slot
|
| 244 |
+
})
|
| 245 |
+
elif action['act'] in ['SELECT'] and len(value_list) == 0:
|
| 246 |
+
# (slot=='' and len(value_list) == 0) or (slot!='' and len(value_list) > 0)
|
| 247 |
+
assert slot == '', f'{domain}-{slot}-{action}'
|
| 248 |
+
turn['dialogue_acts']['binary'].append({
|
| 249 |
+
"intent": intent,
|
| 250 |
+
"domain": domain,
|
| 251 |
+
"slot": slot
|
| 252 |
+
})
|
| 253 |
+
elif action['act'] in ['INFORM_COUNT']:
|
| 254 |
+
# always has "count" as the slot, and a single element in values for the number of results obtained by the system.
|
| 255 |
+
assert slot == 'count' and len(value_list) == 1
|
| 256 |
+
value = value_list[0]
|
| 257 |
+
|
| 258 |
+
turn['dialogue_acts']['non-categorical'].append({
|
| 259 |
+
"intent": intent,
|
| 260 |
+
"domain": domain,
|
| 261 |
+
"slot": slot,
|
| 262 |
+
"value": value,
|
| 263 |
+
})
|
| 264 |
+
|
| 265 |
+
# find char span
|
| 266 |
+
(start, end), num = pharse_in_sen(value, t['utterance'])
|
| 267 |
+
assert num > 0, f'{value}-{t["utterance"]}' # {1:20086, 2:341, 3:19}
|
| 268 |
+
assert value.lower() == t['utterance'][start:end].lower() \
|
| 269 |
+
or digit2word[value].lower() == t['utterance'][start:end].lower()
|
| 270 |
+
# first match is always the choice
|
| 271 |
+
turn['dialogue_acts']['non-categorical'][-1].update({
|
| 272 |
+
"value": t['utterance'][start:end], "start": start, "end": end
|
| 273 |
+
})
|
| 274 |
+
else:
|
| 275 |
+
# have slot & value
|
| 276 |
+
if ontology['domains'][domain]['slots'][slot]['is_categorical']:
|
| 277 |
+
possible_values = [value.lower() for value in ontology['domains'][domain]['slots'][slot]['possible_values']]
|
| 278 |
+
for value in value_list:
|
| 279 |
+
if value.lower() not in possible_values and value != 'dontcare':
|
| 280 |
+
ontology['domains'][domain]['slots'][slot]['possible_values'].append(value)
|
| 281 |
+
print(f'add value to ontology\t{domain}-{slot}-{value}', possible_values)
|
| 282 |
+
turn['dialogue_acts']['categorical'].append({
|
| 283 |
+
"intent": intent,
|
| 284 |
+
"domain": domain,
|
| 285 |
+
"slot": slot,
|
| 286 |
+
"value": value,
|
| 287 |
+
})
|
| 288 |
+
else:
|
| 289 |
+
# span info in frame['slots']
|
| 290 |
+
for value in value_list:
|
| 291 |
+
for slot_info in frame['slots']:
|
| 292 |
+
start = slot_info['start']
|
| 293 |
+
end = slot_info['exclusive_end']
|
| 294 |
+
if slot_info['slot'] == slot and t['utterance'][start:end].lower() == value.lower():
|
| 295 |
+
assert t['utterance'][start:end] == value, f'{action}-{slot_info}-{t["utterance"][start:end]}'
|
| 296 |
+
turn['dialogue_acts']['non-categorical'].append({
|
| 297 |
+
"intent": intent,
|
| 298 |
+
"domain": domain,
|
| 299 |
+
"slot": slot,
|
| 300 |
+
"value": value,
|
| 301 |
+
"start": start,
|
| 302 |
+
"end": end
|
| 303 |
+
})
|
| 304 |
+
break
|
| 305 |
+
else:
|
| 306 |
+
assert value == 'dontcare', f'{action}-{slot_info}'
|
| 307 |
+
|
| 308 |
+
if speaker == 'user':
|
| 309 |
+
state = deepcopy(init_state)
|
| 310 |
+
active_intent = {}
|
| 311 |
+
requested_slots = {}
|
| 312 |
+
for frame in t['frames']:
|
| 313 |
+
domain = frame['service']
|
| 314 |
+
active_intent[domain] = frame['state']['active_intent']
|
| 315 |
+
requested_slots[domain] = frame['state']['requested_slots']
|
| 316 |
+
for slot, value_list in frame['state']['slot_values'].items():
|
| 317 |
+
state[domain][slot] = value_list[0]
|
| 318 |
+
for value in value_list[1:]:
|
| 319 |
+
state[domain][slot] += '|' + value
|
| 320 |
+
turn['state'] = state
|
| 321 |
+
turn['active_intent'] = active_intent
|
| 322 |
+
turn['requested_slots'] = requested_slots
|
| 323 |
+
else:
|
| 324 |
+
# service_call and service_results
|
| 325 |
+
turn['service_call'] = {}
|
| 326 |
+
turn['db_results'] = {}
|
| 327 |
+
for frame in t['frames']:
|
| 328 |
+
if 'service_call' not in frame:
|
| 329 |
+
continue
|
| 330 |
+
domain = frame['service']
|
| 331 |
+
turn['service_call'][domain] = frame['service_call']
|
| 332 |
+
turn['db_results'][domain] = frame['service_results']
|
| 333 |
+
|
| 334 |
+
# add to dialogue_acts dictionary in the ontology
|
| 335 |
+
for da_type in turn['dialogue_acts']:
|
| 336 |
+
das = turn['dialogue_acts'][da_type]
|
| 337 |
+
for da in das:
|
| 338 |
+
ontology["dialogue_acts"][da_type].setdefault((da['intent'], da['domain'], da['slot']), {})
|
| 339 |
+
ontology["dialogue_acts"][da_type][(da['intent'], da['domain'], da['slot'])][speaker] = True
|
| 340 |
+
dialogue['turns'].append(turn)
|
| 341 |
+
dialogues.append(dialogue)
|
| 342 |
+
|
| 343 |
+
for da_type in ontology['dialogue_acts']:
|
| 344 |
+
ontology["dialogue_acts"][da_type] = sorted([str({'user': speakers.get('user', False), 'system': speakers.get('system', False), 'intent':da[0],'domain':da[1], 'slot':da[2]}) for da, speakers in ontology["dialogue_acts"][da_type].items()])
|
| 345 |
+
json.dump(dialogues[:10], open(f'dummy_data.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
| 346 |
+
json.dump(ontology, open(f'{new_data_dir}/ontology.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
| 347 |
+
json.dump(dialogues, open(f'{new_data_dir}/dialogues.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False)
|
| 348 |
+
with ZipFile('data.zip', 'w', ZIP_DEFLATED) as zf:
|
| 349 |
+
for filename in os.listdir(new_data_dir):
|
| 350 |
+
zf.write(f'{new_data_dir}/{filename}')
|
| 351 |
+
rmtree(original_data_dir)
|
| 352 |
+
rmtree(new_data_dir)
|
| 353 |
+
return dialogues, ontology
|
| 354 |
+
|
| 355 |
+
if __name__ == '__main__':
|
| 356 |
+
preprocess()
|