-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathJsonQueryParser.py
166 lines (124 loc) · 5.53 KB
/
JsonQueryParser.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
#!/usr/bin/env python
"""This program execute
Run:
$ python JsonQueryParser.py qald-file.json
"""
import sys
import json
import os
import unicodedata
import re
from collections import defaultdict
pattern = "http://dbpedia.org/ontology"
pattern2 = "dbo:"
class QueryDataBase(object):
def __init__(self, inputfilepath):
self.inputfilepath = inputfilepath
self.database=defaultdict(list)
def openFile(self):
if os.path.exists(self.inputfilepath):
with open(self.inputfilepath, 'r') as inputfile:
try:
tmpinputdata = json.loads(inputfile.read())
inputfile.close()
return tmpinputdata
except IOError:
print "Error :Input file read or open error "
sys.exit()
except TypeError:
print "Error :Input file json error "
sys.exit()
else:
print "Error :File path does not exist"
sys.exit()
def patternList(self, tmpStr):
tmplist = tmpStr.split(" ")
filterlist = []
for i in range (0, len(tmplist)):
if pattern2 in tmplist[i]:
matchObj = re.match( 'dbo:.+', tmplist[i], re.M|re.I)
if matchObj:
filterlist.append(matchObj.group()[4:])
elif pattern in tmplist[i]:
matchObj = re.match( '<http://dbpedia.org/ontology/.+>', tmplist[i], re.M|re.I)
if matchObj:
filterlist.append(tmplist[i][29:-1])
return filterlist
def createDataBase(self, inputData) :
dataset_id = inputData['dataset']['id']
if dataset_id == 'qald-6-train-multilingual' or dataset_id == 'qald-7-train-multilingual':
for id in range (0, len(inputData['questions'])):
if "en" == str(inputData['questions'][id]['question'][0]['language']):
tmpQue = inputData['questions'][id]['question'][0]['string']
tmpQue_str = (unicodedata.normalize('NFKD', tmpQue).encode('ascii','ignore')).upper()
if inputData['questions'][id]['query'].has_key('sparql'):
try:
self.database[tmpQue_str] = self.patternList(str(inputData['questions'][id]['query']['sparql']))
except:
self.database[tmpQue_str] = []
else:
self.database[tmpQue_str] = []
elif dataset_id == 'qald-5_train' :
for id in range (0, len(inputData['questions'])) :
if "en" == str(inputData['questions'][id]['body'][0]['language']) :
tmpQue = inputData['questions'][id]['body'][0]['string']
tmpQue_str = (unicodedata.normalize('NFKD', tmpQue).encode('ascii','ignore')).upper()
# some entries do not have query
if 'query' in inputData['questions'][id]:
try:
self.database[tmpQue_str] = self.patternList(str(inputData['questions'][id]['query']))
except:
self.database[tmpQue_str] = []
else:
self.database[tmpQue_str] = []
else :
print "Invalid QALD dataset key"
def getQueryResult(self, query):
precision = 0.0
count = 0.0
if query in self.database.keys():
print "Query Found !!"
uriQueryList = self.user_input_uri_list()
checkList = self.database[query]
if len(checkList) > 0 :
for i in uriQueryList:
if i in checkList:
count += 1.0
precision = count/len(checkList)
else:
print "Query Not Found !!!!!"
return precision
def user_input_uri_list(self):
count = int(raw_input("Type count:").rstrip())
uriQueryList = []
for i in range(0, count):
tmp_uri = raw_input("Type Uri :").rstrip()
uriQueryList.append(tmp_uri.lower())
return uriQueryList
def print_result(self, query, precision):
print "\nResult for " + query + " ."
print "Precision: ", precision
def main():
if len(sys.argv) > 1 :
inputfilepath = sys.argv[1]
else :
print "Usage: $ python JsonQueryParser.py qald-file.json"
return 1
print "Example :"
print "Type Query: What is the birth name of Angela Merkel?"
print "Type count: 2"
print "Type Uri : <http://dbpedia.org/ontology/birthName>\n\n"
#inputfilepath = "qald-6-train-multilingual.json"
#inputfilepath = "qald-5_train.json"
queryDataBase = QueryDataBase(inputfilepath)
inputData = queryDataBase.openFile()
queryDataBase.createDataBase(inputData)
while True:
query = raw_input("Type Query:").rstrip()
precision = queryDataBase.getQueryResult(query.upper())
queryDataBase.print_result(query, precision)
keyinput = raw_input("Please Enter Y for Continue or any other to stop ===>")
if "y" != keyinput.lower():
break
if __name__ == "__main__":
main()