-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdf.py
166 lines (137 loc) · 7.29 KB
/
rdf.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
import json
import pandas as pd
from rdflib import RDF, Literal, URIRef, Graph
from rdflib.plugins.stores.sparqlstore import SPARQLUpdateStore
from rdflib.namespace import RDFS
from sparql_dataframe import get as get_sparql
from urllib.error import URLError
from processor import Processor, QueryProcessor
from model import Collection, Manifest, Canvas
match_types = {
"Collection": {"uriref": "http://iiif.io/api/presentation/3#Collection", "model": Collection},
"Manifest": {"uriref": "http://iiif.io/api/presentation/3#Manifest", "model": Manifest},
"Canvas": {"uriref": "http://iiif.io/api/presentation/3#Canvas", "model": Canvas}
}
class CollectionProcessor(Processor):
def uploadData(self, filename: str) -> bool:
collection = URIRef(match_types["Collection"]["uriref"])
manifest = URIRef(match_types["Manifest"]["uriref"])
canvas = URIRef(match_types["Canvas"]["uriref"])
has_item = URIRef("http://iiif.io/api/presentation/3#hasItem")
my_graph = Graph()
with open(filename, "r") as user_file:
json_file = json.load(user_file)
for key, value in json_file.items():
collection_subject = URIRef(json_file["id"])
if key == "type" and value == "Collection":
type_triple = (collection_subject, RDF.type, collection)
my_graph.add(type_triple)
label = Literal(json_file["label"]["none"][0])
label_triple = (collection_subject, RDFS.label, label)
my_graph.add(label_triple)
for item in json_file["items"]:
if item["type"] == "Manifest":
manifest_subject = URIRef(item["id"])
type_triple = (manifest_subject, RDF.type, manifest)
my_graph.add(type_triple)
label = Literal(item["label"]["none"][0])
label_triple = (manifest_subject, RDFS.label, label)
my_graph.add(label_triple)
triple = (collection_subject, has_item, manifest_subject)
my_graph.add(triple)
for each in item["items"]:
if each["type"] == "Canvas":
canvas_subject = URIRef(each["id"])
type_triple = (canvas_subject, RDF.type, canvas)
my_graph.add(type_triple)
label = Literal(each["label"]["none"][0])
label_triple = (canvas_subject, RDFS.label, label)
my_graph.add(label_triple)
triple = (manifest_subject, has_item, canvas_subject)
my_graph.add(triple)
store = SPARQLUpdateStore()
store.open((self.dbPathOrUrl, self.dbPathOrUrl))
try:
for triple in my_graph.triples((None, None, None)):
store.add(triple)
except URLError:
print("There is a problem with the connection to the database.")
print("Are you sure the database is running?")
store.close()
return True
class TriplestoreQueryProcessor(QueryProcessor):
prefix_sc = "PREFIX sc: <http://iiif.io/api/presentation/3#> "
def getEntityById(self, id: str) -> pd.DataFrame:
query = f"""select ?id ?type ?label
WHERE {{
BIND(<{id}> AS ?id)
?id rdf:type ?type ;
rdfs:label ?label .
}}"""
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getAllCanvases(self):
"""it returns a data frame containing all the canvases included in the database."""
query = self.prefix_sc + """select ?id ?label ?title where
{
?manifest sc:hasItem ?id .
?id rdfs:label ?label .
?id rdf:type sc:Canvas .
?manifest rdfs:label ?title .
}
"""
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getAllCollections(self):
"""it returns a data frame containing all the collections included in the database."""
query = self.prefix_sc + "select ?id ?label where { ?id ?p sc:Collection . ?id rdfs:label ?label . }"
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getAllManifests(self):
"""it returns a data frame containing all the manifests included in the database."""
query = self.prefix_sc + """select ?id ?label where
{
?id ?p sc:Manifest .
?id rdfs:label ?label .
}
"""
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getCanvasesInCollection(self, collection_id: str):
"""it returns a data frame containing all the canvases included in the database
that are contained in the collection identified by the input identifier.
Example:
PREFIX sc: <http://iiif.io/api/presentation/3#>
select * where {
<https://dl.ficlit.unibo.it/iiif/28429/collection> sc:hasItem ?manifest .
?manifest sc:hasItem ?canvas .
?canvas rdfs:label ?label .
?canvas rdf:type ?type .
?manifest rdfs:label ?title .
}
"""
query = self.prefix_sc + "select * where { <" + collection_id + "> sc:hasItem ?manifest . ?manifest sc:hasItem ?id . ?id rdfs:label ?label . ?id rdf:type ?type . ?manifest rdfs:label ?title .}"
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getCanvasesInManifest(self, manifest_id: str):
"""it returns a data frame containing all the canvases included in the database
that are contained in the manifest identified by the input identifier.
Example:
PREFIX sc: <http://iiif.io/api/presentation/3#>
select ?s where { <https://dl.ficlit.unibo.it/iiif/2/28429/manifest> sc:hasItem ?s . }
"""
query = self.prefix_sc + "select ?id ?label ?title where { <" + manifest_id + "> sc:hasItem ?id . ?id rdfs:label ?label . <" + manifest_id + "> rdfs:label ?title . }"
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getManifestsInCollection(self, collection_id: str):
"""it returns a data frame containing all the manifests included in the database
that are contained in the collection identified by the input identifier."""
query = self.prefix_sc + "select * where { <" + collection_id + "> sc:hasItem ?id . ?id rdfs:label ?label . ?id rdf:type ?type . }"
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql
def getEntitiesWithLabel(self, label: str):
"""it returns a data frame containing all the entities included in the database
that have the input label."""
query = self.prefix_sc + "SELECT * WHERE { ?id rdfs:label '" + label + "' . ?id rdf:type ?type . ?id rdfs:label ?label . }"""
df_sparql = get_sparql(self.dbPathOrUrl, query, True)
return df_sparql