如何在Python中解析和加载一个本体论?

问题描述 投票:3回答:1

我在一个'owl'文件(nif.owl)里有一个本体。我熟悉Java,但它一直在崩溃;因此,我尝试使用Python。然而,由于我以前没有使用过Python,我不确定我是否正确地加载了本体!这里是我认为正确的部分。

这里是我认为与加载本体有关的部分。

g = rdflib.Graph()
g.parse ('nif.owl', format='xml')
nif = rdflib.Namespace('http://purl.org/nif/ontology/nif.owl')
g.bind('nif', nif)

我认为问题出在g.parse将格式设置为 "xml "的地方。我想可能是'xml'的格式有误。

我还把本体文件的头作为图片附上。

enter image description here

我认为代码出错的原因是我得到的结果,我在下面的图片中显示了。enter image description here

谢谢!

PS:下面是完整的代码,以防有什么问题。

import logging
import rdflib
import time

logging.basicConfig()
logger = logging.getLogger('logger')
logger.warning('The system may break down')

start_time = time.time()

g = rdflib.Graph()
g.parse ('nif.owl', format='xml')
nif = rdflib.Namespace('http://purl.org/nif/ontology/nif.owl')
g.bind('nif', nif)
query = """
select distinct ?p 
where { ?s ?p ?o}
        LIMIT 5
        """
result = g.query(query)
print(result.serialize(format='csv'))

print("--- %s seconds ---" % (time.time() - start_time))
python sparql rdf
1个回答
1
投票

你的代码没有什么问题,除了格式应该是...。format='application/rdf+xml'.

© www.soinside.com 2019 - 2024. All rights reserved.