简体   繁体   English

如何检索由 rdflib 加载的本体的命名空间

[英]how to retrieve the namespace of an ontology loaded by rdflib

I created an ontology and now I want to read it using rdflib.我创建了一个本体,现在我想使用 rdflib 阅读它。 the question is how to get the URI (namespace) or the prefix of the loaded graph with out manually inspecting the ontology as below:问题是如何在不手动检查本体的情况下获取 URI(命名空间)或加载图形的前缀,如下所示:

from rdflib import *

rdf_address="C:/Users/eh/ontologies/RNO_V5042/NVDB_RNO_V5042_RDF.owl"
g=Graph()
g.parse(rdf_address)

for namespace in g.namespaces():
    print namespace

rno = Namespace("http://www.semanticweb.org/eh/ontologies/2015/3/RNO_V5042#") ###### **how to find this namespace?**

aClass = rno.Node
roundabout= rno.Roundabout
namedIndividual = URIRef('http://www.w3.org/2002/07/owl#NamedIndividual')
rdftype = URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
for triple in g.triples((None,rdftype,rno.Roundabout)):
    print triple

Having a rdflib Graph, parse() it and use MyNameSpace(g).有一个 rdflib Graph,parse() 它并使用 MyNameSpace(g)。 This gives the namespace for empty prefix, ie, ":".这给出了空前缀的命名空间,即“:”。

from rdflib import Graph

g = Graph() 
cOntoFile = "MyOnto.ttl"
g.parse(cOntoFile, format="turtle")

print(MyNameSpace(g))

def MyNameSpace(g):
  for ns_prefix, namespace in g.namespaces():
    if not ns_prefix:
      return namespace
  return ""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM