diff --git a/ontoScorer/ontology_utils.py b/ontoScorer/ontology_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..e87b6659c0d828c21b20f8d4acd77df4be8dc253 --- /dev/null +++ b/ontoScorer/ontology_utils.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# Ontology Utilities +#------------------------------------------------------------------------------ +# -- +#============================================================================== + +from rdflib import Graph + +def harmonize_prefixes(graph, source_uri, target_uri): + for s, p, o in graph.triples((None, None, None)): + s = s if not s.startswith(source_uri) else s.replace(source_uri, target_uri) + p = p if not p.startswith(source_uri) else p.replace(source_uri, target_uri) + o = o if not isinstance(o, str) or not o.startswith(source_uri) else o.replace(source_uri, target_uri) + graph.add((s, p, o)) + return graph + +def extract_prefixes_from_graph(graph): + return {prefix: namespace for prefix, namespace in graph.namespaces()} + +def uri_to_short_form(uri, prefix_mappings, graph_prefixes): + for prefix, *uris in prefix_mappings: + for u in uris: + if u in uri: + return f"{prefix}:{uri.replace(u, '')}" + for prefix, namespace in graph_prefixes.items(): + if namespace in uri: + return f"{prefix}:{uri.replace(namespace, '')}" + return uri + diff --git a/tests/dev_test_2.py b/tests/dev_test_2.py new file mode 100644 index 0000000000000000000000000000000000000000..e2cecb4e7f7af3f91c2ad5c0c1abf1b38783027b --- /dev/null +++ b/tests/dev_test_2.py @@ -0,0 +1,127 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +print("=== DEV TEST 1") + +import os +from rdflib import Graph, Namespace + +DATA_FOLDER_PATH = f'{os.path.dirname(os.path.abspath(__file__))}/test_data' +ontology_a = f"{DATA_FOLDER_PATH}/ontology_a.ttl" +ontology_b = f"{DATA_FOLDER_PATH}/ontology_b.ttl" + +# Charger la première ontologie +graph_a = Graph() +graph_a.parse(ontology_a, format="ttl") + +# Charger la deuxième ontologie +graph_b = Graph() +graph_b.parse(ontology_b, format="ttl") + + +from rdflib import URIRef + +def harmonize_prefixes(graph, old_prefix, new_prefix): + new_graph = Graph() + + for s, p, o in graph: + if isinstance(s, URIRef) and old_prefix in str(s): + s = URIRef(str(s).replace(old_prefix, new_prefix)) + if isinstance(p, URIRef) and old_prefix in str(p): + p = URIRef(str(p).replace(old_prefix, new_prefix)) + if isinstance(o, URIRef) and old_prefix in str(o): + o = URIRef(str(o).replace(old_prefix, new_prefix)) + + new_graph.add((s, p, o)) + + return new_graph + + + +graph_a = harmonize_prefixes(graph_a, "https://tenet.tetras-libre.fr/base-ontology#", "https://reference.tetras-libre.fr/base-ontology#") + +def compare_graphs(graph1, graph2): + diff1 = set(graph1) - set(graph2) + diff2 = set(graph2) - set(graph1) + return diff1, diff2 + +diff_a_b, diff_b_a = compare_graphs(graph_a, graph_b) +print(f"Result: {len(diff_a_b)} | {len(diff_b_a)}") + + +print("\n=== DEV TEST 2") + +equivalent_prefix = [ + ("base", "https://reference.tetras-libre.fr/base-ontology#", "https://tenet.tetras-libre.fr/base-ontology#"), + ("result", "https://reference.tetras-libre.fr/expected-result#", "https://tenet.tetras-libre.fr/extract-result#") +] + + + +# from rdflib import URIRef + +# def update_prefixes(graph, prefix_equivalences): +# new_graph = Graph() + +# for s, p, o in graph: +# new_s, new_p, new_o = s, p, o + +# for prefix, old_prefix_uri, new_prefix_uri in prefix_equivalences: +# if isinstance(new_s, URIRef) and old_prefix_uri in str(new_s): +# new_s = URIRef(str(new_s).replace(old_prefix_uri, new_prefix_uri)) +# if isinstance(new_p, URIRef) and old_prefix_uri in str(new_p): +# new_p = URIRef(str(new_p).replace(old_prefix_uri, new_prefix_uri)) +# if isinstance(new_o, URIRef) and old_prefix_uri in str(new_o): +# new_o = URIRef(str(new_o).replace(old_prefix_uri, new_prefix_uri)) + +# new_graph.add((new_s, new_p, new_o)) + +# return new_graph + + +def uri_to_short_form(uri, prefix_mappings): + for prefix, *uris in prefix_mappings: + for u in uris: + if u in uri: + return f"{prefix}:{uri.replace(u, '')}" + return uri # Si aucune correspondance n'est trouvée, retourne l'URI original. + + + +uri = "https://reference.tetras-libre.fr/expected-result#SolarSystem" +short_form = uri_to_short_form(uri, equivalent_prefix) +print(short_form) # Affiche "result:SolarSystem" + + +print("\n=== DEV TEST 3") + +def extract_prefixes_from_graph(graph): + return {prefix: namespace for prefix, namespace in graph.namespaces()} + + +def uri_to_short_form(uri, prefix_mappings, graph_prefixes): + # Vérifier d'abord les correspondances dans equivalent_prefix + for prefix, *uris in prefix_mappings: + for u in uris: + if u in uri: + return f"{prefix}:{uri.replace(u, '')}" + + # Si aucune correspondance n'est trouvée dans equivalent_prefix, utiliser les préfixes du graphe + for prefix, namespace in graph_prefixes.items(): + if namespace in uri: + return f"{prefix}:{uri.replace(namespace, '')}" + + return uri # Si aucune correspondance n'est trouvée, retourne l'URI original. + + + +graph = Graph() +graph.parse(ontology_a, format="ttl") + +graph_prefixes = extract_prefixes_from_graph(graph) +uri = "http://www.w3.org/2000/01/rdf-schema#label" +short_form = uri_to_short_form(uri, equivalent_prefix, graph_prefixes) +print(short_form) # Affiche "rdfs:label" + + + diff --git a/tests/test_ontology_utils.py b/tests/test_ontology_utils.py new file mode 100644 index 0000000000000000000000000000000000000000..cbb6ce1601b26e4ac38ae5e58604fc25da29c229 --- /dev/null +++ b/tests/test_ontology_utils.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3.10 +# -*-coding:Utf-8 -* + +#============================================================================== +# test_ontology_modules: Ontology Modules Testing +#------------------------------------------------------------------------------ +# Contains tests for verifying functionality of the ontology submodules. +#============================================================================== + +import unittest +from rdflib import Graph + +# Importing necessary modules from the ontoScorer package for testing. +from context import ontoScorer +from ontoScorer import ontology_utils as ou + + +class TestOntologyUtils(unittest.TestCase): + + def setUp(self): + self.equivalent_prefix = [ + ("base", "https://reference.tetras-libre.fr/base-ontology#", "https://tenet.tetras-libre.fr/base-ontology#"), + ("result", "https://reference.tetras-libre.fr/expected-result#", "https://tenet.tetras-libre.fr/extract-result#") + ] + self.rdf_data = """ + @prefix ns1: <https://tenet.tetras-libre.fr/base-ontology#> . + @prefix owl: <http://www.w3.org/2002/07/owl#> . + @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> . + """ + self.graph = Graph().parse(data=self.rdf_data, format="turtle") + + def test_uri_to_short_form(self): + graph_prefixes = ou.extract_prefixes_from_graph(self.graph) + short_form = ou.uri_to_short_form("https://reference.tetras-libre.fr/expected-result#SolarSystem", self.equivalent_prefix, graph_prefixes) + self.assertEqual(short_form, "result:SolarSystem") + + short_form2 = ou.uri_to_short_form("http://www.w3.org/2000/01/rdf-schema#label", self.equivalent_prefix, graph_prefixes) + self.assertEqual(short_form2, "rdfs:label") + +if __name__ == '__main__': + unittest.main() +