|
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: |
|
|
|
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) |
|
|