|
from zipfile import ZipFile, ZIP_DEFLATED |
|
import json |
|
import os |
|
from pprint import pprint |
|
from copy import deepcopy |
|
from collections import Counter |
|
from tqdm import tqdm |
|
from shutil import rmtree |
|
import re |
|
|
|
digit2word = { |
|
'0': 'zero', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', |
|
'6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', '10': 'ten' |
|
} |
|
|
|
match = { |
|
'0': 0, |
|
'1': 0, |
|
'>1': 0, |
|
} |
|
|
|
|
|
def pharse_in_sen(phrase, sen): |
|
''' |
|
match value in the sentence |
|
:param phrase: str |
|
:param sen: str |
|
:return: start, end if matched, else None, None |
|
''' |
|
assert isinstance(phrase, str) |
|
pw = '(^|[\s,\.:\?!-])(?P<v>{})([\s,\.:\?!-]|$)' |
|
pn = '(^|[\s\?!-]|\D[,\.:])(?P<v>{})($|[\s\?!-]|[,\.:]\D|[,\.:]$)' |
|
if phrase.isdigit(): |
|
pattern = pn |
|
else: |
|
pattern = pw |
|
p = re.compile(pattern.format(re.escape(phrase)), re.I) |
|
m = re.search(p, sen) |
|
if m: |
|
num = len(re.findall(p, sen)) |
|
if num > 1: |
|
match['>1'] += 1 |
|
else: |
|
match['1'] += 1 |
|
return m.span('v'), num |
|
if phrase.isdigit() and phrase in digit2word: |
|
phrase = digit2word[phrase] |
|
p = re.compile(pw.format(re.escape(phrase)), re.I) |
|
m = re.search(p, sen) |
|
if m: |
|
num = len(re.findall(p, sen)) |
|
if num > 1: |
|
match['>1'] += 1 |
|
else: |
|
match['1'] += 1 |
|
return m.span('v'), num |
|
match['0'] += 1 |
|
return (None, None), 0 |
|
|
|
|
|
def sys_intent(): |
|
"""from original data README.md""" |
|
return { |
|
"inform": {"description": "Inform the value for a slot to the user."}, |
|
"request": {"description": "Request the value of a slot from the user."}, |
|
"confirm": {"description": "Confirm the value of a slot before making a transactional service call."}, |
|
"offer": {"description": "Offer a certain value for a slot to the user."}, |
|
"notify_success": {"description": "Inform the user that their request was successful."}, |
|
"notify_failure": {"description": "Inform the user that their request failed."}, |
|
"inform_count": {"description": "Inform the number of items found that satisfy the user's request."}, |
|
"offer_intent": {"description": "Offer a new intent to the user."}, |
|
"req_more": {"description": "Asking the user if they need anything else."}, |
|
"goodbye": {"description": "End the dialogue."}, |
|
} |
|
|
|
|
|
def usr_intent(): |
|
"""from original data README.md""" |
|
return { |
|
"inform_intent": {"description": "Express the desire to perform a certain task to the system."}, |
|
"negate_intent": {"description": "Negate the intent which has been offered by the system."}, |
|
"affirm_intent": {"description": "Agree to the intent which has been offered by the system."}, |
|
"inform": {"description": "Inform the value of a slot to the system."}, |
|
"request": {"description": "Request the value of a slot from the system."}, |
|
"affirm": {"description": "Agree to the system's proposition. "}, |
|
"negate": {"description": "Deny the system's proposal."}, |
|
"select": {"description": "Select a result being offered by the system."}, |
|
"request_alts": {"description": "Ask for more results besides the ones offered by the system."}, |
|
"thank_you": {"description": "Thank the system."}, |
|
"goodbye": {"description": "End the dialogue."}, |
|
} |
|
|
|
|
|
def get_intent(): |
|
"""merge sys & usr intent""" |
|
return { |
|
"inform": {"description": "Inform the value for a slot."}, |
|
"request": {"description": "Request the value of a slot."}, |
|
"confirm": {"description": "Confirm the value of a slot before making a transactional service call."}, |
|
"offer": {"description": "Offer a certain value for a slot to the user."}, |
|
"notify_success": {"description": "Inform the user that their request was successful."}, |
|
"notify_failure": {"description": "Inform the user that their request failed."}, |
|
"inform_count": {"description": "Inform the number of items found that satisfy the user's request."}, |
|
"offer_intent": {"description": "Offer a new intent to the user."}, |
|
"req_more": {"description": "Asking the user if they need anything else."}, |
|
"goodbye": {"description": "End the dialogue."}, |
|
"inform_intent": {"description": "Express the desire to perform a certain task to the system."}, |
|
"negate_intent": {"description": "Negate the intent which has been offered by the system."}, |
|
"affirm_intent": {"description": "Agree to the intent which has been offered by the system."}, |
|
"affirm": {"description": "Agree to the system's proposition. "}, |
|
"negate": {"description": "Deny the system's proposal."}, |
|
"select": {"description": "Select a result being offered by the system."}, |
|
"request_alts": {"description": "Ask for more results besides the ones offered by the system."}, |
|
"thank_you": {"description": "Thank the system."}, |
|
} |
|
|
|
|
|
def preprocess(): |
|
original_data_dir = 'dstc8-schema-guided-dialogue-master/sgd_x/data/v2' |
|
dataset_name = 'sgd2' |
|
new_data_dir = 'data' |
|
|
|
if not os.path.exists(original_data_dir): |
|
raise FileNotFoundError(f'cannot find original data {original_data_dir}, should manually download dstc8-schema-guided-dialogue-master.zip from https://github.com/google-research-datasets/dstc8-schema-guided-dialogue and run `python3 -m sgd_x.generate_sgdx_dialogues`') |
|
|
|
os.makedirs(new_data_dir, exist_ok=True) |
|
|
|
dialogues = [] |
|
ontology = {'domains': {}, |
|
'intents': get_intent(), |
|
'state': {}, |
|
'dialogue_acts': { |
|
"categorical": {}, |
|
"non-categorical": {}, |
|
"binary": {} |
|
}} |
|
splits = ['train', 'validation', 'test'] |
|
for data_split in splits: |
|
data_dir = os.path.join(original_data_dir, data_split if data_split != 'validation' else 'dev') |
|
|
|
with open(os.path.join(data_dir, 'schema.json')) as f: |
|
data = json.load(f) |
|
for schema in data: |
|
domain = schema['service_name'] |
|
ontology['domains'].setdefault(domain, {}) |
|
ontology['domains'][domain]['description'] = schema['description'] |
|
ontology['domains'][domain].setdefault('slots', {}) |
|
ontology['domains'][domain]['active_intents'] = schema['intents'] |
|
ontology['state'].setdefault(domain, {}) |
|
for slot in schema['slots']: |
|
ontology['domains'][domain]['slots'][slot['name']] = { |
|
"description": slot['description'], |
|
"is_categorical": slot['is_categorical'], |
|
"possible_values": slot['possible_values'] |
|
} |
|
ontology['state'][domain][slot['name']] = '' |
|
|
|
ontology['domains'][domain]['slots']['count'] = { |
|
"description": "the number of items found that satisfy the user's request.", |
|
"is_categorical": False, |
|
"possible_values": [] |
|
} |
|
|
|
|
|
cnt = 0 |
|
for root, dirs, files in os.walk(data_dir): |
|
fs = sorted([x for x in files if 'dialogues' in x]) |
|
for f in tqdm(fs, desc='processing schema-guided-{}'.format(data_split)): |
|
data = json.load(open(os.path.join(data_dir, f))) |
|
for d in data: |
|
dialogue = { |
|
"dataset": dataset_name, |
|
"data_split": data_split, |
|
"dialogue_id": f'{dataset_name}-{data_split}-{cnt}', |
|
"original_id": d['dialogue_id'], |
|
"domains": d['services'], |
|
"goal": { |
|
'description': '', |
|
'inform': {}, |
|
'request': {} |
|
}, |
|
"turns": [] |
|
} |
|
cnt += 1 |
|
prev_state = {} |
|
for domain in dialogue['domains']: |
|
prev_state.setdefault(domain, deepcopy(ontology['state'][domain])) |
|
|
|
for utt_idx, t in enumerate(d['turns']): |
|
speaker = t['speaker'].lower() |
|
turn = { |
|
'speaker': speaker, |
|
'utterance': t['utterance'], |
|
'utt_idx': utt_idx, |
|
'dialogue_acts': { |
|
'binary': [], |
|
'categorical': [], |
|
'non-categorical': [], |
|
}, |
|
} |
|
for frame in t['frames']: |
|
domain = frame['service'] |
|
for action in frame['actions']: |
|
intent = action['act'].lower() |
|
assert intent in ontology['intents'], [intent] |
|
slot = action['slot'] |
|
value_list = action['values'] |
|
if action['act'] in ['REQ_MORE', 'AFFIRM', 'NEGATE', 'THANK_YOU', 'GOODBYE']: |
|
|
|
assert slot == "" and len(value_list) == 0 |
|
turn['dialogue_acts']['binary'].append({ |
|
"intent": intent, |
|
"domain": '', |
|
"slot": '' |
|
}) |
|
elif action['act'] in ['NOTIFY_SUCCESS', 'NOTIFY_FAILURE', 'REQUEST_ALTS', 'AFFIRM_INTENT', 'NEGATE_INTENT']: |
|
|
|
assert slot == "" and len(value_list) == 0 |
|
turn['dialogue_acts']['binary'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": '' |
|
}) |
|
elif action['act'] in ['OFFER_INTENT', 'INFORM_INTENT']: |
|
|
|
assert slot == 'intent' and len(value_list) == 1, print(slot, action, d['dialogue_id'], utt_idx) |
|
turn['dialogue_acts']['binary'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": value_list[0] |
|
}) |
|
elif action['act'] in ['REQUEST'] and len(value_list) == 0: |
|
|
|
assert slot in ontology['domains'][domain]['slots'], f'{domain}-{slot}' |
|
turn['dialogue_acts']['binary'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": slot |
|
}) |
|
elif action['act'] in ['SELECT'] and len(value_list) == 0: |
|
|
|
assert slot == '', f'{domain}-{slot}-{action}' |
|
turn['dialogue_acts']['binary'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": slot |
|
}) |
|
elif action['act'] in ['INFORM_COUNT']: |
|
|
|
assert slot == 'count' and len(value_list) == 1 |
|
value = value_list[0] |
|
|
|
turn['dialogue_acts']['non-categorical'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": slot, |
|
"value": value, |
|
}) |
|
|
|
|
|
(start, end), num = pharse_in_sen(value, t['utterance']) |
|
assert num > 0, f'{value}-{t["utterance"]}' |
|
assert value.lower() == t['utterance'][start:end].lower() \ |
|
or digit2word[value].lower() == t['utterance'][start:end].lower() |
|
|
|
turn['dialogue_acts']['non-categorical'][-1].update({ |
|
"value": t['utterance'][start:end], "start": start, "end": end |
|
}) |
|
else: |
|
|
|
assert domain in ontology['domains'], print(ontology['domains']) |
|
assert slot in ontology['domains'][domain]['slots'], print(slot, action, d['dialogue_id'], utt_idx) |
|
if ontology['domains'][domain]['slots'][slot]['is_categorical']: |
|
possible_values = [value.lower() for value in ontology['domains'][domain]['slots'][slot]['possible_values']] |
|
for value in value_list: |
|
if value.lower() not in possible_values and value != 'dontcare': |
|
ontology['domains'][domain]['slots'][slot]['possible_values'].append(value) |
|
print(f'add value to ontology\t{domain}-{slot}-{value}', possible_values) |
|
turn['dialogue_acts']['categorical'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": slot, |
|
"value": value, |
|
}) |
|
else: |
|
|
|
for value in value_list: |
|
for slot_info in frame['slots']: |
|
start = slot_info['start'] |
|
end = slot_info['exclusive_end'] |
|
if slot_info['slot'] == slot and t['utterance'][start:end].lower() == value.lower(): |
|
assert t['utterance'][start:end] == value, f'{action}-{slot_info}-{t["utterance"][start:end]}' |
|
turn['dialogue_acts']['non-categorical'].append({ |
|
"intent": intent, |
|
"domain": domain, |
|
"slot": slot, |
|
"value": value, |
|
"start": start, |
|
"end": end |
|
}) |
|
break |
|
else: |
|
assert value == 'dontcare', f'{action}-{slot_info}' |
|
|
|
if speaker == 'user': |
|
state = deepcopy(prev_state) |
|
active_intent = {} |
|
requested_slots = {} |
|
for frame in t['frames']: |
|
domain = frame['service'] |
|
active_intent[domain] = frame['state']['active_intent'] |
|
requested_slots[domain] = frame['state']['requested_slots'] |
|
for slot in state[domain]: |
|
if slot in frame['state']['slot_values']: |
|
value_list = frame['state']['slot_values'][slot] |
|
state[domain][slot] = value_list[0] |
|
for value in value_list[1:]: |
|
state[domain][slot] += '|' + value |
|
else: |
|
state[domain][slot] = '' |
|
prev_state = state |
|
turn['state'] = state |
|
turn['active_intent'] = active_intent |
|
turn['requested_slots'] = requested_slots |
|
else: |
|
|
|
turn['service_call'] = {} |
|
turn['db_results'] = {} |
|
for frame in t['frames']: |
|
if 'service_call' not in frame: |
|
continue |
|
domain = frame['service'] |
|
turn['service_call'][domain] = frame['service_call'] |
|
turn['db_results'][domain] = frame['service_results'] |
|
|
|
|
|
for da_type in turn['dialogue_acts']: |
|
das = turn['dialogue_acts'][da_type] |
|
for da in das: |
|
ontology["dialogue_acts"][da_type].setdefault((da['intent'], da['domain'], da['slot']), {}) |
|
ontology["dialogue_acts"][da_type][(da['intent'], da['domain'], da['slot'])][speaker] = True |
|
dialogue['turns'].append(turn) |
|
dialogues.append(dialogue) |
|
|
|
for da_type in ontology['dialogue_acts']: |
|
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()]) |
|
json.dump(dialogues[:10], open(f'dummy_data.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False) |
|
json.dump(ontology, open(f'{new_data_dir}/ontology.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False) |
|
json.dump(dialogues, open(f'{new_data_dir}/dialogues.json', 'w', encoding='utf-8'), indent=2, ensure_ascii=False) |
|
with ZipFile('data.zip', 'w', ZIP_DEFLATED) as zf: |
|
for filename in os.listdir(new_data_dir): |
|
zf.write(f'{new_data_dir}/{filename}') |
|
|
|
rmtree(new_data_dir) |
|
return dialogues, ontology |
|
|
|
if __name__ == '__main__': |
|
preprocess() |