-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcreate_dataset.py
More file actions
91 lines (66 loc) · 2.98 KB
/
Copy pathcreate_dataset.py
File metadata and controls
91 lines (66 loc) · 2.98 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import json
import os
import random
random.seed(42)
import numpy as np
np.random.seed(42)
import shutil
base_path = 'data/multi_hop_temp/'
output_dir = 'data/multi_hop/'
maxhop = 40
def combine_facts(*fact_lists):
combined = []
types = ['atomic']
for i in range(len(fact_lists)):
types.append(str(i) + "hop_train")
for facts, tp in zip(fact_lists, types):
for fact in facts:
fact["type"] = tp
combined.append(fact)
return combined
def save_combined_facts(combined_facts, output_file):
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(combined_facts, f)
print(f"Saved combined facts to {output_file}")
def add_type_label(facts, fact_type):
for fact in facts:
fact['type'] = fact_type
return facts
def random_sample_with_type(facts, sample_size, fact_type):
sampled_facts = random.sample(facts, min(sample_size, len(facts)))
return add_type_label(sampled_facts, fact_type)
with open(os.path.join(base_path, 'atomic_facts.json'), 'r', encoding='utf-8') as f:
atomic_facts = json.load(f)
train_files = {}
test_files = {}
for i in range(2, maxhop + 1):
train_files[f"{i}hop_train"] = os.path.join(base_path, f"{i}hop_train.json")
test_files[f"{i}hop_test"] = os.path.join(base_path, f"{i}hop_test.json")
os.makedirs(output_dir, exist_ok=True)
shutil.copy(os.path.join(base_path, 'vocab.json'), os.path.join(output_dir, 'vocab.json'))
print(f"Copied vocab.json from {base_path} to {output_dir}")
train_data = {hop: json.load(open(path, 'r', encoding='utf-8')) for hop, path in train_files.items()}
test_data = {hop: json.load(open(path, 'r', encoding='utf-8')) for hop, path in test_files.items()}
print("Creating train sets...")
for i in range(2, maxhop + 1):
train_set = combine_facts(atomic_facts, *[train_data[f"{j}hop_train"] for j in range(2, i + 1)])
if i == 40:
save_combined_facts(train_set, os.path.join(output_dir, f'train.json'))
print("Creating extended test set...")
extended_test_set = []
for i in range(2, maxhop + 1):
extended_test_set.extend(add_type_label(test_data[f"{i}hop_test"], f"{i}hop_test"))
print("Creating small test set with 50 instances per type...")
small_test_set = []
for i in range(2, maxhop + 1):
small_test_set.extend(random_sample_with_type(test_data[f"{i}hop_test"], 50, f"{i}hop_test"))
small_test_path = os.path.join(output_dir, 'test_small.json')
save_combined_facts(small_test_set, small_test_path)
extended_test_set.extend(random_sample_with_type(atomic_facts, 2000, "atomic_facts"))
for i in range(2, maxhop + 1):
extended_test_set.extend(random_sample_with_type(train_data[f"{i}hop_train"], 2000, f"{i}hop_train"))
test_extended_path = os.path.join(output_dir, 'test.json')
save_combined_facts(extended_test_set, test_extended_path)
shutil.copy(test_extended_path, os.path.join(output_dir, 'valid.json'))
print(f"Created valid.json by copying test.json to {output_dir}")
print("All train, test, and valid sets created and saved.")