如何使用python进行语义搜索和本体搜索工作?

时间:2019-07-30 13:15:06

标签: python-3.x owl semantic-web ontology rdflib

我正在构建一个简单的在线语义搜索引擎来查找工作,我发现了一个读取本地猫头鹰文件的简单程序 但我希望它可以在线使用语义网和链接数据来罚款工作和雇主

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

我试图像文档中提到的那样使用RDFlib

import rdflib
g=rdflib.Graph()
g.load('http://dbpedia.org/resource/Semantic_Web')

for s,p,o in g:
    print s,p,o

我应该如何获取有关工作的数据和链接? 或关于雇主或公司?

我应该怎么写猫头鹰文件?

1 个答案:

答案 0 :(得分:1)

schema.org具有JobPosting规范。如果幸运的话,您会发现一些网站在使用它并且使用得很好。根据它们的操作方式(在链接的文档中),您可以将其刮到自己的图形中。这样至少可以节省编写本体的时间。

我只看了一个工作网站:Monster.com,它们足以放置模式lists in JSON-LD on their collection pages, line 1187 in the linked sourceSchema JobPostings on the linked pages, line 261 in the linked source

如果同时安装了rdflibrdflib-jsonld点,则很简单:

from rdflib import Graph
g = Graph()
g.parse("https://job-openings.monster.com/software-engineer-principal-software-engineer-northridge-ca-us-northrop-grumman/0d2caa9e-3b3c-46fa-94d1-cddc75d9ae27")

# Demo
print(len(g))
for s, p, o in g:
    print(s, p, o)