首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用python3.6中的owlReady库从protege查询owl文件

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

Stack Overflow用户
提问于 2017-11-04 14:46:26
回答 1查看 3.4K关注 0票数 1

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

EN

回答 1

Stack Overflow用户

发布于 2017-11-04 15:45:38

这里使用的是python3.6中的python3.6中的python owlReady 2库。

要执行此代码,必须将owlReady 2rdflib库附加到项目中。

在py魅力中,这些库可以立即通过IDE下载

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

intellij设置

代码语言:javascript
复制
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()
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47111939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档