forked from taoyds/typesql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_kg_entities.py
executable file
·208 lines (189 loc) · 4.17 KB
/
get_kg_entities.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
# -*- coding: utf-8 -*-
import json
import sys
import urllib
import torch
from typesql.utils import *
import numpy as np
import datetime
from nltk import ngrams
LOW_CHAR = ["a","aboard",
"about",
"above",
"across",
"after",
"against",
"along",
"amid",
"among",
"an",
"and",
"anti",
"around",
"as",
"at",
"before",
"behind",
"below",
"beneath",
"beside",
"besides",
"between",
"beyond",
"but",
"by",
"concerning",
"considering",
"despite",
"down",
"during",
"except",
"excepting",
"excluding",
"following",
"for",
"from",
"in",
"inside",
"into",
"like",
"minus",
"near",
"of",
"off",
"on",
"onto",
"opposite",
"or",
"outside",
"over",
"past",
"per",
"plus",
"regarding",
"round",
"save",
"since",
"so",
"than",
"the",
"through",
"to",
"toward",
"towards",
"under",
"underneath",
"unlike",
"until",
"up",
"upon",
"versus",
"via",
"with",
"within",
"without",
"yet"]
VISITED = []
#api_key = open('.api_key').read()
api_key = sys.argv[1]
sql_path = sys.argv[2]
service_url = 'https://kgsearch.googleapis.com/v1/entities:search'
#sql_path = "data/dev_tok.jsonl"
def load_data(sql_path):
sql_data = []
print "Loading data from %s"%sql_path
with open(sql_path) as inf:
for idx, line in enumerate(inf):
sql = json.loads(line.strip())
sql_data.append(sql)
return sql_data
def query_kg(query):
global VISITED
entity = None
params = {
'query': query,
'limit': 1,
'indent': True,
'key': api_key,
}
if query not in VISITED:
try:
url = service_url + '?' + urllib.urlencode(params)
response = json.loads(urllib.urlopen(url).read())
element = response['itemListElement'][0]
res = element['result']['name'].lower()
#ent_type = [e.lower() for e in element['result']['@type']]
try:
ent_type = [e.lower() for e in element['result']['@type']]
#desc = element['description']
except:
print "can not get ent_type!"
ent_type = []
#desc = ""
if query == res:
entity = (query.split(" "), ent_type)
except:
VISITED.append(query)
VISITED = list(set(VISITED))
pass
return entity
sql_data = load_data(sql_path)
count = 0
sql_data_kg = []
for sql in sql_data:
ents_kg = []
question = sql["question"]
q_toks = sql["question_tok"]
q_sp = sql["question"].split(" ")
upw_count = 3 #= len([q for q in q_sp if len(q) != 0 and q[0].isupper()])
flag_3 = True
flag_4 = True
flag_5 = True
#always run
if upw_count > 2:
#print "sentence is: ", sql["question"]
#print "question_tok is: ", sql["question_tok"]
bigrams = [q for q in ngrams(q_toks, 2) if q[0] not in LOW_CHAR]
for bg in bigrams:
qb = " ".join(bg)
ent = query_kg(qb)
if ent:
ents_kg.append(ent)
if flag_3:
trigrams = [q for q in ngrams(q_toks, 3) if q[0] not in LOW_CHAR]
for tg in trigrams:
qt = " ".join(tg)
ent = query_kg(qt)
if ent:
ents_kg.append(ent)
if flag_4:
grams4 = [q for q in ngrams(q_toks, 4) if q[0] not in LOW_CHAR]
for fg in grams4:
qf = " ".join(fg)
ent = query_kg(qf)
if ent:
ents_kg.append(ent)
if flag_5:
grams5 = [q for q in ngrams(q_toks, 5) if q[0] not in LOW_CHAR]
for fg in grams5:
qf = " ".join(fg)
ent = query_kg(qf)
if ent:
ents_kg.append(ent)
#else:
# print "skipping one---: ", sql["question"]
sql["kg_entities"] = ents_kg
if len(ents_kg) != 0:
try:
print "sentence is: ", sql["question"]
print "kg_entities is: ", sql["kg_entities"]
except:
pass
sql_data_kg.append(sql)
count += 1
print "\nloaded data exmple count: ", count
out_dir = sql_path + ".kg"
with open(out_dir,'w') as wf:
for d in sql_data_kg:
wf.write(json.dumps(d) + "\n")
print "done!!!"