DialogZoo / src /preprocess /CLINC150.py
AnAutomaticPencil's picture
data preprocessing update
a6326c7
from utils import read_json_file, parse, write_jsonl_file
import os
def load_intent2domain(args):
data = read_json_file(os.path.join(args.input_dir, "domains.json"))
intent2domain = {"oos": "oos"}
for domain in data:
for intent in data[domain]:
assert intent not in intent2domain
intent2domain[intent] = domain
return intent2domain
def load_sent2domain(filename, intent2domain):
data = read_json_file(filename)
sent2domain = dict()
for split in data:
for example in data[split]:
sent2domain[example[0]] = intent2domain[example[1]]
return sent2domain
def reformat(data_file, data_dir, sent2domain):
origin_data = read_json_file(data_file)
data = dict()
for split in origin_data:
if "oos" in split:
continue
data[split] = []
for example in origin_data[split]:
dial = {
"turn": "single",
"locale": "en",
"domain": [sent2domain[example[0]]]
if example[0] in sent2domain
else ["oos"],
"dialog": [
{
"roles": ["USER"],
"utterance": example[0],
"active_intents": [example[1]],
}
],
}
data[split].append(dial)
for split in data:
if "val" in split:
part = split.replace("val", "dev")
else:
part = split
outfile = os.path.join(data_dir, f"{part}.jsonl")
write_jsonl_file(data[split], outfile)
def preprocess(args):
intent2domain = load_intent2domain(args)
sent2domain = load_sent2domain(
os.path.join(args.input_dir, "data_full.json"), intent2domain
)
data_file = os.path.join(args.input_dir, "data_full.json")
# data_dir = os.path.join(args.output_dir, filename[:-5])
data_dir = args.output_dir
reformat(data_file, data_dir, sent2domain)
if __name__ == "__main__":
args = parse()
preprocess(args)