-
Notifications
You must be signed in to change notification settings - Fork 1
/
queries.py
316 lines (290 loc) · 13.9 KB
/
queries.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import requests
import re
def main():
# example query that returns all universities in the database
exampleQuery = """
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ns2: <http://vivoweb.org/ontology/core#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT * WHERE {
?university rdf:type ns2:University;
} LIMIT 25
"""
executeQuery(exampleQuery)
print("""
Enter one of the questions below to test the knowledge base:
1. What is course [COURSE NAME][COURSE NUMBER] about?
2. Which topics is [STUDENT FIRSTNAME] [STUDENT LASTNAME] competent in?
3. Which courses at [UNIVERSITY] teaches [TOPIC].
4. What are all the courses for [COURSE NAME].
5. How many students are registered for [COURSE NAME][COURSE NUMBER]
6. What courses are worth [CREDITS] credits?
7. Give me a list of all universities.
8. What courses are in [SUBJECT_AREA] subject and are worth [CREDITS] credits?
9. What courses has [STUDENT FIRSTNAME][STUDENT LASTNAME] completed
10. What courses has [STUDENT FIRSTNAME][STUDENT LASTNAME] failed
""")
user_input = input()
#-----------------------------------competency q1-------------------------------------------------
# What is course [COURSE NAME][COURSE NUMBER] about?
if re.search("^What is course.", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
courseName = re.split("\s", trimmed)[3]
courseNumber = int(re.split("\s", trimmed)[4])
qres1 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT ?courseDesc
WHERE{{
?x vivo:hasSubjectArea '{courseName}'.
?x vivo:Catalog {courseNumber}.
?x vivo:shortDescription ?courseDesc.
}}
"""
executeQuery(qres1)
#-----------------------------------competency q2-------------------------------------------------
# Which topics is [STUDENT FIRSTNAME] [STUDENT LASTNAME]competent in?
elif re.search("^Which topics is.", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
givenName = re.split("\s", trimmed)[3]
familyName = re.split("\s", trimmed)[4]
qres2 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT ?competency
WHERE{{
?x foaf:givenName '{givenName}'.
?x foaf:familyName '{familyName}'.
?x docu:expertise ?y.
?y vivo:contains ?competency.
}}
"""
executeQuery(qres2)
#-----------------------------------competency q3-------------------------------------------------
# Which courses at [UNIVERSITY] teaches [TOPIC]
elif re.search("^Which courses at.", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
if re.search("Which courses at (.*) teaches", trimmed):
university = re.search("Which courses at (.*) teaches", trimmed).group(1)
if re.search("teaches (.*)", trimmed):
topic = re.search("teaches (.*)", trimmed).group(1)
qres3 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT ?title ?subjectArea ?subjectCode ?courseNum ?courseDesc
WHERE {{
?x vivo:hasSubjectArea ?subjectArea.
?x vivo:Title ?title.
?x vivo:hasSubjectArea ?subjectCode.
?x vivo:Catalog ?courseNum.
?x vivo:shortDescription ?courseDesc.
filter(regex(?courseDesc,'{topic}')).
}}
"""
executeQuery(qres3)
#-----------------------------------competency q4-------------------------------------------------
# What are all the courses for [COURSE NAME]
elif re.search("^What are all the courses for.", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
courseName = re.split("\s",trimmed)[len(re.split("\s",trimmed)) - 1]
qres4 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT ?course ?subjectCode ?courseNum
WHERE {{
?x vivo:hasSubjectArea '{courseName}'.
?x vivo:Title ?course.
?x vivo:hasSubjectArea ?subjectCode.
?x vivo:Catalog ?courseNum.
}}
"""
executeQuery(qres4)
#-----------------------------------competency q5-------------------------------------------------
# How many students are registered for [COURSE NAME][COURSE NUMBER]
elif re.search("^How many students are registered for.", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
courseName = re.split("\s",trimmed)[len(re.split("\s",trimmed)) - 2]
courseNumber = int(re.split("\s",trimmed)[len(re.split("\s",trimmed)) - 1])
qres5 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT (COUNT(?student) as ?numberOfStudents)
WHERE {{
?student docu:HasTaken ?x.
?x vivo:hasSubjectArea '{courseName}'.
?x vivo:Catalog {courseNumber}.
}}
"""
executeQuery(qres5)
#-----------------------------------competency q6-------------------------------------------------
# What courses are worth [CREDITS] credits?
elif re.search("^What courses are worth .", user_input):
trimmed = re.sub("[,?!]", "", user_input)
if re.search("are worth (.*) credits", trimmed):
credits = float(re.search("are worth (.*) credits", trimmed).group(1))
qres6 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT ?title ?subjectCode ?courseNum ?credit
WHERE {{
?x vivo:CourseCredits {credits}.
?x vivo:Title ?title.
?x vivo:hasSubjectArea ?subjectCode.
?x vivo:Catalog ?courseNum.
?x vivo:CourseCredits ?credit
}}
"""
executeQuery(qres6)
#-----------------------------------competency q7-------------------------------------------------
# Give me a list of all universities.
elif re.search("^Give me a list of all universities.", user_input):
qres7 = f"""
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX ns2: <http://vivoweb.org/ontology/core#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?title ?wiki
WHERE {{
?x rdf:type ns2:University.
?x rdfs:label ?title.
?x owl:sameAs ?wiki.
}}
"""
executeQuery(qres7)
# -----------------------------------competency q8-------------------------------------------------
# What courses are in [subject_area] subject and are worth [credits] credits?
elif re.search("^What courses are in.", user_input):
trimmed = re.sub("[,?!]", "", user_input)
if re.search("What courses are in (.*) subject", trimmed):
courseName = re.search("What courses are in (.*) subject", trimmed).group(1)
if re.search("are worth (.*) credits", trimmed):
credits = float(re.search("are worth (.*) credits", trimmed).group(1))
qres8 = f"""
PREFIX vivo: <http://vivoweb.org/ontology/core#>
PREFIX ns2: <http://vivoweb.org/ontology/core#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?title ?subjectCode ?courseNum ?credit
WHERE {{
?x vivo:CourseCredits {credits}.
?x vivo:Title ?title.
?x vivo:hasSubjectArea '{courseName}'.
?x vivo:hasSubjectArea ?subjectCode.
?x vivo:Catalog ?courseNum.
?x vivo:CourseCredits ?credit.
}}
"""
executeQuery(qres8)
# -----------------------------------competency q9-------------------------------------------------
# What courses has [STUDENT FIRSTNAME][STUDENT LASTNAME] completed
elif re.search("^What courses has.*completed$", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
givenName = re.split("\s", trimmed)[3]
familyName = re.split("\s", trimmed)[4]
qres9 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT DISTINCT ?course
WHERE{{
?x foaf:givenName '{givenName}'.
?x foaf:familyName '{familyName}'.
?x docu:HasTaken ?taken.
?taken docu:grade ?grade.
FILTER regex(?grade != 'F').
?taken docu:refersTo ?course.
}}
"""
executeQuery(qres9)
# -----------------------------------competency q10-------------------------------------------------
# What courses has [STUDENT FIRSTNAME][STUDENT LASTNAME] failed
elif re.search("^What courses has.*failed$", user_input):
trimmed = re.sub("[.,?!]", "", user_input)
givenName = re.split("\s", trimmed)[3]
familyName = re.split("\s", trimmed)[4]
qres10 = f"""
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX focu: <http://focu.io/schema#>
PREFIX focudata: <http://focu.io/data#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX vivo: <http://vivoweb.org/ontology/core#>
SELECT DISTINCT ?course
WHERE{{
?x foaf:givenName '{givenName}'.
?x foaf:familyName '{familyName}'.
?x docu:HasTaken ?taken.
?taken docu:grade 'F'.
?taken docu:refersTo ?course.
}}
"""
executeQuery(qres10)
else:
print("Query not recognized")
def executeQuery(query):
#send post request to fuseki server to access dataset : Project1
print(query)
response = requests.post('http://localhost:3030/Project1/sparql',
data={'query': query})
res = response.json()
# Prints the response of the SPARQL query
print(res)
if __name__ == '__main__':
main()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/