-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathretrieve_and_preprocess.py
145 lines (131 loc) · 5.27 KB
/
retrieve_and_preprocess.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from preprocessing import *
from drqa import retriever
import json
import sys
import argparse
from functools import partial
from multiprocessing import Pool, cpu_count
from tqdm import tqdm
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--split', required=True, type=str)
parser.add_argument('--model', default=None, type=str)
args = parser.parse_args()
n_threads = 64
if args.split in ['train', 'dev']:
print("using {}".format(args.split))
table_path = 'traindev_tables_tok'
request_path = 'traindev_request_tok'
if not os.path.exists(f'preprocessed_data/{args.split}_linked.json'):
with open(f'released_data/{args.split}.traced.json', 'r') as f:
data = json.load(f)
results1 = []
with Pool(n_threads) as p:
func_ = partial(
IR,
table_path=table_path,
request_path=request_path
)
results1 = list(
tqdm(
p.imap(func_, data, chunksize=16),
total=len(data),
desc="convert examples to features",
)
)
results2 = []
with Pool(n_threads) as p:
func_ = partial(
CELL,
table_path=table_path,
)
results2 = list(
tqdm(
p.imap(func_, results1, chunksize=16),
total=len(results1),
desc="convert examples to features",
)
)
train_results = analyze(results2, table_path)
random.shuffle(train_results)
with open(f'preprocessed_data/{args.split}_linked.json', 'w') as f:
json.dump(train_results, f, indent=2)
if args.split == 'train':
with open(f'preprocessed_data/{args.split}_linked.json', 'r') as f:
train_results = json.load(f)
results = prepare_stage1_data(train_results, table_path)
with open('preprocessed_data/stage1_training_data.json', 'w') as f:
json.dump(results, f, indent=2)
results = []
with Pool(n_threads) as p:
func_ = partial(
prepare_stage2_data,
table_path=table_path,
request_path=request_path
)
results = list(
tqdm(
p.imap(func_, train_results, chunksize=16),
total=len(train_results),
desc="convert examples to features",
)
)
train_split = []
for r1 in results:
train_split.extend(r1)
with open('preprocessed_data/stage2_training_data.json', 'w') as f:
json.dump(train_split, f, indent=2)
results = prepare_stage3_data(train_results, request_path)
with open('preprocessed_data/stage3_training_data.json', 'w') as f:
json.dump(results, f, indent=2)
elif args.split in ['dev_retrieve', 'test_retrieve']:
split = args.split.split('_')[0]
with open(f'released_data/{split}.json', 'r') as f:
dev_data = json.load(f)
k = 1
with open('data/all_constructed_tables.json', 'r') as f:
all_tables = json.load(f)
with open('data/all_passages.json', 'r') as f:
all_requests = json.load(f)
print('Start Retrieving tables and requested documents')
assert args.model is not None
ranker = retriever.get_class('tfidf')(tfidf_path=args.model)
for d in dev_data:
query = d['question']
doc_names, doc_scores = ranker.closest_docs(query, k)
d['table_id'] = doc_names[0]
d['table'] = all_tables[d['table_id']]
requested_documents = {}
for row in d['table']['data']:
for cell in row:
for ent in cell[1]:
requested_documents[ent] = all_requests[ent]
d['requested_documents'] = requested_documents
print('Done Retrieving tables and requested documents')
results1 = []
with Pool(n_threads) as p:
results1 = list(
tqdm(
p.imap(IR, dev_data, chunksize=32),
total=len(dev_data),
desc="convert examples to features",
)
)
for d in results1:
d['table'] = all_tables[d['table_id']]
results2 = []
with Pool(n_threads) as p:
results2 = list(
tqdm(
p.imap(CELL, results1, chunksize=32),
total=len(results1),
desc="convert examples to features",
)
)
for d in results2:
d['table'] = all_tables[d['table_id']]
dev_inputs = generate_inputs(results2)
with open(f'preprocessed_data/{split}_inputs.json', 'w') as f:
json.dump(dev_inputs, f, indent=2)
else:
raise NotImplementedError