cancel
Showing results for 
Search instead for 
Did you mean: 

how to import ontology to Neo4j?

fan_123
Node Link

I am referring to the tutorials which are provided by goingMeta. I would like to import the ontology and align the ontology with my CSV instance file. First, I tried this code

ttps://github.com/jbarrasa/goingmeta/blob/main/session5/Ontology_Driven_KG_creation.ipynb

However, when I run this code, 

 

 

# processing the ontology...
 
g = rdflib.Graph()
g.parse(path, format='turtle')

simple_query = """
prefix owl: <http://www.w3.org/2002/07/owl#> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

SELECT DISTINCT ?c
  WHERE {
    ?c rdf:type owl:Class .    
  } """

for row in g.query(simple_query):
    print(str(row.c), getLocalPart(str(row.c)), getNamespacePart(str(row.c)))

 

 

I met this error. 

fan_123_0-1662750346429.png

 

My turtle file is like this 

https://github.com/mf093087/Ontology/blob/main/09092022_Test.ttl

 

Besides, if I can't use the code which is provided by goingmeta training session. I wanted to use data importer to import ontology, however, I met two problems.

1. I don't know how to convert my turtle file to a format that the data importer can load.

2. I can't use Neo4j sandbox now. I can use Neo4j aura DB, but I can't connect to the data importer. Is the server closed?

2 REPLIES 2

fan_123
Node Link

Besides, when I run this. 

# read the onto and generate cypher (complete without mappings)

g = rdflib.Graph()
g.parse(path, format='turtle')

classes_and_props_query = """
prefix owl: <http://www.w3.org/2002/07/owl#> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

SELECT DISTINCT ?curi (GROUP_CONCAT(DISTINCT ?propTypePair ; SEPARATOR=",") AS ?props)
WHERE {
    ?curi rdf:type owl:Class .
    optional { 
      ?prop rdfs:domain ?curi ;
        a owl:DatatypeProperty ;
        rdfs:range ?range .
      BIND (concat(str(?prop),';',str(?range)) AS ?propTypePair)
    }
  } GROUP BY ?curi  """

cypher_list = []

for row in g.query(classes_and_props_query):
    cypher = []
    cypher.append("unwind $records AS record")
    cypher.append("merge (n:" + getLocalPart(row.curi) + " { `<id_prop>`: record.`<col with id>`} )")
    for pair in row.props.split(","):
      propName = pair.split(";")[0]
      propType = pair.split(";")[1]
      cypher.append("set n." + getLocalPart(propName) + " = record.`<col with value for " + getLocalPart(propName) + ">`")
    cypher.append("return count(*) as total") 
    cypher_list.append(' \n'.join(cypher))


rels_query = """
prefix owl: <http://www.w3.org/2002/07/owl#> 
prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 

SELECT DISTINCT ?rel ?dom ?ran #(GROUP_CONCAT(DISTINCT ?relTriplet ; SEPARATOR=",") AS ?rels)
WHERE {
    ?rel a ?propertyClass .
    filter(?propertyClass in (rdf:Property, owl:ObjectProperty, owl:FunctionalProperty, owl:AsymmetricProperty, 
           owl:InverseFunctionalProperty, owl:IrreflexiveProperty, owl:ReflexiveProperty, owl:SymmetricProperty, owl:TransitiveProperty))
    
    ?rel rdfs:domain ?dom ;
      rdfs:range ?ran .
    
    #BIND (concat(str(?rel),';',str(?dom),';',str(?range)) AS ?relTriplet)
    
  }"""


for row in g.query(rels_query):
  cypher = []
  cypher.append("unwind $records AS record")
  cypher.append("match (source:" + getLocalPart(row.dom) + " { `<id_prop>`: record.`<col with source id>`} )")
  cypher.append("match (target:" + getLocalPart(row.ran) + " { `<id_prop>`: record.`<col with target id>`} )")
  cypher.append("merge (source)-[r:`"+ getLocalPart(row.rel) +"`]->(target)")
  cypher.append("return count(*) as total") 
  cypher_list.append(' \n'.join(cypher))

for q in cypher_list:
  print("\n\n" + q)

I met this error. 

fan_123_0-1662751158733.png

 

RabbitB
Node

Hello! I'm a complete newbie, I'm also following the same tutorial and running into the same errors. Did you figure out how to fix them? 

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.