如何使用 SPARQL 查询本地 ttl 文件?

问题描述

我正在学习如何查询我从维基百科检索的 ttl 文件。 但是,我坚持使用正确的查询显示我寻求答案的结果。

这是我使用的示例 ttl 文件

# File name : WikipediaGraph.ttl
@prefix ns1: <http://www.example.com/> .

<http://en.wikipedia.org/wiki/Abilene,_Texas> ns1:2010population "117063" ;
    ns1:2019population "123420" ;
    ns1:areaSqKm "276.4" ;
    ns1:areaSqMile "106.7" ;
    ns1:popChange "5.43" ;
    ns1:popPerSqKm "442" ;
    ns1:popPerSqMile "1146" ;
    ns1:state <http://en.wikipedia.org/wiki/Texas> .

下面列出了我想得到答案的示例问题。

  • 列表中哪个州的城市最多? *
  • 列表中哪个州的城市人口最多? *
  • 列表中哪些州的城市增长最快? *

因此,我从一开始就卡住了,因为我无法从简单的查询中得到结果。

import rdflib
from rdflib import Graph,URIRef,Literal,Namespace,RDF

g=rdflib.Graph()
g.parse("WikipediaGraph.ttl",format='turtle')

results = g.query("""
    PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-Syntax-ns#>
    PREFIX ns1: <http://www.example.com/>
    SELECT ?v
    WHERE {
    ?v rdf:type ns1:state .
    }
    ORDER BY ?NAME
""")

print('Results!')
for row in results:
    print(row)

你能告诉我应该如何写来查询上述问题吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)