-
Notifications
You must be signed in to change notification settings - Fork 26
/
orchestrator.py
287 lines (251 loc) · 12.4 KB
/
orchestrator.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from common.graph.graph import Graph
from common.query.querybuilder import QueryBuilder
from parser.lc_quad import LC_Qaud
from sklearn.model_selection import train_test_split
import os
import torch.optim as optim
from learning.treelstm.model import *
from learning.treelstm.vocab import Vocab
from learning.treelstm.trainer import Trainer
from learning.treelstm.dataset import QGDataset
import learning.treelstm.scripts.preprocess_lcquad as preprocess_lcquad
from common.container.uri import Uri
from common.container.linkeditem import LinkedItem
from parser.lc_quad import LC_QaudParser
import common.utility.utility as utility
from learning.classifier.svmclassifier import SVMClassifier
import ujson
import learning.treelstm.Constants as Constants
import numpy as np
class Struct(object): pass
class Orchestrator:
def __init__(self, logger, question_classifier, double_relation_classifer, parser, auto_train=True):
self.logger = logger
self.question_classifier = question_classifier
self.double_relation_classifer = double_relation_classifer
self.parser = parser
self.kb = parser.kb
self.X_train, self.X_test, self.y_train, self.y_test = [], [], [], []
if auto_train and not question_classifier.is_trained:
self.train_question_classifier()
if auto_train and double_relation_classifer is not None and not double_relation_classifer.is_trained:
self.train_double_relation_classifier()
self.dep_tree_cache_file_path = './caches/dep_tree_cache.json'
if os.path.exists(self.dep_tree_cache_file_path):
with open(self.dep_tree_cache_file_path) as f:
self.dep_tree_cache = ujson.load(f)
else:
self.dep_tree_cache = dict()
def prepare_question_classifier_dataset(self, file_path=None):
if file_path is None:
ds = LC_Qaud()
else:
ds = LC_Qaud(file_path)
ds.load()
ds.parse()
X = []
y = []
for qapair in ds.qapairs:
X.append(qapair.question.text)
if "COUNT(" in qapair.sparql.raw_query:
y.append(2)
elif "ASK WHERE" in qapair.sparql.raw_query:
y.append(1)
else:
y.append(0)
return X, y
def prepare_double_relation_classifier_dataset(self, file_path=None):
if file_path is None:
ds = LC_Qaud()
else:
ds = LC_Qaud(file_path)
ds.load()
ds.parse()
X = []
y = []
for qapair in ds.qapairs:
X.append(qapair.question.text)
relation_uris = [u for u in qapair.sparql.uris if u.is_ontology() or u.is_type()]
if len(relation_uris) != len(set(relation_uris)):
y.append(1)
else:
y.append(0)
return X, y
def train_question_classifier(self, file_path=None, test_size=0.2):
X, y = self.prepare_question_classifier_dataset(file_path)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=test_size,
random_state=42)
return self.question_classifier.train(self.X_train, self.y_train)
def train_double_relation_classifier(self, file_path=None, test_size=0.2):
X, y = self.prepare_double_relation_classifier_dataset(file_path)
self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(X, y, test_size=test_size,
random_state=42)
return self.double_relation_classifer.train(self.X_train, self.y_train)
def rank(self, args, question, generated_queries):
if len(generated_queries) == 0:
return []
if 2 > 1:
# try:
# Load the model
checkpoint_filename = '%s.pt' % os.path.join(args.save, args.expname)
dataset_vocab_file = os.path.join(args.data, 'dataset.vocab')
# metrics = Metrics(args.num_classes)
vocab = Vocab(filename=dataset_vocab_file,
data=[Constants.PAD_WORD, Constants.UNK_WORD, Constants.BOS_WORD, Constants.EOS_WORD])
similarity = DASimilarity(args.mem_dim, args.hidden_dim, args.num_classes)
model = SimilarityTreeLSTM(
vocab.size(),
args.input_dim,
args.mem_dim,
similarity,
args.sparse)
criterion = nn.KLDivLoss()
optimizer = optim.Adagrad(model.parameters(), lr=args.lr, weight_decay=args.wd)
emb_file = os.path.join(args.data, 'dataset_embed.pth')
if os.path.isfile(emb_file):
emb = torch.load(emb_file)
model.emb.weight.data.copy_(emb)
checkpoint = torch.load(checkpoint_filename, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint['model'])
trainer = Trainer(args, model, criterion, optimizer)
# Prepare the dataset
json_data = [{"id": "test", "question": question,
"generated_queries": [{"query": query["where"], "correct": False} for query in
generated_queries]}]
output_dir = "./output/tmp"
preprocess_lcquad.save_split(output_dir, *preprocess_lcquad.split(json_data, self.parser))
lib_dir = './learning/treelstm/lib/'
classpath = ':'.join([
lib_dir,
os.path.join(lib_dir, 'stanford-parser/stanford-parser.jar'),
os.path.join(lib_dir, 'stanford-parser/stanford-parser-3.5.1-models.jar')])
if question in self.dep_tree_cache:
preprocess_lcquad.parse(output_dir, cp=classpath, dep_parse=False)
cache_item = self.dep_tree_cache[question]
with open(os.path.join(output_dir, 'a.parents'), 'w') as f_parent, open(
os.path.join(output_dir, 'a.toks'), 'w') as f_token:
for i in range(len(generated_queries)):
f_token.write(cache_item[0])
f_parent.write(cache_item[1])
else:
preprocess_lcquad.parse(output_dir, cp=classpath)
with open(os.path.join(output_dir, 'a.parents')) as f:
parents = f.readline()
with open(os.path.join(output_dir, 'a.toks')) as f:
tokens = f.readline()
self.dep_tree_cache[question] = [tokens, parents]
with open(self.dep_tree_cache_file_path, 'w') as f:
ujson.dump(self.dep_tree_cache, f)
test_dataset = QGDataset(output_dir, vocab, args.num_classes)
test_loss, test_pred = trainer.test(test_dataset)
return test_pred
# except Exception as expt:
# self.logger.error(expt)
# return []
def generate_query(self, question, entities, relations, h1_threshold=None, question_type=None):
ask_query = False
sort_query = False
count_query = False
if question_type is None:
question_type = 0
if self.question_classifier is not None:
question_type = self.question_classifier.predict([question])
if question_type == 2:
count_query = True
elif question_type == 1:
ask_query = True
type_confidence = self.question_classifier.predict_proba([question])[0][question_type]
if isinstance(self.question_classifier.predict_proba([question])[0][question_type], (np.ndarray, list)):
type_confidence = type_confidence[0]
double_relation = False
# if self.double_relation_classifer is not None:
# double_relation = self.double_relation_classifer.predict([question])
# if double_relation == 1:
# double_relation = True
graph = Graph(self.kb)
query_builder = QueryBuilder()
graph.find_minimal_subgraph(entities, relations, double_relation=double_relation, ask_query=ask_query,
sort_query=sort_query, h1_threshold=h1_threshold)
valid_walks = query_builder.to_where_statement(graph, self.parser.parse_queryresult, ask_query=ask_query,
count_query=count_query, sort_query=sort_query)
# if question_type == 0 and len(relations) == 1:
# double_relation = True
# graph = Graph(self.kb)
# query_builder = QueryBuilder()
# graph.find_minimal_subgraph(entities, relations, double_relation=double_relation, ask_query=ask_query,
# sort_query=sort_query, h1_threshold=h1_threshold)
# valid_walks_new = query_builder.to_where_statement(graph, self.parser.parse_queryresult,
# ask_query=ask_query,
# count_query=count_query, sort_query=sort_query)
# valid_walks.extend(valid_walks_new)
if len(valid_walks) == 0:
return valid_walks, question_type, 0
args = Struct()
base_path = "./learning/treelstm/"
args.save = os.path.join(base_path, "checkpoints/")
args.expname = "lc_quad"
args.mem_dim = 150
args.hidden_dim = 50
args.num_classes = 2
args.input_dim = 300
args.sparse = False
args.lr = 0.01
args.wd = 1e-4
args.data = os.path.join(base_path, "data/lc_quad/")
args.cuda = False
try:
scores = self.rank(args, question, valid_walks)
except:
scores = [1.1 for _ in valid_walks]
for idx, item in enumerate(valid_walks):
if idx >= len(scores):
item["confidence"] = 0.3
else:
item["confidence"] = float(scores[idx] - 1)
return valid_walks, question_type, type_confidence
if __name__ == "__main__":
args = Struct()
base_path = "./learning/treelstm/"
args.save = os.path.join(base_path, "checkpoints/")
args.expname = "lc_quad"
args.mem_dim = 150
args.hidden_dim = 50
args.num_classes = 2
args.input_dim = 300
args.sparse = ""
args.lr = 0.01
args.wd = 1e-4
args.data = os.path.join(base_path, "data/lc_quad/")
args.cuda = False
parser = LC_QaudParser()
kb = parser.kb
base_dir = "./output"
question_type_classifier_path = os.path.join(base_dir, "question_type_classifier")
utility.makedirs(question_type_classifier_path)
question_type_classifier = SVMClassifier(os.path.join(question_type_classifier_path, "svm.model"))
o = Orchestrator(None, question_type_classifier, None, parser, True)
raw_entities = [{"surface": "", "uris": [{"confidence": 1, "uri": "http://dbpedia.org/resource/Bill_Finger"}]}]
entities = []
for item in raw_entities:
uris = [Uri(uri["uri"], kb.parse_uri, uri["confidence"]) for uri in item["uris"]]
entities.append(LinkedItem(item["surface"], uris))
raw_relations = [{"surface": "", "uris": [{"confidence": 1, "uri": "http://dbpedia.org/ontology/creator"}]},
{"surface": "", "uris": [{"confidence": 1, "uri": "http://dbpedia.org/ontology/ComicsCharacter"}]}]
relations = []
for item in raw_relations:
uris = [Uri(uri["uri"], kb.parse_uri, uri["confidence"]) for uri in item["uris"]]
relations.append(LinkedItem(item["surface"], uris))
question = "Which comic characters are painted by Bill Finger?"
generated_queries = o.generate_query(question, entities, relations)[0]
# print generated_queries
# generated_queries = [
# {'where': [u'?u_0 <http://dbpedia.org/ontology/creator> <http://dbpedia.org/resource/Bill_Finger>',
# u'?u_0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/ComicsCharacter>']},
# {'where': [u'?u_0 <http://dbpedia.org/ontology/ComicsCharacter> <http://dbpedia.org/resource/Bill_Finger>',
# u'?u_0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://dbpedia.org/ontology/creator>']}
# ]
scores = o.rank(args, question, generated_queries)
print(scores)
generated_queries.extend(generated_queries)
scores = o.rank(args, question, generated_queries)
print(scores)