| import os |
| from utils import parse, read_json_file, write_jsonl_file, write_json_file |
| from typing import List |
| import shutil |
|
|
|
|
| def reformat(args, domains, split): |
| processed_data = [] |
| ontology = {} |
| for domain in domains: |
| train_files = [ |
| filename |
| for filename in os.listdir(os.path.join(args.input_dir, domain)) |
| if split in filename |
| ] |
| ontology[domain] = {} |
| for filename in sorted(train_files): |
| filepath = os.path.join(args.input_dir, domain, filename) |
| data = read_json_file(filepath) |
| for example in data: |
| processed_example = { |
| "turn": "single", |
| "locale": "en", |
| "dialog": [ |
| { |
| "roles": ["USER"], |
| "utterance": example["userInput"]["text"], |
| } |
| ], |
| } |
|
|
| if "labels" in example: |
| processed_example["dialog"][0]["belief_state"] = [ |
| { |
| "informed_slot_value_table": [], |
| "domain": domain, |
| } |
| ] |
| for label in example["labels"]: |
| if "startIndex" in label["valueSpan"]: |
| start = label["valueSpan"]["startIndex"] |
| else: |
| start = 0 |
| end = label["valueSpan"]["endIndex"] |
| value = example["userInput"]["text"][start:end] |
|
|
| ontology[domain][label["slot"]] = False |
|
|
| processed_example["dialog"][0]["belief_state"][0][ |
| "informed_slot_value_table" |
| ].append( |
| { |
| "slot": label["slot"], |
| "values": [{"value": value}], |
| "relation": "=", |
| } |
| ) |
| else: |
| processed_example["dialog"][0]["belief_state"] = [] |
|
|
| processed_data.append(processed_example) |
|
|
| write_jsonl_file(processed_data, os.path.join(args.output_dir, f"{split}.jsonl")) |
| write_json_file(ontology, os.path.join(args.output_dir, f"{split}_ontology.json")) |
|
|
|
|
| def preprocess(args): |
| domains = ["Buses_1", "Events_1", "Homes_1", "RentalCars_1"] |
| reformat(args, domains, "train") |
| |
| reformat(args, domains, "test") |
|
|
| |
| shutil.copy( |
| os.path.join(args.output_dir, "test.jsonl"), |
| os.path.join(args.output_dir, "dev.jsonl"), |
| ) |
| shutil.copy( |
| os.path.join(args.output_dir, "test_ontology.json"), |
| os.path.join(args.output_dir, "dev_ontology.json"), |
| ) |
|
|
|
|
| if __name__ == "__main__": |
| args = parse() |
| preprocess(args) |
|
|