gpt4 book ai didi

python - 使用本地 RDF 存储的 SPARQL 查询

转载 作者:行者123 更新时间:2023-12-01 05:45:58 27 4
gpt4 key购买 nike

我正在尝试使用本地 RDF 图创建 SPARQL 查询。但它不起作用。我已经包含了我的代码,下面是我的代码。

我有两个类(class),分别称为“学生”和“大学”。学生类有两个属性(enrolledOn 和 StudiesAt)。 University 类还有两个属性(UniversityLocation 和 UniversityRanking)。此外,我还输入了一些数据(RDF 三元组)。 Student 类和 University 类各有三个数据实体。

我的 SPARQL 查询位于底部。我想选择所有在排名前 10 的大学学习的学生。但目前,我的 SPARQL 查询没有返回任何内容。该查询应返回 Khalil 和 Ahmed。

任何帮助将不胜感激。谢谢。

我的代码:

import rdfextras
import rdflib
from rdflib.graph import Graph, Store, URIRef, Literal
from rdflib.namespace import Namespace, RDFS
from rdflib import plugin
from SPARQLWrapper import SPARQLWrapper, JSON

rdflib.plugin.register('sparql', rdflib.query.Processor,
'rdfextras.sparql.processor', 'Processor')
rdflib.plugin.register('sparql', rdflib.query.Result,
'rdfextras.sparql.query', 'SPARQLQueryResult')


#=====================data for STUDENT class==============================
rdf_xml_Student_data = """<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:Student="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student#">

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student/Harith">
<Student:enrolledOn>MScComputerScience</Student:enrolledOn>
<Student:studiesAt>Queen_Mary</Student:studiesAt>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student/Khalil">
<Student:enrolledOn>BScComputerScience</Student:enrolledOn>
<Student:studiesAt>Oxford_University</Student:studiesAt>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student/Ahmed">
<Student:enrolledOn>BScComputerScience</Student:enrolledOn>
<Student:studiesAt>Oxford_University</Student:studiesAt>
</rdf:Description>

</rdf:RDF>
"""


#=====================data for UNIVERSITY class==============================
rdf_xml_University_data = """<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:University="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University#">

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University/Queen_Mary">
<University:UniversityLocation>London</University:UniversityLocation>
<University:UniversityRanking>36</University:UniversityRanking>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University/City_University">
<University:UniversityLocation>London</University:UniversityLocation>
<University:UniversityRanking>43</University:UniversityRanking>
</rdf:Description>

<rdf:Description rdf:about="http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University/Oxford_University">
<University:UniversityLocation>Oxford</University:UniversityLocation>
<University:UniversityRanking>2</University:UniversityRanking>
</rdf:Description>

</rdf:RDF>
"""


# -- (part1) create and RDF store in memory --
memory_store = plugin.get('IOMemory', Store)()
graph_id = URIRef(u'http://example.com/foo')
g = Graph(store=memory_store, identifier=graph_id)
g.bind('ex','http://example.com/')

g.parse(data=rdf_xml_Student_data, format="application/rdf+xml")
g.parse(data=rdf_xml_University_data, format="application/rdf+xml")




#===========================SPARQL QUERY====================================
# QUERY - select all students who study at top 10 ranked universities
results = g.query("""PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX student: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student#>
PREFIX university: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University#>

SELECT ?stu
WHERE { ?uni university:UniversityRanking ?UniversityRanking.
?stu student:studiesAt ?uni.
FILTER ( ?UniversityRanking < 10)
}
""")



print("\n============QUERY RESULTS===============\n")
for row in results.result:
print(row)

这就是我运行上述代码后三元组在图中的存储方式:

=========================STUDENT class==================================
Subject Predicate Object
========================================================================
Harith enrolledOn MScComputerScience
Harith studiesAt Queen_Mary
Khalil enrolledOn BScComputerScience
Khalil studiesAt Oxford_University
Ahmed enrolledOn BScComputerScience
Ahmed studiesAt Oxford_University


=============================UNIVERSITY class=======================
Subject Predicate Object
===============================================================
Queen_Mary UniversityLocation London
Queen_Mary UniversityRanking 36
City_University UniversityLocation London
City_University UniversityRanking 43
Oxford_University UniversityLocation Oxford
Oxford_University UniversityRanking 2

最佳答案

这并不是一个真正的答案,但除非您的 RDFLib 使用超过 3-4 年,否则您的代码可以简单得多:

from rdflib import Graph

#=====================data for STUDENT class==============================
rdf_xml_Student_data = """<?xml version="1.0"?> ... <snip>"""

#=====================data for UNIVERSITY class==============================
rdf_xml_University_data = """<?xml version="1.0"?> ... <snip>"""


# -- (part1) create and RDF store in memory --
g = Graph()

g.parse(data=rdf_xml_Student_data)
g.parse(data=rdf_xml_University_data)


#===========================SPARQL QUERY====================================
# QUERY - select all students who study at top 10 ranked universities
results = g.query("""PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX student: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/Person/Student#>
PREFIX university: <http://www.semanticweb.org/ontologies/2013/2/Coursework.owl/Thing/University#>

SELECT ?stu
WHERE { ?uni university:UniversityRanking ?UniversityRanking.
?stu student:studiesAt ?uni.
FILTER ( ?UniversityRanking < 10)
}
""")



print("\n============QUERY RESULTS===============\n")
for row in results.result:
print(row)

关于python - 使用本地 RDF 存储的 SPARQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16123096/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com