|
5 | 5 |
|
6 | 6 | apikey = os.getenv("OPEN-AI-KEY") |
7 | 7 |
|
8 | | -print(apikey) |
9 | | - |
10 | 8 | from agents import Agent, ModelSettings, function_tool |
11 | 9 | import rdflib |
| 10 | + |
12 | 11 | knowledge_graph_path = os.getenv("KNOWLEDGE_GRAPH_PATH") |
13 | 12 |
|
14 | 13 | ontologies_path = os.getenv("ONTOLOGIES_PATH") |
| 14 | +# SPARQLwrapper |
| 15 | +dataset = rdflib.Dataset() |
15 | 16 |
|
| 17 | +onto = dataset.graph("https://imaging-plaza.epfl.ch/ontology#") |
| 18 | +onto.parse(ontologies_path) |
| 19 | + |
| 20 | +data = dataset.graph("https://imaging-plaza.epfl.ch/finalGraph") |
| 21 | +data.parse(knowledge_graph_path) |
16 | 22 | # Load the knowledge graph |
17 | 23 |
|
18 | | -# SPARQLwrapper |
19 | | -onto = rdflib.Graph() |
20 | | -onto.parse(knowledge_graph_path) |
21 | 24 |
|
22 | 25 | #Query |
23 | 26 | query = r""" |
|
26 | 29 | PREFIX schema: <http://schema.org/> |
27 | 30 | PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> |
28 | 31 |
|
29 | | -SELECT DISTINCT ?o |
30 | | -
|
31 | | -WHERE { |
32 | | - ?s ?p ?o . |
33 | | - FILTER (!(?p IN (schema:name, schema:description, rdfs:comment, skos:definition))) |
34 | | - FILTER (!regex(STR(?o), "^[ \t]*https?://")) |
35 | | - FILTER (!regex(STR(?o), "^\\d{4}-\\d{2}-\\d{2}T00:00:00\\.000Z$")) |
36 | | - FILTER (datatype(?o) = xsd:string) |
| 32 | +CONSTRUCT { |
| 33 | + ?s ?p ?result . |
| 34 | +} |
| 35 | +WHERE { |
| 36 | + GRAPH <https://imaging-plaza.epfl.ch/finalGraph> { |
| 37 | + ?s ?p ?o . |
| 38 | + FILTER (!(?p IN (schema:name, schema:description, rdfs:comment, skos:definition))) |
| 39 | + FILTER (!regex(STR(?o), "^[ \t]*https?://")) |
| 40 | + FILTER (!regex(STR(?o), "^\\d{4}-\\d{2}-\\d{2}T00:00:00\\.000Z$")) |
| 41 | + FILTER (datatype(?o) = xsd:string) |
37 | 42 | } |
| 43 | + GRAPH <https://imaging-plaza.epfl.ch/ontology#> { |
| 44 | + OPTIONAL { |
| 45 | + ?s2 rdfs:label ?o. |
| 46 | + } |
| 47 | + |
| 48 | + # Ensure only one IRI is bound to the label, skipping rows with multiple IRIs |
| 49 | + FILTER NOT EXISTS { |
| 50 | + ?s3 rdfs:label ?o. |
| 51 | + FILTER (?s3 != ?s2) # Ensures ?s2 is the only IRI bound to the label |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + BIND(IF(BOUND(?s2), ?s2, ?o) AS ?result) |
| 56 | +} |
| 57 | +
|
38 | 58 | """ |
39 | | -results = onto.query(query) |
| 59 | +results = dataset.query(query) |
40 | 60 | # Print the results |
41 | 61 | for row in results: |
42 | | - print(row.o) |
43 | | -#count the number of rows: |
44 | | -print("Number of rows: ", len(results)) |
45 | | -# Find all objects of triples that are strings (Agent 1 using SPARQL SELECT query) |
| 62 | + print(row) |
| 63 | + |
| 64 | +# Create a new graph to store the constructed triples |
| 65 | +constructed_graph = rdflib.Graph() |
| 66 | + |
| 67 | +# Add the results of the CONSTRUCT query to the new graph |
| 68 | +for triple in results.graph: |
| 69 | + constructed_graph.add(triple) |
| 70 | + |
| 71 | + |
46 | 72 |
|
47 | 73 | # Attempt to match those strings to strings in the ontology (Agent 2 - FUZON ) |
48 | 74 | # |
|
0 commit comments