I am working on a RAG Streamlit application that uploads an ontology file (RDF format), parses it using Owlready2, and populates a Neo4j graph database. The goal is to extract classes, object properties (relationships), and data properties (attributes) from the ontology to create nodes with detailed properties and relationships. After that you can chat with the data of the ontology
Things i have done:
- Parsed classes and object properties using ontology.classes() and ontology.object_properties().
- Created nodes for classes and relationships for object properties in Neo4j using Cypher queries
(Minimal Code Example)
from owlready2 import get_ontology
from neo4j import GraphDatabase
def parse_ontology_and_create_graph(ontology_path, session):
ontology = get_ontology(ontology_path).load()
# Create nodes for classes
for cls in ontology.classes():
session.run("CREATE (:Class {name: $name})", name=cls.name)
# Create relationships for object properties
for prop in ontology.object_properties():
for domain in prop.domain:
for range_ in prop.range:
session.run("""
MATCH (d:Class {name: $domain_name}), (r:Class {name: $range_name})
CREATE (d)-[:`{relationship}`]->(r)
""", domain_name=domain.name, range_name=range_.name, relationship=prop.name)
Problems I’m Facing
Missing Data Properties: Some Attributes are defined in the ontology are not included in the exported graph maybe because my code does not handle data properties.
Missing Relationships: Some Relationships are specified in the ontology but are missing from the graph. My code only handles basic object properties and might be incomplete.
Name Mismatches: Some relationships are incorrectly named in Neo4j, which does not exist in the ontology. This might be caused by a misstep in the mapping logic.