File size: 4,648 Bytes
a6326c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
import os
from utils import read_json_file, parse, write_jsonl_file
def parse_domains(tasks):
domains = set()
for task in tasks:
domain: str = task["Task"].split("_")[0]
if domain.endswith("s"):
domain = domain[:-1]
domains.add(domain)
return list(domains)
def parse_belief_states(state):
belief_state = []
for intent in state:
intent_state = {"intent": intent, "slot_value_table": []}
for slot in state[intent]:
intent_state["slot_value_table"].append(
{
"slot": slot,
"relation": state[intent][slot]["relation"],
"value": state[intent][slot]["value"],
}
)
belief_state.append(intent_state)
return belief_state
def preprocess(args):
filenames = [filename for filename in os.listdir(args.input_dir)]
data = {"train": [], "dev": [], "test": [], "fewshot": []}
fewshot_dials = []
for filename in filenames:
if "fewshot" in filename:
fewshot_data = read_json_file(os.path.join(args.input_dir, filename))
fewshot_dials += fewshot_data["fewshot_dials"]
fewshot_dials = set(fewshot_dials)
for filename in filenames:
# ignore this file
if filename == "dict_en_zh.json" or "fewshot" in filename:
continue
path = os.path.join(args.input_dir, filename)
origin_data = read_json_file(path)
locale = filename[:2]
partition = filename.split("_")[1]
if partition.endswith(".json"):
partition = partition[:-5]
if partition == "valid":
partition = "dev"
for dial_id, dialog in origin_data.items():
parsed_dialog = {
"turn": "multi",
"domain": parse_domains(dialog["Scenario"]["WizardCapabilities"]),
"locale": locale,
"dialog": [],
}
last_query = None
querying_result = None
for event in dialog["Events"]:
turn = dict()
turn["role"] = event["Agent"]
if turn["role"] == "User":
turn["active_intent"] = event["active_intent"]
turn["belief_state"] = parse_belief_states(event["state"])
else:
if turn["role"] == "Wizard" and "Text" not in event:
assert last_query is None
last_query = {
"Constraints": event["Constraints"],
"API": event["API"],
}
continue
elif turn["role"] == "KnowledgeBase":
assert querying_result is None
querying_result = {
"Item": event["Item"],
"TotalItems": event["TotalItems"],
"Topic": event["Topic"],
}
continue
else:
if last_query is not None:
turn["query"] = last_query
last_query = None
turn["querying_result"] = querying_result
querying_result = None
if event["PrimaryItem"]:
turn["main_items"] = [event["PrimaryItem"]]
if event["SecondaryItem"]:
turn["main_items"].append(event["SecondaryItem"])
turn["dialog_act"] = []
Actions = event["Actions"]
das = dict()
for action in Actions:
act = action.pop("act")
if act not in das:
das[act] = []
das[act].append(action)
for act in das:
turn["dialog_act"].append(
{"act": act, "slot_value_table": das[act]}
)
turn["utterance"] = event["Text"]
parsed_dialog["dialog"].append(turn)
data[partition].append(parsed_dialog)
if dial_id in fewshot_dials:
data["fewshot"].append(parsed_dialog)
for partition in data:
if data[partition]:
write_jsonl_file(
data[partition], os.path.join(args.output_dir, f"{partition}.jsonl")
)
if __name__ == "__main__":
args = parse()
preprocess(args)
|