#!/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"