-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrelational.py
138 lines (117 loc) · 5.78 KB
/
relational.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
# all the classes for handling the relational database
# AnnotationProcessor, MetadataProcessor, RelationalQueryProcessor
import csv
import sqlite3
import pandas as pd
from processor import Processor, QueryProcessor
all = [
"AnnotationProcessor",
"MetadataProcessor",
"QueryProcessor",
"RelationalQueryProcessor",
]
class AnnotationProcessor(Processor):
def uploadData(self, filename: str) -> bool:
"""it takes in input the path of a CSV file containing annotations and uploads them in the database.
This method can be called everytime there is a need to upload annotations in the database."""
connection = sqlite3.connect(self.dbPathOrUrl)
cursor = connection.cursor()
create_table = '''CREATE TABLE IF NOT EXISTS annotations(
id STRING PRIMARY KEY,
body STRING NOT NULL,
target STRING NOT NULL,
motivation STRING NOT NULL);
'''
cursor.execute(create_table)
with open(filename, "r", encoding="utf-8") as file:
contents = csv.reader(file)
next(contents)
insert_records = "INSERT INTO annotations (id, body, target, motivation) VALUES(?, ?, ?, ?)"
try:
cursor.executemany(insert_records, contents)
connection.commit()
connection.close()
except sqlite3.IntegrityError:
print("Error: the database already contains the entity with the same identifier.")
return True
class MetadataProcessor(Processor):
def uploadData(self, filename: str) -> bool:
"""it takes in input the path of a CSV file containing metadata and uploads them in the database.
This method can be called everytime there is a need to upload metadata in the database."""
connection = sqlite3.connect(self.dbPathOrUrl)
cursor = connection.cursor()
create_table = '''CREATE TABLE IF NOT EXISTS metadata(
id STRING PRIMARY KEY,
title STRING NOT NULL,
creator STRING NOT NULL);
'''
cursor.execute(create_table)
with open(filename, "r", encoding="utf-8") as file:
contents = csv.reader(file)
next(contents)
insert_records = "INSERT INTO metadata (id, title, creator) VALUES(?, ?, ?)"
try:
cursor.executemany(insert_records, contents)
connection.commit()
connection.close()
except sqlite3.IntegrityError:
print("Error: the database already contains the entity with the same identifier.")
return True
class RelationalQueryProcessor(QueryProcessor):
def getEntityById(self, id: str) -> pd.DataFrame:
if not isinstance(id, str):
return None
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM metadata WHERE id = ?"
result = pd.read_sql(query, con, params=(id,))
if not result.empty:
return result
query = "SELECT * FROM annotations WHERE id = ?"
result = pd.read_sql(query, con, params=(id,))
return result
def getAllAnnotations(self):
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM annotations"
df_sql = pd.read_sql(query, con)
return df_sql
def getAllImages(self):
"""it returns a data frame containing all the images included in the database."""
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT body FROM annotations"
df_sql = pd.read_sql(query, con)
return df_sql
def getAnnotationsWithBody(self, body):
""""it returns a data frame containing all the annotations included in the database
that have, as annotation body, the entity specified by the input identifier."""
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM annotations"
df_sql = pd.read_sql(query, con)
return df_sql.query(f"body == '{body}'")
def getAnnotationsWithTarget(self, target):
"""it returns a data frame containing all the annotations included in the database
that have, as annotation target, the entity specified by the input identifier."""
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM annotations"
df_sql = pd.read_sql(query, con)
return df_sql.query(f"target == '{target}'")
def getAnnotationsWithBodyAndTarget(self, body, target):
"""it returns a data frame containing all the annotations included in the database
that have, as annotation body and annotation target, the entities specified by the input identifiers."""
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM annotations"
df_sql = pd.read_sql(query, con)
return df_sql.query(f"body == '{body}' and target == '{target}'")
def getEntitiesWithCreator(self, creator):
"""it returns a data frame containing all the metadata included in the database
related to the entities having the input creator as one of their creators."""
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM metadata"
df_sql = pd.read_sql(query, con)
return df_sql[df_sql['creator'].str.contains(creator)]
def getEntitiesWithTitle(self, title):
"""it returns a data frame containing all the metadata included in the database
related to the entities having, as title, the input title."""
with sqlite3.connect(self.dbPathOrUrl) as con:
query = "SELECT * FROM metadata"
df_sql = pd.read_sql(query, con)
return df_sql.query(f"title == '{title}'")