-
Notifications
You must be signed in to change notification settings - Fork 0
/
Ontology.py
502 lines (389 loc) · 18 KB
/
Ontology.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
from rdflib import OWL, RDF, URIRef, Literal, Namespace, Graph, BNode
from rdflib.namespace import FOAF, DCTERMS, XSD, RDF, SDO, OWL, RDFS
from rdflib.util import guess_format
from lookup import DBpediaLookup, WikidataAPI, GoogleKGLookup
from isub import isub
import pandas as pd
import csv
import re
import numpy
import owlrl
class Ontology(object):
def __init__(self, file, graph, pizza_restaurant, is_external):
self.file = file
self.stringToURI = dict()
self.graph = graph
self.graph.parse(pizza_restaurant)
self.is_external = is_external
# Namespace to create URIRefs
self.cw_ns_str = 'http://www.semanticweb.org/in3067-inm713/restaurants#'
# Special namespaces class to create directly URIRefs in python.
self.cw = Namespace(self.cw_ns_str)
# Prefix for the serialization
self.graph.bind("cw", self.cw) # cw is a newly created namespace
# Enable DBPedia lookups
self.dbpedia = DBpediaLookup()
def dframed(self):
df = pd.read_csv(self.file)
df = pd.DataFrame(df)
return df
def ingr_split(self, ingredients):
new_ingredients = []
for i in [ingredients]:
i = str(i)
temp = ''
i = i.replace('and', ',')
i = i.replace('with', ',')
i = i.replace('or', ',')
i = i.replace('any', ',')
if ',' in i:
temp = ','
if (temp != ''):
t = i.split(temp)
for e in t:
if e == ' ' or e == '':
continue
e = e.replace(' ', '')
new_ingredients.append(e)
else:
if i == 'nan':
i = ''
new_ingredients.append(i)
return new_ingredients
def preprocessing(self):
temp = self.dframed()
print(temp.shape)
extra = []
for a in range(temp.shape[0]):
# print('------------------------------')
for b in temp.columns:
if b == 'item description':
splt_ing = self.ingr_split(str(temp.iloc[a][b]))
else:
splt_ing = str(temp.iloc[a][b])
temp2 = splt_ing
# print(temp2)
if temp2 == 'nan':
temp2 = ''
# temp2 = re.sub(r'[^\w]', '', temp2)
if ',' in temp2:
temp2 = temp2.split(',')
if b == 'categories':
for cat in range(len(temp2)):
if 'and' in temp2[cat]:
temp2[cat] = temp2[cat].replace('and', '')
extra.append(temp2)
numpy.warnings.filterwarnings('ignore', category=numpy.VisibleDeprecationWarning)
extra = numpy.array(extra)
extra = extra.reshape((501, 11))
# extra = extra.flatten()
return extra
def create_property(self, subject, object):
self.graph.add((URIRef(subject), RDF.type, object))
def createURIForEntity(self, name, useExternalURI):
# We create fresh URI (default option)
self.stringToURI[name] = self.cw_ns_str + name.replace(" ", "_")
if useExternalURI: # We connect to online KG
uri = self.getExternalKGURI(name)
if uri != "":
self.stringToURI[name] = uri
return self.stringToURI[name]
def getExternalKGURI(self, name):
'''
Approximate solution: We get the entity with highest lexical similarity
The use of context may be necessary in some cases
'''
entities = self.dbpedia.getKGEntities(name, 5)
# print("Entities from DBPedia:")
current_sim = -1
current_uri = ''
for ent in entities:
isub_score = isub(name, ent.label)
if current_sim < isub_score:
current_uri = ent.ident
current_sim = isub_score
# print(current_uri)
return current_uri
def type_triple(self, subject, class_type, useExternalURI):
subject = str(subject)
sbj = re.sub(r'[^\w]', '', subject)
sbj_uri = self.cw + sbj
sbj_uri = self.createURIForEntity(sbj.lower(), useExternalURI)
self.graph.add((URIRef(sbj_uri), RDF.type, class_type))
# self.graph.add((URIRef(sbj_uri), RDF.type, object))
# self.create_property(object, OWL.Class)
def literal_triple(self, subject, object, predicate, datatype):
subject = str(subject)
object = str(object)
sbj = re.sub(r'[^\w]', '', subject)
sbj_uri = self.cw + sbj
lit = Literal(object, datatype=datatype)
self.graph.add((URIRef(sbj_uri), predicate, lit))
def object_triple(self, subject, predicate, object):
subject = str(subject)
object = str(object)
subject = re.sub(r'[^\w]', '', subject)
object = re.sub(r'[^\w]', '', object)
sbj_uri = self.cw + subject
obj_uri = self.cw + object
self.graph.add((URIRef(sbj_uri), predicate, URIRef(obj_uri)))
self.create_property(predicate, OWL.ObjectProperty)
# def location_triple(self, subject, object):
# subject = str(subject)
# object = str(object)
# subject = re.sub(r'[^\w]', '', subject)
# object = re.sub(r'[^\w]', '', object)
# self.graph.add((URIRef(self.cw + subject), self.cw.Location, URIRef(self.cw + object)))
def RDFSolution(self):
self.knowledge_graph(False)
def usingExternal(self):
self.knowledge_graph(True)
def knowledge_graph(self, useExternalURI):
lst = self.preprocessing()
# URIs FOR DIFFERENT PIZZAS WITH SAME NAME
for i, a in enumerate(lst):
id = str(i)
# restaurant_name = aprtin[0] + i
pizza_item = a[7]
restaurants = a[6]
price = a[8] # for literals only
if price == '':
price = '0'
# price = float(price)
if type(pizza_item) == list:
blank_string = ''
for iter_pizza in pizza_item:
blank_string = blank_string + iter_pizza
pizza_item = blank_string
pizza_item = pizza_item + '_' + id
if type(restaurants) == str:
restaurants = [restaurants]
# category filter used for dbpedia
filter_city = 'http://dbpedia.org/resource/Category:Cities_in_the_United_States'
filter_States = 'http://dbpedia.org/resource/Category:States_of_the_United_States'
# TYPES --> subject, type, object
# LITERALS --> subject, object, predicate, datatype
# restaurantName
# self.type_triple(a[0], self.cw.restaurantName)
self.literal_triple(a[0], a[0], self.cw.restaurantName, XSD.string)
# Address
self.type_triple(a[1], self.cw.Address, None)
self.literal_triple(a[1], a[1], self.cw.Address, XSD.string)
# City
self.type_triple(a[2], self.cw.City, useExternalURI) # add DBpedia
self.literal_triple(a[2], a[2], self.cw.City, XSD.string)
# Country
self.type_triple(a[3], self.cw.Country, useExternalURI) # add DBpedia
self.literal_triple(a[3], a[3], self.cw.Country, XSD.string)
# postCode
# self.type_triple(a[4], self.cw.postCode)
self.literal_triple(a[4], a[4], self.cw.postCode, XSD.string)
# State
self.type_triple(a[5], self.cw.State, useExternalURI) # add DBpedia
self.literal_triple(a[5], a[5], self.cw.State, XSD.string)
# Restaurant category
for r in restaurants:
self.type_triple(r, self.cw.Restaurant, None) # --> Category
for r in restaurants:
self.literal_triple(r, r, self.cw.Restaurant, XSD.string) # --> Category
# MenuItem
self.type_triple(pizza_item, self.cw.MenuItem, None)
self.literal_triple(pizza_item, pizza_item, self.cw.pizzaName, XSD.string) # check again
# ItemValue
self.type_triple(price, self.cw.ItemValue, None)
self.literal_triple(price, price, self.cw.amount, XSD.double)
# Currency
self.type_triple(a[9], self.cw.Currency, None)
self.literal_triple(a[9], a[9], self.cw.Currency, XSD.string)
# Ingredients
for r in a[10]:
self.type_triple(r, self.cw.Ingredient, None) # REMOVE BRACKETS in triples
for r in a[10]:
self.literal_triple(r, r, self.cw.Ingredient, XSD.string)
# ----------------------------------------------------------------------------------------------------------
# OBJECTS --> subject, predicate, object
self.object_triple(a[0], self.cw.IsLocatedIn, a[1])
restaurant = a[6] if isinstance(a[6], list) else [a[6]]
for rest in restaurant:
self.object_triple(a[0], self.cw.hasRestaurant, rest)
self.object_triple(a[9], self.cw.isCurrencyOf, a[3])
self.object_triple(a[1], self.cw.hasPostCode, a[4])
# amountCurrency
self.object_triple(price, self.cw.amountCurrency, a[9])
# hasIngredient
ingredient = a[10] if isinstance(a[10], list) else [a[10]]
for ingre in ingredient:
self.object_triple(pizza_item, self.cw.hasIngredient, ingre)
# hasLocation
# hasAdress
self.object_triple(a[2], self.cw.hasAdress, a[1])
# hasCity
self.object_triple(a[5], self.cw.hasCity, a[2])
# hasState
self.object_triple(a[3], self.cw.hasState, a[5])
# hasValue
self.object_triple(pizza_item, self.cw.hasValue, price)
# isIngredientOf
ingredient = a[10] if isinstance(a[10], list) else [a[10]]
for ingre in ingredient:
self.object_triple(ingre, self.cw.isIngredientOf, pizza_item)
# locatedIn
# locatedInAdress
self.object_triple(a[0], self.cw.locatedInAdress, a[1])
# locatedInCity
self.object_triple(a[1], self.cw.locatedInCity, a[2])
# locatedInCountry
self.object_triple(a[5], self.cw.locatedInCountry, a[3])
# locatedInState
self.object_triple(a[2], self.cw.locatedInstate, a[5])
# servedIn
# servedInRestaurant
self.object_triple(pizza_item, self.cw.servedInRestaurant, a[0])
# serves
# servesMenuItem
self.object_triple(a[0], self.cw.servesMenuItem, pizza_item)
# ----------------------------------------------------------------------------------------------------------
#
# # DATA PROPERTIES
# # amount
# self.create_property(self.cw.amount, OWL.DatatypeProperty)
#
# # firstLineAddress
# self.create_property(self.cw.firstLineAddress, OWL.DatatypeProperty)
#
# # itemName
# self.create_property(self.cw.itemName, OWL.DatatypeProperty)
#
# # name
# self.create_property(self.cw.name, OWL.DatatypeProperty)
#
# # postCode
# self.create_property(self.cw.postCode, OWL.DatatypeProperty)
#
# # restaurantName
# self.create_property(self.cw.restaurantName, OWL.DatatypeProperty)
# ----------------------------------------------------------------------------------------------------------
# print("Data triples from CSV: '" + str(len(self.graph)) + "'.")
# print(self.graph.serialize(format="turtle"))
def saveGraph(self, filename):
self.graph.serialize(destination=filename, format='ttl')
def saveOWL(self, filename):
self.graph.serialize(destination=filename, format='xml')
def saveTTL(self, filename):
self.graph.serialize(destination=filename, format='ttl')
def performReasoning(self, ontology_file):
# We expand the graph with the inferred triples
# We use owlrl library with OWL2 RL Semantics (instead of RDFS semantic as we saw in lab 4)
# More about OWL 2 RL Semantics in lecture/lab 7
print("Data triples from CSV: '" + str(len(self.graph)) + "'.")
# We should load the ontology first
# print(guess_format(ontology_file))
self.graph.parse(ontology_file, format=guess_format(ontology_file)) # e.g., format=ttl
print("Triples including ontology: '" + str(len(self.graph)) + "'.")
# We apply reasoning and expand the graph with new triples
owlrl.DeductiveClosure(owlrl.OWLRL_Semantics, axiomatic_triples=False, datatype_axioms=False).expand(self.graph)
print("Triples after OWL 2 RL reasoning: '" + str(len(self.graph)) + "'.")
def performSPARQLQuery1(self, file_query_out):
qres = self.graph.query(
"""PREFIX cw: <http://www.semanticweb.org/in3067-inm713/restaurants#>
SELECT DISTINCT ?restaurantName ?Address ?City
WHERE {
?restaurantName cw:locatedInAdress ?Address.
?Address cw:locatedInCity ?City.
?restaurantName cw:servesMenuItem ?pizzaItem.
?pizzaItem cw:pizzaName ?pizza_name.
?pizzaItem cw:hasIngredient ?ingre.
?ingre cw:Ingredient ?ingredient.
FILTER NOT EXISTS
{
FILTER(regex(?pizza_name, 'tomat', 'i') || regex(?ingredient, 'tomat', 'i'))
}
} GROUP BY ?restaurantName ?Address ?City"""
)
print("QUERY1 - Restaurant that sell pizzas without tomato : %s" % (str(len(qres))))
f_out = open(file_query_out, "w+")
for row in qres:
# Row is a list of matched RDF terms: URIs, literals or blank nodes
line_str = '\"%s\",\"%s\",\"%s\"\n' % (row.restaurantName, row.Address, row.City)
f_out.write(line_str)
f_out.close()
def performSPARQLQuery2(self, file_query_out):
qres = self.graph.query(
"""PREFIX cw: <http://www.semanticweb.org/in3067-inm713/restaurants#>
SELECT (AVG(?amount) AS ?average)
WHERE {
?pizza cw:pizzaName ?pname .
?pizza cw:hasValue ?value .
?value cw:amount ?amount .
FILTER regex(?pname, 'marg', 'i')
}"""
)
print("QUERY2 - Average price of Margherita Pizza : %s" % (str(len(qres))))
f_out = open(file_query_out, "w+")
for row in qres:
# Row is a list of matched RDF terms: URIs, literals or blank nodes
line_str = '\"%s\"\n' % (row.average)
f_out.write(line_str)
f_out.close()
def performSPARQLQuery3(self, file_query_out):
qres = self.graph.query(
"""PREFIX cw: <http://www.semanticweb.org/in3067-inm713/restaurants#>
SELECT (COUNT(DISTINCT ?rname) AS ?cnt) ?city
WHERE {
?rname cw:locatedInAdress ?add .
?add cw:locatedInCity ?city .
?city cw:locatedInstate ?state .
}
GROUP BY ?city
HAVING ((?cnt) >= 6)
ORDER BY ASC(?state) ASC(?cnt)"""
)
print("QUERY3 - Cities with more than 6 restaurants : %s" % (str(len(qres))))
print("OUTPUT SHOULD BE (cnt - 6) (city - NewYork), HAVING Clause does not work for some reason in python, "
"but works fine on GraphDB")
f_out = open(file_query_out, "w+")
for row in qres:
# Row is a list of matched RDF terms: URIs, literals or blank nodes
line_str = '\"%s\",\"%s\"\n' % (row.cnt, row.city)
f_out.write(line_str)
f_out.close()
def performSPARQLQuery4(self, file_query_out):
qres = self.graph.query(
"""PREFIX cw: <http://www.semanticweb.org/in3067-inm713/restaurants#>
SELECT ?rname ?Address ?postCode
WHERE
{
?rname cw:locatedInAdress ?Address .
?Address cw:locatedInCity ?City .
?City cw:hasAdress ?Address .
?Address cw:hasPostCode ?postCode .
FILTER(?postCode=cw:) .
}GROUP BY ?rname ?Address ?postCode"""
)
print("QUERY4 - List of restaurants with missing postcode : %s" % (str(len(qres))))
f_out = open(file_query_out, "w+")
for row in qres:
# Row is a list of matched RDF terms: URIs, literals or blank nodes
line_str = '\"%s\",\"%s\",\"%s\"\n' % (row.rname, row.Address, row.postCode)
f_out.write(line_str)
f_out.close()
def performSPARQLQuery5(self, file_query_out):
qres = self.graph.query(
"""PREFIX cw: <http://www.semanticweb.org/in3067-inm713/restaurants#>
SELECT DISTINCT ?RestaurantName ?Restaurant ?City
WHERE
{
?RestaurantName cw:hasRestaurant ?Restaurant .
?Restaurant cw:Restaurant ?restaurant_type .
?RestaurantName cw:locatedInAdress ?Address .
?Address cw:locatedInCity ?City .
FILTER(regex(?restaurant_type, 'american', 'i') || regex(?restaurant_type, 'asian', 'i'))
}GROUP BY ?RestaurantName ?Restaurant ?City """
)
print("QUERY5 - List of American and Asian Restaurants : %s" % (str(len(qres))))
f_out = open(file_query_out, "w+")
for row in qres:
# Row is a list of matched RDF terms: URIs, literals or blank nodes
line_str = '\"%s\",\"%s\",\"%s\"\n' % (row.RestaurantName, row.Restaurant, row.City)
f_out.write(line_str)
f_out.close()