-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebase.py
More file actions
361 lines (310 loc) · 15.8 KB
/
codebase.py
File metadata and controls
361 lines (310 loc) · 15.8 KB
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
from owlready2 import *
import uuid
import json
import pandas as pd
import os
import ollama
#### Alex' Funktionen
def dict2kg(onto,data):
user_id = uuid.uuid4().hex
# get references to classes and data properties
clPerson = onto.search(iri = "*Person")[0]
clOrganization = onto.search(iri = "*Organization")[0]
#dpName = onto.search(iri = "*name")[0] # Data Property
#dpAge = onto.search(iri = "*age")[0] # Data Property
#rMember = onto.search(iri = "*member")[0] # Object Property
# gibt klasse reaction name aus
clReactionName = onto.search(iri = "*0007009")[0]
# gibt klasse reaction name aus
clCatalysisResearchField = onto.search(iri = "*0000196")[0]
if type(data["voc4cat:CatalysisResearchField"]) != list:
data["voc4cat:CatalysisResearchField"] = [data["voc4cat:CatalysisResearchField"]]
if type(data["foaf:Organization"]) != list:
data["foaf:Organization"] = [data["foaf:Organization"]]
#person member organization
if type(data["foaf:Organization"]) == list:
for org in data["foaf:Organization"]:
for resField in data["voc4cat:CatalysisResearchField"]:
for resname in data["voc4cat:ReactionName"]:
codestring = """with onto:
Indv = onto.search(label = "person_{}").first() # gibt liste zurueck
if Indv == None:
Indv = clPerson('{}')
Indv.label = "person_{}"
Indv.name = "{}"
Indv.givenName.append("{}")
Indv.age = "{}"
org_indv = onto.search(label = "{}").first() # gibt liste zurueck
if org_indv == None:
org_indv = clOrganization('{}')
org_indv.label = "{}"
Indv.member.append(org_indv)
reac_indv = onto.search(label = "{}").first() # gibt liste zurueck
if reac_indv == None:
reac_indv = clReactionName('{}')
reac_indv.label = "{}"
Indv.favReaction.append(reac_indv)
research_indv = onto.search(label = "{}").first() # gibt liste zurueck
if research_indv == None:
research_indv = clCatalysisResearchField('{}')
research_indv.label = "{}"
Indv.favResearch.append(research_indv)
""".format(user_id,
user_id,
user_id,
user_id,
data["foaf:name"],
data["foaf:age"],
org,
org,
org,
resname,
resname,
resname,
resField,
resField,
resField,
)
code = compile(codestring, "<string>","exec")
exec(code)
else:
codestring = """with onto:
Indv = onto.search(label = "person_{}").first() # gibt liste zurueck
if Indv == None:
Indv = clPerson('{}')
Indv.label = "person_{}"
Indv.name = "{}"
Indv.givenName.append("{}")
Indv.age = "{}"
org_indv = onto.search(label = "{}").first() # gibt liste zurueck
if org_indv == None:
org_indv = clOrganization('{}')
org_indv.label = "{}"
Indv.member.append(org_indv)
reac_indv = onto.search(label = "{}").first() # gibt liste zurueck
if reac_indv == None:
reac_indv = clReactionName('{}')
reac_indv.label = "{}"
Indv.favReaction.append(reac_indv)
research_indv = onto.search(label = "{}").first() # gibt liste zurueck
if research_indv == None:
research_indv = clCatalysisResearchField('{}')
research_indv.label = "{}"
Indv.favResearch.append(research_indv)
""".format(user_id,
user_id,
user_id,
user_id,
data["foaf:name"],
data["foaf:age"],
data["foaf:Organization"],
data["foaf:Organization"],
data["foaf:Organization"],
data["voc4cat:ReactionName"],
data["voc4cat:ReactionName"],
data["voc4cat:ReactionName"],
data["voc4cat:CatalysisResearchField"],
data["voc4cat:CatalysisResearchField"],
data["voc4cat:CatalysisResearchField"],
)
code = compile(codestring, "<string>","exec")
exec(code)
return onto
#####
def kg_query(onto_world, search_str, query_number):
if query_number == 0:
# Give me all people whose favorite reaction contains search_str (case insensitive)
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?person ?name ?org ?reaction ?research
WHERE {{ ?person a foaf:Person.
?person foaf:givenName ?name.
?person foaf:member ?org.
?person voc4cat:favReaction ?reaction.
?person voc4cat:favResearch ?research.
?reaction rdfs:label ?reacLab.
FILTER(CONTAINS(LCASE(?reacLab),LCASE("{}")))
}}
""".format(search_str)
elif query_number == 1:
# Give me all people whose organization contains search_str (case insensitive)
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?person ?name ?org ?reaction ?research
WHERE {{ ?person a foaf:Person.
?person foaf:givenName ?name.
?person foaf:member ?org.
?person voc4cat:favReaction ?reaction.
?person voc4cat:favResearch ?research.
?org rdfs:label ?orgLab.
FILTER(CONTAINS(LCASE(?orgLab),LCASE("{}")))
}}
""".format(search_str)
# search for people with search_string in research field
elif query_number == 2:
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?person ?name ?org ?reaction ?research
WHERE {{ ?person a foaf:Person.
?person foaf:givenName ?name.
?person foaf:member ?org.
?person voc4cat:favReaction ?reaction.
?person voc4cat:favResearch ?research.
?research rdfs:label ?researchLab.
FILTER(CONTAINS(LCASE(?researchLab),LCASE("{}")))
}}
""".format(search_str)
# Search in Names
elif query_number == 3:
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?person ?name ?org ?reaction ?research
WHERE {{ ?person a foaf:Person.
?person foaf:givenName ?name.
?person foaf:member ?org.
?person voc4cat:favReaction ?reaction.
?person voc4cat:favResearch ?research.
FILTER(CONTAINS(LCASE(?name),LCASE("{}")))
}}
""".format(search_str)
# list all organizations
elif query_number == 4:
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?orgL
WHERE {{?org a foaf:Organization.
?org rdfs:label ?orgL.
}}
""".format(search_str)
# list all researchFields
elif query_number == 5:
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?resFieldL
WHERE {{?resField a voc4cat:voc4cat_0000196.
?resField rdfs:label ?resFieldL.
}}
""".format(search_str)
# list all voc4cat:ReactionName
elif query_number == 6:
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?resFieldL
WHERE {{?resField a voc4cat:voc4cat_0007009.
?resField rdfs:label ?resFieldL.
}}
""".format(search_str)
# people with same favourite reaction
elif query_number == 7:
query = """
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
SELECT DISTINCT ?person ?name ?org ?reaction ?research
WHERE {{ ?reaction voc4cat:favReactionOf ?person.
?person foaf:givenName ?name.
?person foaf:member ?org.
?person voc4cat:favResearch ?research.
FILTER(CONTAINS(LCASE(?reaction),LCASE("{}")))
}}
""".format(search_str)
else:
raise ValueError("query_number must be 0, 1, 2, 3, 4, 5, 6 or 7")
results = list(onto_world.sparql(query))
rows = []
for r in results:
row = [i if isinstance(i, str) else i.label.first() for i in r]
rows.append(row)
if query_number == 4:
df = pd.DataFrame(rows, columns = ['org'])
elif query_number == 5:
df = pd.DataFrame(rows, columns = ['resField'])
elif query_number == 6:
df = pd.DataFrame(rows, columns = ['reacName'])
else:
columns = ['person', 'name', 'org', 'reaction', 'research']
df = pd.DataFrame(rows, columns=columns)
return df
#####
##### Marcs Funktionen
def call_ollama(user_input):
prompt = user_input
# Extract JSON string from response
try:
# response = ollama.generate(model="summit_demo_new", prompt=prompt)
response = ollama.generate(model="demo_software_metadata_extraction", prompt=prompt)
except Exception as e:
print(f"Error calling Ollama API: {e}")
return None
json_str = response["response"].split("json")[1][:-3].strip()
# Parse JSON string to dict
try:
json_data = json.loads(json_str)
except json.JSONDecodeError as e:
print(f"Error parsing JSON: {e}")
return None
# Load existing data if file exists
if os.path.exists("testData_copy.json"):
with open("testData_copy.json", "r", encoding="utf-8") as f:
try:
result = json.load(f)
except json.JSONDecodeError:
result = {"person": {}}
else:
result = {"person": {}}
# Save updated data
with open("testData_copy.json", "w", encoding="utf-8") as f:
json.dump(json_data, f, indent=4)
return json_data
#####
# SPARQL QUERY TO BE TESTED
# PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
# PREFIX owl: <http://www.w3.org/2002/07/owl#>
# PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
# PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
# PREFIX foaf: <http://xmlns.com/foaf/0.1/>
# PREFIX voc4cat: <https://w3id.org/nfdi4cat/>
# SELECT DISTINCT ?favReaction ?name
# WHERE { ?favReaction a voc4cat:voc4cat_0007009.
# ?favReaction rdfs:label ?name.
# ?favReaction voc4cat:favReactionOf ?person.
# # FILTER(CONTAINS(LCASE(?name),LCASE("Ma")))
# }