| from utils import parse, read_json_file, write_jsonl_file, write_json_file |
| import os |
|
|
|
|
| def parse_dialogue_act(dialogue_acts, utterance, domain): |
| parsed_dialogue_acts = [] |
| for da in dialogue_acts: |
| svt = [] |
|
|
| for slot, value in da["slots"]: |
| value = str(value) |
| |
| if slot == "slot": |
| svt.append({"slot": value}) |
| else: |
| if value in utterance: |
| start = utterance.index(value) |
| end = start + len(value) |
| else: |
| start = -1 |
| end = -1 |
| svt.append( |
| { |
| "slot": slot, |
| "values": [ |
| { |
| "value": value, |
| "start": start, |
| "end": end, |
| } |
| ], |
| "relation": "=", |
| } |
| ) |
|
|
| dialogue_act = { |
| "act": da["act"], |
| "slot_value_table": svt, |
| "domain": domain, |
| } |
|
|
| parsed_dialogue_acts.append(dialogue_act) |
|
|
| return parsed_dialogue_acts |
|
|
|
|
| def preprocess( |
| input_dir, |
| output_dir, |
| split, |
| domain="restaurant", |
| intent="FindRestaurants", |
| write=True, |
| ): |
| processed_data = [] |
|
|
| schema = { |
| domain: set(), |
| } |
|
|
| with open(os.path.join(input_dir, f"scripts/config/{split}.flist"), "r") as reader: |
| for example_dir_name in reader: |
| example_dir = os.path.join(input_dir, "data", example_dir_name.strip()) |
| data = read_json_file(os.path.join(example_dir, "log.json")) |
| label = read_json_file(os.path.join(example_dir, "label.json")) |
|
|
| dialog = {"turn": "multi", "locale": "en", "dialog": []} |
|
|
| for turn_idx, turn_data in enumerate(data["turns"]): |
| |
| utterance = turn_data["output"]["transcript"] |
| dialog["dialog"].append( |
| { |
| "roles": ["SYSTEM"], |
| "utterance": utterance, |
| "dialogue_acts": parse_dialogue_act( |
| turn_data["output"]["dialog-acts"], utterance, domain |
| ), |
| } |
| ) |
|
|
| |
| label_turn = label["turns"][turn_idx] |
| |
| |
| utterance = turn_data["input"]["live"]["asr-hyps"][0]["asr-hyp"] |
| dialog["dialog"].append( |
| { |
| "roles": ["USER"], |
| "utterance": utterance, |
| "belief_state": [ |
| { |
| "intent": intent, |
| "informed_slot_value_table": [], |
| "requested_slots": label_turn["requested-slots"], |
| "domain": domain, |
| } |
| ], |
| "dialogue_acts": parse_dialogue_act( |
| label_turn["semantics"]["json"], utterance, domain |
| ), |
| } |
| ) |
|
|
| for slot, value in label_turn["goal-labels"].items(): |
| dialog["dialog"][-1]["belief_state"][-1][ |
| "informed_slot_value_table" |
| ].append( |
| { |
| "slot": slot, |
| "values": [ |
| { |
| "value": value, |
| } |
| ], |
| "relation": "=", |
| } |
| ) |
|
|
| schema[domain].add(slot) |
|
|
| processed_data.append(dialog) |
|
|
| ontology = {domain: {slot: True for slot in schema[domain]}} |
|
|
| if write: |
| outfile = os.path.join(output_dir, f"{split}.jsonl") |
| write_jsonl_file(processed_data, outfile) |
| if "train" in split: |
| split = "train" |
| elif "dev" in split: |
| split = "dev" |
| elif "test" in split: |
| split = "test" |
| write_json_file( |
| ontology, os.path.join(args.output_dir, f"{split}_ontology.json") |
| ) |
|
|
| return processed_data, schema |
|
|
|
|
| if __name__ == "__main__": |
| args = parse() |
|
|
| preprocess(os.path.join(args.input_dir, "traindev"), args.output_dir, "dstc2_train") |
| preprocess(os.path.join(args.input_dir, "traindev"), args.output_dir, "dstc2_dev") |
| preprocess(os.path.join(args.input_dir, "test"), args.output_dir, "dstc2_test") |
|
|