【问题标题】:How to query a owl file from protege using owlReady library in python 3.6如何在 python 3.6 中使用 owlReady 库从 protege 中查询 owl 文件
【发布时间】:2017-11-04 14:46:26
【问题描述】:

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

【问题讨论】:

    标签: python python-3.x owl protege owlready


    【解决方案1】:

    这里,在 python 3.6 中使用了 python owlReady 2 库

    您必须将 owlReady 2 和 rdflib 库附加到项目才能执行此代码


    在 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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2014-12-03
      • 1970-01-01
      相关资源
      最近更新 更多