python中用rdflib生成rdf,用sparql查询

根据原作者:利用RDFLib的SPARQL进行查询的一个例子进行了改写。
其他资料:
rdflib中SPARQL如何判断得到的结果为空
使用 SPARQL 查询 RDF

Python RDF知识库查询

接下来,上我的代码~ 其实,根据三元组的模式<a,?,b>与sparql语句就可发现规律,可以任意改写啦

# coding:utf-8

import rdflib

def create():
    g = rdflib.Graph()
    has_border_with = rdflib.URIRef('http://www.example.org/has_border_with')
    located_in = rdflib.URIRef('http://www.example.org/located_in')
    ##############
    germany = rdflib.URIRef('http://www.example.org/country1')
    france = rdflib.URIRef('http://www.example.org/country2')
    china = rdflib.URIRef('http://www.example.org/country3')
    mongolia = rdflib.URIRef('http://www.example.org/country4')
    ##############
    europa = rdflib.URIRef('http://www.example.org/part1')
    asia = rdflib.URIRef('http://www.example.org/part2')
    ##############
    g.add((germany, has_border_with, france))
    g.add((china, has_border_with, mongolia))
    g.add((germany, located_in, europa))
    g.add((france, located_in, europa))
    g.add((china, located_in, asia))
    g.add((mongolia, located_in, asia))
    ##############
    # c3,has,c4
    # c3,loc,p2
    g.serialize("graph.rdf")

def query():
    g = rdflib.Graph()
    g.parse("graph.rdf", format="xml")
    ################################
    # <a,?,?>
    q = "select ?relation ?part where { <http://www.example.org/country1> ?relation ?part}"
    x = g.query(q)
    t = list(x) ##### 二维
    # print(t[0][0])
    # http://www.example.org/has_border_with
    # print(t[0][1])
    # http://www.example.org/part1
    print(len(t))  #没有,则=0
    print(t[0])
    # <?,b,?>
    q = "select ?country ?part where {?country <http://www.example.org/located_in> ?part}"
    x = g.query(q)
    t = list(x)
    print(len(t))
    print(t[0])
    # <?,?,c>
    q = "select ?country ?relation where {?country ?relation <http://www.example.org/part1>}"
    x = g.query(q)
    t = list(x)
    print(len(t))
    print(t[0])
    ################################
    # <a,b,?>
    q = "select ?part where {<http://www.example.org/country1> <http://www.example.org/located_in> ?part}"
    x = g.query(q)
    t = list(x) ######二维: n*1
    print(len(t))
    # print(t[0][0])
    # http://www.example.org/part1
    print(t[0])
    # <a,?,c>
    # <?,a,b>

if __name__ == "__main__":
    create()
    query()

graph.rdf:

<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF
   xmlns:ns1="http://www.example.org/"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
>
  <rdf:Description rdf:about="http://www.example.org/country3">
    <ns1:located_in rdf:resource="http://www.example.org/part2"/>
    <ns1:has_border_with rdf:resource="http://www.example.org/country4"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.org/country1">
    <ns1:has_border_with rdf:resource="http://www.example.org/country2"/>
    <ns1:located_in rdf:resource="http://www.example.org/part1"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.org/country2">
    <ns1:located_in rdf:resource="http://www.example.org/part1"/>
  </rdf:Description>
  <rdf:Description rdf:about="http://www.example.org/country4">
    <ns1:located_in rdf:resource="http://www.example.org/part2"/>
  </rdf:Description>
</rdf:RDF>

  • 10
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,我明白了你的问题。首先,我们来安装 `rdflib-endpoint`。 安装命令如下: ``` pip install rdflib-endpoint ``` 安装完成后,我们可以使用以下命令启动一个电影 RDF 数据的 SPARQL 查询服务: ``` python -m rdflib_endpoint movie_data.ttl --port 8000 ``` 其中 `movie_data.ttl` 是电影 RDF 数据的 Turtle 文件路径,`--port` 参数指定了服务监听的端口号为 8000。 接下来,我为你提供一些 SPARQL 查询语句: 1. 查询所有电影的名称和评分: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX movie: <http://example.org/movie#> SELECT ?name ?rating WHERE { ?movie rdf:type movie:Movie . ?movie movie:name ?name . ?movie movie:rating ?rating . } ``` 2. 查询所有导演及其执导的电影数量: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX movie: <http://example.org/movie#> SELECT ?director (COUNT(?movie) AS ?count) WHERE { ?movie rdf:type movie:Movie . ?movie movie:director ?director . } GROUP BY ?director ``` 3. 查询所有演员及其出演的电影数量: ```sparql PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX movie: <http://example.org/movie#> SELECT ?actor (COUNT(?movie) AS ?count) WHERE { ?movie rdf:type movie:Movie . ?movie movie:actor ?actor . } GROUP BY ?actor ``` 使用 `sparqlwrapper` 库连接本地 SPARQL 服务并向其发送查询可以使用以下 Python 代码: ```python from SPARQLWrapper import SPARQLWrapper, JSON # 创建 SPARQLWrapper 实例,设置服务地址和端口号 sparql = SPARQLWrapper("http://localhost:8000/sparql") # 设置查询语句 query = """ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX movie: <http://example.org/movie#> SELECT ?name ?rating WHERE { ?movie rdf:type movie:Movie . ?movie movie:name ?name . ?movie movie:rating ?rating . } """ # 将查询语句设置到 SPARQLWrapper 实例中 sparql.setQuery(query) # 指定查询结果格式为 JSON sparql.setReturnFormat(JSON) # 向 SPARQL 服务发送查询请求,并获取查询结果 results = sparql.query().convert() # 解析查询结果 for result in results["results"]["bindings"]: name = result["name"]["value"] rating = result["rating"]["value"] # 在 HTML 中展示查询结果 print(f"<p>{name}: {rating}</p>") ``` 你可以根据需要修改查询语句和展示结果的方式。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值