Skip to content
Snippets Groups Projects
Commit 7aa4a11f authored by Aurélien Lamercerie's avatar Aurélien Lamercerie
Browse files

Basic script to compare ontologies

parent 935b8992
No related branches found
No related tags found
No related merge requests found
env/*
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# ontoScorer: Script to compare two ontologies and calculate metrics
#------------------------------------------------------------------------------
# This script loads two OWL ontologies from local files, extracts classes from
# these ontologies and compares them. It then calculates precision, recall, and
# F1 score, which are commonly used metrics in information retrieval to assess
# the quality of a retrieval system. The paths to the ontology files are
# specified directly in the script.
#==============================================================================
from rdflib import Graph, OWL
from rdflib.namespace import split_uri
from sklearn.metrics import precision_score, recall_score, f1_score
# Define the path to the data folder
DATA_FOLDER_PATH = "data"
# Define paths to the ontology files
REFERENCE_ONTOLOGY_PATH = f"{DATA_FOLDER_PATH}/reference_ontology.ttl"
GENERATED_ONTOLOGY_PATH = f"{DATA_FOLDER_PATH}/generated_ontology.ttl"
#==============================================================================
# Methods to manipulate ontologies and calculate the metrics
#==============================================================================
def load_ontology(path):
g = Graph()
g.parse(path, format="ttl")
return g
def get_classes(ontology, ontology_path):
classes = set()
triplets_count = 0
for s, p, o in ontology.triples((None, None, None)):
triplets_count += 1
if o == OWL.Class:
_, class_name = split_uri(s)
classes.add(class_name)
print(f"Ontology analyzed: {ontology_path}")
print(f"Number of classes found: {len(classes)}")
print(f"Number of triplets analyzed: {triplets_count}")
return classes
def calculate_metrics(reference_classes, generated_classes):
all_classes = reference_classes.union(generated_classes)
y_true = [1 if cls in reference_classes else 0 for cls in all_classes]
y_pred = [1 if cls in generated_classes else 0 for cls in all_classes]
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
f1 = f1_score(y_true, y_pred)
return precision, recall, f1
#==============================================================================
# Main Method
#==============================================================================
def main():
# Load ontologies
ref_ontology = load_ontology(REFERENCE_ONTOLOGY_PATH)
gen_ontology = load_ontology(GENERATED_ONTOLOGY_PATH)
# Extract classes
ref_classes = get_classes(ref_ontology, REFERENCE_ONTOLOGY_PATH)
gen_classes = get_classes(gen_ontology, GENERATED_ONTOLOGY_PATH)
# Calculate metrics
precision, recall, f1 = calculate_metrics(ref_classes, gen_classes)
# Print results
print(f"Precision: {precision}")
print(f"Recall: {recall}")
print(f"F1 Score: {f1}")
if __name__ == "__main__":
main()
@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#> .
<https://tenet.tetras-libre.fr/extract-result#SolarSystem> a owl:Individual,
<https://tenet.tetras-libre.fr/extract-result#system>,
<https://tenet.tetras-libre.fr/extract-result#system-hasPart-object>,
<https://tenet.tetras-libre.fr/extract-result#system-hasPart-sun> ;
rdfs:label "SolarSystem" ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#direct> a owl:ObjectProperty ;
rdfs:label "direct" ;
rdfs:subPropertyOf ns1:Out_ObjectProperty ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#gravitation-bind-system> a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty <https://tenet.tetras-libre.fr/extract-result#bind> ;
owl:someValuesFrom <https://tenet.tetras-libre.fr/extract-result#system> ],
<https://tenet.tetras-libre.fr/extract-result#gravitation> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#hasManner> a owl:ObjectProperty ;
rdfs:label "hasManner" ;
rdfs:subPropertyOf ns1:Out_ObjectProperty ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#not-direct> a owl:ObjectProperty ;
rdfs:subPropertyOf ns1:Out_ObjectProperty ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#object-orbit-hasManner-direct-sun> a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty <https://tenet.tetras-libre.fr/extract-result#orbit-hasManner-direct> ;
owl:someValuesFrom <https://tenet.tetras-libre.fr/extract-result#sun> ],
<https://tenet.tetras-libre.fr/extract-result#object> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#object-orbit-hasManner-not-direct-sun> a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty <https://tenet.tetras-libre.fr/extract-result#orbit-hasManner-not-direct> ;
owl:someValuesFrom <https://tenet.tetras-libre.fr/extract-result#sun> ],
<https://tenet.tetras-libre.fr/extract-result#object> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#bind> a owl:ObjectProperty ;
rdfs:label "bind" ;
rdfs:subPropertyOf ns1:Out_ObjectProperty ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#gravitation> a owl:Class ;
rdfs:label "gravitation" ;
rdfs:subClassOf ns1:Entity ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#orbit-hasManner-direct> a owl:ObjectProperty ;
rdfs:subPropertyOf <https://tenet.tetras-libre.fr/extract-result#orbit> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#orbit-hasManner-not-direct> a owl:ObjectProperty ;
rdfs:subPropertyOf <https://tenet.tetras-libre.fr/extract-result#orbit> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#system-hasPart-object> a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty <https://tenet.tetras-libre.fr/extract-result#hasPart> ;
owl:someValuesFrom <https://tenet.tetras-libre.fr/extract-result#object> ],
<https://tenet.tetras-libre.fr/extract-result#system> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#system-hasPart-sun> a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty <https://tenet.tetras-libre.fr/extract-result#hasPart> ;
owl:someValuesFrom <https://tenet.tetras-libre.fr/extract-result#sun> ],
<https://tenet.tetras-libre.fr/extract-result#system> ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#hasPart> a owl:ObjectProperty ;
rdfs:label "hasPart" ;
rdfs:subPropertyOf ns1:Out_ObjectProperty ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#orbit> a owl:ObjectProperty ;
rdfs:label "orbit" ;
rdfs:subPropertyOf ns1:Out_ObjectProperty ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#object> a owl:Class ;
rdfs:label "object" ;
rdfs:subClassOf ns1:Entity ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#sun> a owl:Class ;
rdfs:label "sun" ;
rdfs:subClassOf ns1:Entity ;
ns1:fromStructure "unknown" .
<https://tenet.tetras-libre.fr/extract-result#system> a owl:Class ;
rdfs:label "system" ;
rdfs:subClassOf ns1:Entity,
ns1:Undetermined_Thing ;
ns1:fromStructure "unknown" .
@prefix base: <https://reference.tetras-libre.fr/base-ontology#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix result: <https://reference.tetras-libre.fr/expected-result#> .
result:SolarSystem a owl:Individual,
result:system,
result:system-isBindBy-gravitation,
result:system-hasPart-object-orbit-sun,
result:system-hasPart-sun ;
rdfs:label "SolarSystem" .
result:direct a owl:ObjectProperty ;
rdfs:label "direct" ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:gravitation-bind-system a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:bind ;
owl:someValuesFrom result:system ],
result:gravitation .
result:hasManner a owl:ObjectProperty ;
rdfs:label "hasManner" ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:not-direct a owl:ObjectProperty ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:object-orbit-sun a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:orbit ;
owl:someValuesFrom result:sun ],
result:object .
result:object-orbit-hasManner-direct-sun a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:orbit-hasManner-direct ;
owl:someValuesFrom result:sun ],
result:object-orbit-sun .
result:object-orbit-hasManner-not-direct-sun a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:orbit-hasManner-not-direct ;
owl:someValuesFrom result:sun ],
result:object-orbit-sun .
result:gravitation a owl:Class ;
rdfs:label "gravitation" ;
rdfs:subClassOf base:Entity .
result:system-isBindBy-gravitation a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:isBindBy ;
owl:someValuesFrom result:gravitation ],
result:system .
result:system-hasPart-object a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:hasPart ;
owl:someValuesFrom result:object ],
result:system .
result:system-hasPart-object-orbit-sun a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:hasPart ;
owl:someValuesFrom result:object-orbit-sun ],
result:system-hasPart-object .
result:system-hasPart-sun a owl:Class ;
rdfs:subClassOf [ a owl:Restriction ;
owl:onProperty result:hasPart ;
owl:someValuesFrom result:sun ],
result:system .
result:bind a owl:ObjectProperty ;
rdfs:label "bind" ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:isBindBy owl:inverseOf result:bind ;
rdfs:label "isBindBy" ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:orbit a owl:ObjectProperty ;
rdfs:label "orbit" ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:orbit-hasManner-direct a owl:ObjectProperty ;
rdfs:subPropertyOf result:orbit .
result:orbit-hasManner-not-direct a owl:ObjectProperty ;
rdfs:subPropertyOf result:orbit .
result:hasPart a owl:ObjectProperty ;
rdfs:label "hasPart" ;
rdfs:subPropertyOf base:Out_ObjectProperty .
result:object a owl:Class ;
rdfs:label "object" ;
rdfs:subClassOf base:Entity .
result:system a owl:Class ;
rdfs:label "system" ;
rdfs:subClassOf base:Entity.
result:sun a owl:Class ;
rdfs:label "sun" ;
rdfs:subClassOf base:Entity .
#!/usr/bin/python3.10
# -*-coding:Utf-8 -*
#==============================================================================
# ontoScorer: [brief description of the module]
#------------------------------------------------------------------------------
# Detailed module description, if needed
#==============================================================================
# TODO
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment