| | import os |
| | from utils import read_json_file, write_jsonl_file, parse |
| |
|
| |
|
| | def get_actions_goal(actions): |
| | goal = [] |
| | for item in actions: |
| | g = { |
| | "intent": item["act"], |
| | "slot_value_table": [ |
| | { |
| | "slot": item["slot"], |
| | "value": item["values"][0] if len(item["values"]) else None, |
| | } |
| | ], |
| | } |
| | goal.append(g) |
| | return goal |
| |
|
| |
|
| | def get_state_goal(state): |
| | goal = [{"intent": state["active_intent"], "slot_value_table": []}] |
| | for slot, value in state["slot_values"].items(): |
| | svt = {"slot": slot, "value": value[0]} |
| | goal[0]["slot_value_table"].append(svt) |
| | return goal |
| |
|
| |
|
| | def get_call_goal(call): |
| | goal = [{"intent": call["method"], "slot_value_table": []}] |
| | for slot, value in call["parameters"].items(): |
| | svt = {"slot": slot, "value": value} |
| | goal[0]["slot_value_table"].append(svt) |
| | return goal |
| |
|
| |
|
| | def get_goal(domain_name, content): |
| | goal_func = { |
| | "actions": get_actions_goal, |
| | "state": get_state_goal, |
| | "service_call": get_call_goal, |
| | } |
| | return goal_func[domain_name](content) |
| |
|
| |
|
| | def get_belief_state(frame): |
| | bs = [] |
| | for domain in ["actions", "state", "service_call"]: |
| | if domain in frame: |
| | blf_stt = {"domain": domain, "goal": get_goal(domain, frame[domain])} |
| |
|
| | bs.append(blf_stt) |
| |
|
| | return bs |
| |
|
| |
|
| | def get_querying_result(frame): |
| | if "service_results" not in frame: |
| | return [] |
| | else: |
| | return frame["service_results"] |
| |
|
| |
|
| | def parse_slots_index(slots): |
| | slot2index = dict() |
| | for slot in slots: |
| | |
| | |
| | |
| | |
| | |
| | slot2index[slot["slot"]] = { |
| | "start": slot["start"], |
| | "end": slot["exclusive_end"], |
| | } |
| | return slot2index |
| |
|
| |
|
| | def parse_belief_state(frames): |
| | intent_domain_bs = dict() |
| |
|
| | for frame in frames: |
| | slot2index = parse_slots_index(frame["slots"]) |
| | domain = frame["service"] |
| | state = frame["state"] |
| | intent = state["active_intent"] |
| |
|
| | if intent not in intent_domain_bs: |
| | intent_domain_bs[intent] = dict() |
| |
|
| | if domain not in intent_domain_bs[intent]: |
| | intent_domain_bs[intent][domain] = { |
| | "svt": [], |
| | } |
| |
|
| | for slot in state["slot_values"]: |
| | svp = { |
| | "slot": slot, |
| | "value": state["slot_values"][slot], |
| | "relation": "equal_to", |
| | } |
| |
|
| | if slot in slot2index: |
| | svp["start"] = slot2index[slot]["start"] |
| | svp["end"] = slot2index[slot]["end"] |
| |
|
| | intent_domain_bs[intent][domain]["svt"].append(svp) |
| | intent_domain_bs[intent][domain]["requested_slots"] = state["requested_slots"] |
| |
|
| | bs = [] |
| | for intent in intent_domain_bs: |
| | for domain in intent_domain_bs[intent]: |
| | bs.append( |
| | { |
| | "intent": intent, |
| | "domain": domain, |
| | "requested_slots": intent_domain_bs[intent][domain][ |
| | "requested_slots" |
| | ], |
| | "slot_value_table": intent_domain_bs[intent][domain]["svt"], |
| | } |
| | ) |
| |
|
| | return bs |
| |
|
| |
|
| | def parse_dialog_act(frames): |
| | domain_act_svt = dict() |
| |
|
| | for frame in frames: |
| | slot2index = parse_slots_index(frame["slots"]) |
| | domain = frame["service"] |
| | actions = frame["actions"] |
| |
|
| | if domain not in domain_act_svt: |
| | domain_act_svt[domain] = dict() |
| |
|
| | for action in actions: |
| | act = action["act"] |
| | if act not in domain_act_svt[domain]: |
| | domain_act_svt[domain][act] = [] |
| |
|
| | svp = { |
| | "slot": action["slot"], |
| | "value": action["values"], |
| | "relation": "equal_to", |
| | } |
| |
|
| | if "canonical_values" in action: |
| | svp["canonical_values"] = (action["canonical_values"],) |
| |
|
| | slot = action["slot"] |
| | if slot in slot2index: |
| | svp["start"] = slot2index[slot]["start"] |
| | svp["end"] = slot2index[slot]["end"] |
| |
|
| | domain_act_svt[domain][act].append(svp) |
| |
|
| | da = [] |
| | for domain in domain_act_svt: |
| | for act in domain_act_svt[domain]: |
| | da.append( |
| | { |
| | "act": act, |
| | "domain": domain, |
| | "slot_value_table": domain_act_svt[domain][act], |
| | } |
| | ) |
| |
|
| | return da |
| |
|
| |
|
| | def preprocess(args): |
| | filenames = os.listdir(args.input_dir) |
| | data = {"train": [], "dev": [], "test": []} |
| |
|
| | for filename in filenames: |
| | if not filename.endswith(".json"): |
| | continue |
| | path = os.path.join(args.input_dir, filename) |
| | origin_data = read_json_file(path) |
| |
|
| | locale = filename.split("_")[0] |
| | partition = filename.split("_")[1][:-5] |
| |
|
| | for dial in origin_data: |
| | parsed_dial = { |
| | "turn": "multi", |
| | "domain": dial["services"], |
| | "locale": locale, |
| | "dialog": [], |
| | } |
| |
|
| | for origin_turn in dial["turns"]: |
| | frames = origin_turn["frames"] |
| |
|
| | |
| |
|
| | turn = { |
| | "role": origin_turn["speaker"], |
| | "utterance": origin_turn["utterance"], |
| | "dialog_act": parse_dialog_act(frames), |
| | } |
| |
|
| | if turn["role"] == "USER": |
| | turn["belief_state"] = parse_belief_state(frames) |
| |
|
| | if "service_call" in origin_turn: |
| | turn["query"] = origin_turn["service_call"] |
| | if "service_results" in origin_turn: |
| | turn["querying_result"] = origin_turn["querying_result"] |
| |
|
| | parsed_dial["dialog"].append(turn) |
| |
|
| | data[partition].append(parsed_dial) |
| |
|
| | 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) |
| |
|