如何使用python3.6中的owlReady库从protege查询owl文件

2024-09-29 23:32:59 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用pythonowlready 2库从protege加载和查询owl文件


Tags: 文件owlprotegepythonowlready
1条回答
网友
1楼 · 发布于 2024-09-29 23:32:59

这里,使用了python3.6中的python owlReady 2库

为了执行这段代码,您必须将owlReady 2rdflib库附加到项目中


在pycharm中,可以通过IDE立即下载这些库

“文件”>;“设置”>;“搜索”“项目解释器”>;单击“+”标记,然后搜索库并逐个安装

intellij settings


from owlready2 import *


class SparqlQueries:
def __init__(self):
    my_world = World()
    my_world.get_ontology("file://ExampleOntolohy.owl").load() #path to the owl file is given here
    sync_reasoner(my_world)  #reasoner is started and synchronized here
    self.graph = my_world.as_rdflib_graph()

def search(self):
    #Search query is given here
    #Base URL of your ontology has to be given here
    query = "base <http://www.semanticweb.org/ExampleOntology> " \
            "SELECT ?s ?p ?o " \
            "WHERE { " \
            "?s ?p ?o . " \
            "}"

    #query is being run
    resultsList = self.graph.query(query)

    #creating json object
    response = []
    for item in resultsList:
        s = str(item['s'].toPython())
        s = re.sub(r'.*#',"",s)

        p = str(item['p'].toPython())
        p = re.sub(r'.*#', "", p)

        o = str(item['o'].toPython())
        o = re.sub(r'.*#', "", o)
        response.append({'s' : s, 'p' : p, "o" : o})

    print(response) #just to show the output
    return response


runQuery = SparqlQueries()
runQuery.search()

相关问题 更多 >

    热门问题