File size: 1,942 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 |
import os
import re
import sys
from utils import read_txt_file, write_jsonl_file, parse
def readfile(path):
data = read_txt_file(path)
return data
def get_slot_value_table(decouple):
svt = []
pattern = re.compile(r'[[](.*?)[]]', re.S)
slot_list = re.findall(pattern, decouple)
dic = {}
for item in slot_list:
slot_value = item.split(":")[-1]
pair = slot_value.strip().split(' ', 1)
dic["slot"] = pair[0]
dic["value"] = pair[-1]
svt.append(dic)
dic = {}
return svt
def preprocess(args):
filenames = os.listdir(args.input_dir)
for filename in filenames:
if len(filename) > 2:
continue
# Aiming at different path
# path = os.path.join(args.input_dir, filename)
# if not os.path.isdir(path):
# continue
# path = os.path.join(path, args.dataset + ".txt")
path = os.path.join(args.input_dir, filename, args.dataset + ".txt")
data = readfile(path)
turns = []
for line in data:
t = {}
t["turn"] = "single"
t["dialog"] = []
d = {}
elem = line.split("\t")
d["role"] = "ROLE"
d["utterance"] = elem[3]
d["slot_value_table"] = get_slot_value_table(elem[6])
d["summary"] = None
d["locale"] = elem[5][:2]
d["scenario"] = elem[4]
d["intent"] = elem[1].split(":")[1]
d["answer"] = None
t["dialog"].append(d)
t["knowledge"] = None
t["goal"] = None
turns.append(t)
write_jsonl_file(turns, os.path.join(args.output_dir, filename + '_' + args.dataset + ".jsonl"))
if __name__ == "__main__":
args = parse()
if args.dataset not in ["train", "test", "eval"]:
print("Wrong dataset type")
sys.exit(1)
preprocess(args)
|