使用http://www.sparql.org/sparql.html运行此查询
prefix oxprop: <http://ophileon.com/ox/property#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
prefix gn: <http://www.geonames.org/ontology#>
select *
from <http://www.ophileon.com/ox/poi.rdf>
where
{
?poi rdfs:label ?poiname.
?poi owl:sameAs ?geonameuri.
SERVICE <http://factforge.net/sparql>{
?geonameuri gn:population ?population.
}
FILTER(langMatches(lang(?poiname), "EN")).
}返回
-------------------------------------------------------------------------------------------------------
| poi | poiname | geonameuri | population |
=======================================================================================================
| <http://ophileon.com/ox/poi/2> | "Wageningen"@en | <http://sws.geonames.org/2745088/> | "35433" |
| <http://ophileon.com/ox/poi/3> | "Netherlands"@en | <http://sws.geonames.org/2750405/> | "16645000" |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "767457" |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "741636" |
-------------------------------------------------------------------------------------------------------即具有多个人口值。显然来自不同的图表,而事实伪造是在质疑。是否有一种方法可以限制factforge或将其排序到例如geonames图?顺便说一下,geonames没有提供一个开放的SPARQL端点,这就是我使用Factforge的原因。
发布于 2013-10-16 23:25:29
让我们从稍微更改查询开始。让我们强制?poiname成为"Amsterdam"@en,这样我们只会得到有问题的结果:
prefix oxprop: <http://ophileon.com/ox/property#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
prefix gn: <http://www.geonames.org/ontology#>
select *
from <http://www.ophileon.com/ox/poi.rdf>
where
{
values ?poiname { "Amsterdam"@en }
?poi rdfs:label ?poiname.
?poi owl:sameAs ?geonameuri.
SERVICE <http://factforge.net/sparql> {
?geonameuri gn:population ?population.
}
FILTER(langMatches(lang(?poiname), "EN")).
}SPARQL结果
现在,我们可以将service块中的查询包装在graph ?g { ... }中,以找出这些三元组的来源。也就是说,我们现在有:
SERVICE <http://factforge.net/sparql> {
graph ?g { ?geonameuri gn:population ?population. }
}SPARQL结果
----------------------------------------------------------------------------------------------------------------------------
| poiname | poi | geonameuri | population | g |
============================================================================================================================
| "Amsterdam"@en | <http://ophileon.com/ox/poi/1> | <http://sws.geonames.org/2759794/> | "741636" | <http://nytimes.com> |
----------------------------------------------------------------------------------------------------------------------------现在只有一个结果;似乎另一个结果是在默认图中。
可以通过这种方式使用graph关键字指定要查询的图形。详细信息在SPARQL1.1建议的13.3查询数据集中进行了描述。
通过在查询中使用graph ?g { },您将强制数据位于一个命名图中(也就是说,您将不再从默认图中获取三元组)。不幸的是,这似乎删除了一些您想要的结果。例如,将此应用于原始查询(不限于阿姆斯特丹):
prefix oxprop: <http://ophileon.com/ox/property#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
prefix gn: <http://www.geonames.org/ontology#>
select *
from <http://www.ophileon.com/ox/poi.rdf>
where
{
?poi rdfs:label ?poiname.
?poi owl:sameAs ?geonameuri.
SERVICE <http://factforge.net/sparql>{
graph ?g { ?geonameuri gn:population ?population. }
}
FILTER(langMatches(lang(?poiname), "EN")).
}SPARQL结果
------------------------------------------------------------------------------------------------------------------------------
| poi | poiname | geonameuri | population | g |
==============================================================================================================================
| <http://ophileon.com/ox/poi/3> | "Netherlands"@en | <http://sws.geonames.org/2750405/> | "16645000" | <http://nytimes.com> |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "741636" | <http://nytimes.com> |
------------------------------------------------------------------------------------------------------------------------------只给出两个结果;您不再有Wageningen的结果。您可以尝试使用或不使用图表来询问结果,使用
{ graph ?g { ?geonameuri gn:population ?population. } }
union
{ ?geonameuri gn:population ?population. }SPARQL结果
------------------------------------------------------------------------------------------------------------------------------
| poi | poiname | geonameuri | population | g |
==============================================================================================================================
| <http://ophileon.com/ox/poi/2> | "Wageningen"@en | <http://sws.geonames.org/2745088/> | "35433" | |
| <http://ophileon.com/ox/poi/3> | "Netherlands"@en | <http://sws.geonames.org/2750405/> | "16645000" | <http://nytimes.com> |
| <http://ophileon.com/ox/poi/3> | "Netherlands"@en | <http://sws.geonames.org/2750405/> | "16645000" | |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "741636" | <http://nytimes.com> |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "767457" | |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "741636" | |
------------------------------------------------------------------------------------------------------------------------------现在我们把数据看得更清楚了。我们不能肯定地说,但是看起来nytime数据在默认图中是复制的,在荷兰是好的,否则可能没有值,但是在阿姆斯特丹,默认图已经有了一个值,并且它与命名图中的值不同。
因此,直接的答案是,您可以控制查询哪些图形,但在这种情况下,根本不清楚您想要使用哪些数据。最好按每个位置预期相同的值进行分组,然后组合人口会以某种方式进行(例如,取最大值或最小值,或将它们连接起来,或者其他什么东西)。例如,(请注意,我们添加了一个xsd:前缀,用于向xsd:integer进行转换,而?population值是字符串,因此需要转换到xsd:integer以求平均值):
prefix oxprop: <http://ophileon.com/ox/property#>
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix owl: <http://www.w3.org/2002/07/owl#>
prefix wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
prefix gn: <http://www.geonames.org/ontology#>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
select
?poi
?poiname
?geonameuri
(min(?population) as ?minPopulation)
(max(?population) as ?maxPopulation)
(group_concat(?population;separator=' ') as ?allPopulations)
(avg(xsd:integer(?population)) as ?avgPopulation)
(sample(?population) as ?somePopulation)
from <http://www.ophileon.com/ox/poi.rdf>
where
{
?poi rdfs:label ?poiname.
?poi owl:sameAs ?geonameuri.
SERVICE <http://factforge.net/sparql> {
?geonameuri gn:population ?population.
}
FILTER(langMatches(lang(?poiname), "EN")).
}
group by ?poi ?poiname ?geonameuriSPARQL结果
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| poi | poiname | geonameuri | minPopulation | maxPopulation | allPopulations | avgPopulation | somePopulation |
=============================================================================================================================================================================
| <http://ophileon.com/ox/poi/2> | "Wageningen"@en | <http://sws.geonames.org/2745088/> | "35433" | "35433" | "35433" | 35433.0 | "35433" |
| <http://ophileon.com/ox/poi/3> | "Netherlands"@en | <http://sws.geonames.org/2750405/> | "16645000" | "16645000" | "16645000" | 16645000.0 | "16645000" |
| <http://ophileon.com/ox/poi/1> | "Amsterdam"@en | <http://sws.geonames.org/2759794/> | "741636" | "767457" | "767457 741636" | 754546.5 | "767457" |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------https://stackoverflow.com/questions/19412208
复制相似问题