DialogZoo / src /preprocess /GlobalWoZ.py
AnAutomaticPencil's picture
data preprocessing update
a6326c7
import os
from utils import read_json_file, write_jsonl_file, parse
def get_belief_state(metadata):
bs = []
if not len(metadata):
return bs
for domain_name, domain_content in metadata.items():
blf_stt = {"domain": domain_name,
"goal": []}
if len(domain_content) > 0:
for intent_name, intent_content in domain_content.items():
goal = {"intent": intent_name,
"slot_value_table": []}
if isinstance(intent_content, list):
for item in intent_content:
svt = {"slot": intent_name,
"value": item}
goal["slot_value_table"].append(svt)
else:
for slot, value in intent_content.items():
if isinstance(value, (list, dict)) and len(value) == 0: # vacant
real_value = None
elif isinstance(value, (list, dict)) and len(value) == 1: # list or diction with one value
real_value = value[0]
elif isinstance(value, (str, bool)): # string or bool
real_value = value
svt = {"slot": slot,
"value": real_value}
goal["slot_value_table"].append(svt)
blf_stt["goal"].append(goal)
bs.append(blf_stt)
return bs
def get_slot_value_table(intent):
slot_value_table = []
if not len(intent):
return slot_value_table
for item in intent:
svt = {"slot": item[0],
"value": item[1]}
slot_value_table.append(svt)
return slot_value_table
def preprocess(args):
filenames = os.listdir(args.input_dir)
for filename in filenames:
'''
add train/eval/test instruction
'''
path = os.path.join(args.input_dir, filename)
data = read_json_file(path)
locale = filename.rsplit('.', 1)[0][-2:]
turns = []
for file_name, item in data.items():
print(file_name)
t = {"turn": "multi",
"dialog": [],
"goal": get_belief_state(item["goal"]),
"knowledge": None}
turn = 1
for obj in item["log"]:
role = "USER" if turn % 2 else "SYS"
d = {"role": role,
"utterance": obj["text"],
"belief_state": get_belief_state(obj["metadata"]),
"querying_result": None,
"summary": None,
"locale": locale,
"topic": None,
"opinion": None,
"answer": None}
if "dialog_act" in obj:
blf_stt = {"domain": "dialog_act",
"goal": []}
if len(obj["dialog_act"]) > 0:
for intent_name, intent_content in obj["dialog_act"].items():
goal = {"intent": intent_name,
"slot_value_table": get_slot_value_table(intent_content)}
blf_stt["goal"].append(goal)
d["belief_state"].append(blf_stt)
if "span_info" in obj:
blf_stt = {"domain": "span_info",
"goal": []}
if len(obj["span_info"]) > 0:
for elem in obj["span_info"]:
goal = {"intent": elem[0],
"slot_value_table": [{"slot": elem[1], "value": elem[2]}]}
blf_stt["goal"].append(goal)
d["belief_state"].append(blf_stt)
t["dialog"].append(d)
turn += 1
turns.append(t)
write_jsonl_file(turns, os.path.join(args.output_dir, filename[:-5]) + ".jsonl")
if __name__ == "__main__":
args = parse()
preprocess(args)