-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeneric.py
304 lines (276 loc) · 15.3 KB
/
generic.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
from typing import List
import pandas as pd
from processor import QueryProcessor
from rdf import match_types
from model import Annotation, Canvas, Collection, Image, Manifest, IdentifiableEntity, EntityWithMetadata
class GenericQueryProcessor(QueryProcessor):
queryProcessors = []
"""the variable containing the list of QueryProcessor objects to involve when one of the get methods below is executed.
In practice, every time a get method is executed, the method will call the related method on
all the QueryProcessor objects included in the variable queryProcessors, before combining the results and returning the requested object."""
def cleanQueryProcessors(self):
"""It clean the list queryProcessors from all the QueryProcessor objects it includes."""
self.queryProcessors = []
return True
def addQueryProcessor(self, qp) -> bool:
"""It append the input QueryProcessor object to the list queryProcessors."""
self.queryProcessors.append(qp)
return True
def convert_row_to_model(self, row: pd.Series):
"""It converts the input row into an object having the class model."""
model = [value["model"] for _, value in match_types.items() if value["uriref"] == row["type"]][0]
return model(
id=row["id"],
label=row.get("label"),
title=row.get("title"),
creators=row.get("creator"),
)
def convert_dataframe_to_list(self, dataframe: pd.DataFrame):
"""It converts the input dataframe into a list of objects having the class model."""
return [self.convert_row_to_model(row) for _, row in dataframe.iterrows()]
def getEntityById(self, id: str):
entity = pd.DataFrame(columns=["id"])
for query_processor in self.queryProcessors:
data = query_processor.getEntityById(id)
if data is not None and not data.empty:
entity = pd.merge(entity, data, on='id', how='outer')
if entity.empty:
return None
if "type" in entity.columns:
return self.convert_dataframe_to_list(entity)[0]
elif "motivation" in entity.columns:
entity = entity.to_dict()
return Annotation(
id=entity.get("id", {}).get(0),
motivation=entity.get("motivation", {}).get(0),
body=Image(id=entity.get("body", {}).get(0)),
target=IdentifiableEntity(id=entity.get("target", {}).get(0)),
)
return None
def getAllAnnotations(self):
"""it returns a list of objects having class Annotation included in the databases accessible via the query processors."""
for qp in self.queryProcessors:
if "getAllAnnotations" in dir(qp):
annotations = qp.getAllAnnotations()
if not annotations.empty:
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in annotations.iterrows()]
return []
def getAllCanvas(self):
"""it returns a list of objects having class Canvas included in the databases accessible via the query processors."""
canvases = pd.DataFrame()
for qp in self.queryProcessors:
if "getAllCanvases" in dir(qp):
canvases = qp.getAllCanvases()
return [Canvas(
id=canvas["id"],
label=canvas.get("label"),
title=canvas.get("title"),
) for _, canvas in canvases.iterrows()]
def getAllImages(self):
"""it returns a list of objects having class Image included in the databases accessible via the query processors."""
for qp in self.queryProcessors:
if "getAllImages" in dir(qp):
images = qp.getAllImages()
return [Image(
id=image["body"],
) for _, image in images.iterrows()]
def getAllManifests(self):
"""it returns a list of objects having class Manifest included in the databases accessible via the query processors."""
for qp in self.queryProcessors:
if "getAllManifests" in dir(qp):
manifests = qp.getAllManifests()
else:
relational = qp
return [Manifest(
id=manifest["id"],
label=manifest.get("label"),
title=relational.getEntityById(manifest["id"]).loc[0, "title"],
creators=relational.getEntityById(manifest["id"]).loc[0, "creator"],
list_of_canvas=self.getCanvasesInManifest(manifest["id"]),
) for _, manifest in manifests.iterrows()]
def getAnnotationsToCanvas(self, canvas_id: str):
"""it returns a list of objects having class Annotation, included in the databases
accessible via the query processors, that have, as annotation target, the canvas specified by the input identifier."""
for qp in self.queryProcessors:
if "getAnnotationsWithTarget" in dir(qp):
annotation = qp.getAnnotationsWithTarget(canvas_id)
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in annotation.iterrows()]
def getAnnotationsToCollection(self, collection_id: str):
"""it returns a list of objects having class Annotation, included in the databases accessible
via the query processors, that have, as annotation target, the collection specified by the input identifier."""
annotation_data = pd.DataFrame()
for qp in self.queryProcessors:
if "getAnnotationsWithTarget" in dir(qp):
collection = qp.getAnnotationsWithTarget(collection_id)
annotation_data = pd.concat([annotation_data, collection], ignore_index=True)
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in annotation_data.iterrows()]
def getAnnotationsToManifest(self, manifest_id: str):
"""it returns a list of objects having class Annotation, included in the databases accessible
via the query processors, that have, as annotation target, the manifest specified by the input identifier."""
manifest_data = pd.DataFrame()
for qp in self.queryProcessors:
if "getAnnotationsWithTarget" in dir(qp):
manifest = qp.getAnnotationsWithTarget(manifest_id)
manifest_data = pd.concat([manifest_data, manifest], ignore_index=True)
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in manifest_data.iterrows()]
def getAnnotationsWithBody(self, id: str):
"""it returns a list of objects having class Annotation, included in the databases accessible
via the query processors, that have, as annotation body, the entity specified by the input identifier."""
for qp in self.queryProcessors:
if "getAnnotationsWithBody" in dir(qp):
annotation = qp.getAnnotationsWithBody(id)
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in annotation.iterrows()]
return None
def getAnnotationsWithTarget(self, id: str):
"""it returns a list of objects having class Annotation, included in the databases accessible
via the query processors, that have, as annotation target, the entity specified by the input identifier."""
for qp in self.queryProcessors:
if "getAnnotationsWithTarget" in dir(qp):
annotation = qp.getAnnotationsWithTarget(id)
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in annotation.iterrows()]
return None
def getAnnotationsWithBodyAndTarget(self, body: str, target: str):
"""it returns a list of objects having class Annotation, included in the databases accessible
via the query processors, that have, as annotation body and annotation target, the entities specified by the input identifiers."""
for qp in self.queryProcessors:
if "getAnnotationsWithBodyAndTarget" in dir(qp):
annotation = qp.getAnnotationsWithBodyAndTarget(body, target)
return [Annotation(
id=annotation["id"],
motivation=annotation["motivation"],
body=Image(id=annotation["body"]),
target=IdentifiableEntity(id=annotation["target"]),
) for _, annotation in annotation.iterrows()]
return None
def getCanvasesInCollection(self, collection_id: str):
"""it returns a list of objects having class Canvas, included in the databases accessible
via the query processors, that are contained in the collection identified by the input identifier."""
for qp in self.queryProcessors:
if "getCanvasesInCollection" in dir(qp):
canvases = qp.getCanvasesInCollection(collection_id)
return [Canvas(
id=canvas["id"],
label=canvas.get("label"),
title=canvas.get("title"),
) for _, canvas in canvases.iterrows()]
def getCanvasesInManifest(self, manifest_id: str):
"""it returns a list of objects having class Canvas, included in the databases accessible
via the query processors, that are contained in the manifest identified by the input identifier."""
for qp in self.queryProcessors:
if "getCanvasesInManifest" in dir(qp):
canvases = qp.getCanvasesInManifest(manifest_id)
return [Canvas(
id=canvas["id"],
label=canvas["label"],
title=canvas["title"],
) for _, canvas in canvases.iterrows()]
def getEntitiesWithCreator(self, creator_id: str):
"""it returns a list of objects having class EntityWithMetadata, included in the databases accessible
via the query processors, related to the entities having the input creator as one of their creators."""
entities = pd.DataFrame()
for qp in self.queryProcessors:
if "getEntitiesWithCreator" in dir(qp):
entities = qp.getEntitiesWithCreator(creator_id)
else:
triple_qp = qp
if not entities.empty:
entities["label"] = entities["id"].apply(lambda x: triple_qp.getEntityById(x).loc[0, "label"])
entities["type"] = entities["id"].apply(lambda x: triple_qp.getEntityById(x).loc[0, "type"])
return self.convert_dataframe_to_list(entities)
return []
def getEntitiesWithLabel(self, label: str):
"""it returns a list of objects having class EntityWithMetadata, included in the databases accessible
via the query processors, related to the entities having, as label, the input label."""
entities = pd.DataFrame()
for qp in self.queryProcessors:
if "getEntitiesWithLabel" in dir(qp):
entities = qp.getEntitiesWithLabel(label)
else:
relational_qp = qp
if not entities.empty:
entities["title"] = entities["id"].apply(lambda x: relational_qp.getEntityById(x).loc[0, "title"])
entities["creator"] = entities["id"].apply(lambda x: relational_qp.getEntityById(x).loc[0, "creator"])
return self.convert_dataframe_to_list(entities)
return []
def getEntitiesWithTitle(self, title: str) -> List[EntityWithMetadata]:
"""it returns a list of objects having class EntityWithMetadata, included in the databases accessible
via the query processors, related to the entities having, as title, the input title."""
entities = pd.DataFrame()
for qp in self.queryProcessors:
if "getEntitiesWithTitle" in dir(qp):
entities = qp.getEntitiesWithTitle(title)
else:
triple_qp = qp
if not entities.empty:
entities["label"] = entities["id"].apply(lambda x: triple_qp.getEntityById(x).loc[0, "label"])
entities["type"] = entities["id"].apply(lambda x: triple_qp.getEntityById(x).loc[0, "type"])
return self.convert_dataframe_to_list(entities)
return []
def getImagesAnnotatingCanvas(self, canvas_id: str):
"""it returns a list of objects having class Image, included in the databases accessible
via the query processors, that are body of the annotations targetting the canvases specified by the input identifier."""
for qp in self.queryProcessors:
if "getAnnotationsWithTarget" in dir(qp):
canvases = qp.getAnnotationsWithTarget(canvas_id)
canvases = canvases.to_dict("records")
return [Image(id=canvas["body"]) for canvas in canvases]
def getManifestsInCollection(self, collection_id: str):
"""it returns a list of objects having class Manifest, included in the databases accessible
via the query processors, that are contained in the collection identified by the input identifier."""
for qp in self.queryProcessors:
if "getManifestsInCollection" in dir(qp):
manifests = qp.getManifestsInCollection(collection_id)
else:
relational_qp = qp
return [Manifest(
id=manifest["id"],
label=manifest.get("label"),
title=relational_qp.getEntityById(manifest["id"]).loc[0, "title"],
creators=relational_qp.getEntityById(manifest["id"]).loc[0, "creator"],
list_of_canvas=self.getCanvasesInManifest(manifest["id"]),
) for _, manifest in manifests.iterrows()]
def getAllCollections(self):
"""it returns a list of objects having class Collection included in the databases accessible via the query processors."""
collections = pd.DataFrame()
for qp in self.queryProcessors:
if "getAllCollections" in dir(qp):
collections = qp.getAllCollections()
else:
relational_qp = qp
return [Collection(
id=collection["id"],
label=collection.get("label"),
title=collection.get("title"),
creators=relational_qp.getEntityById(collection["id"]).to_dict().get("creator", {}).get(0, []),
list_of_manifests=self.getManifestsInCollection(collection["id"]),
) for _, collection in collections.iterrows()]