限制 Factforge sparql 端点使用的图 [英] Limiting graphs to be used by Factforge sparql endpoint

查看:40
本文介绍了限制 Factforge sparql 端点使用的图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 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 正在查询的不同图表.有没有办法限制或优先考虑 factforge 到例如 geonames 图?顺便说一句,geonames 不提供开放的 SPARQL 端点,这就是我使用 Factforge 的原因.

i.e. with multiple values for population. Apparently coming from different graphs that factforge is querying. Is there a way to limit or prioritize factforge to for instance the geonames graph ? BTW, geonames does not provide an open SPARQL endpoint, that's why I'm using Factforge.

推荐答案

让我们从稍微更改您的查询开始.让我们强制 ?poiname"Amsterdam"@en,这样我们只会得到有问题的结果:

Let's start by changing your query just a little bit. Let's force ?poiname to be "Amsterdam"@en, so that we're only getting the problematic results:

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 { ... } 中,以找出这些三元组的来源.也就是说,我们现在有:

Now, we can wrap the query that's in the service block inside graph ?g { ... } to find out where those triples are coming from. That is, we now have:

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

现在只有一个结果;似乎另一个结果在默认图中.

There's only one result now; it seems that the other result was in the default graph.

您可以通过这种方式使用 graph 关键字来指定要查询的图形.SPARQL 1.1 建议的13.3 查询数据集中描述了详细信息.

You can specify which graphs you want to query by using the graph keyword this way. The details are described in 13.3 Querying the Dataset of the SPARQL 1.1 Recommendation.

通过在查询中使用 graph ?g { },您将强制数据位于命名图中(即,您将不再从默认图中获取三元组).不幸的是,这似乎删除了您想要的一些结果.例如,将此应用于您的原始查询(不限于阿姆斯特丹):

By using the graph ?g { } in the query, you're forcing the data to be in a named graph (i.e., you won't be getting triples from the default graph anymore). This seems to remove some of the results that you wanted, unfortunately. E.g., Applying this to you original query (not restricted to Amsterdam):

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

只给出两个结果;你不再有瓦赫宁根的结果.您可以尝试使用或不使用图表来询问结果,使用

gives just two results; you no longer have a result for Wageningen. You can try asking for results with and without a graph, using

{ 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"   |                      |
------------------------------------------------------------------------------------------------------------------------------

现在我们更清楚地看到了数据.我们不能肯定,但看起来 nytimes 数据在默认图表中重复,这在荷兰的情况下很好,否则可能没有值,但在阿姆斯特丹的情况下很糟糕,默认图表已经有一个值,并且与命名图中的值不同.

and now we see the data a bit more clearly. We can't say for sure, but it looks like the nytimes data is duplicated in the default graph, which is good in the case of Netherlands which might not have a value otherwise, but bad in the case of Amsterdam, where the default graph already has a value, and it differs from the one in the named graph.

那么,直接的答案是您可以控制查询哪些图表,但在这种情况下,您根本不清楚要使用哪些数据.您最好按每个位置都相同的值进行分组,然后以某种方式组合总体结果(例如,取最大值、最小值、或连接它们或其他方式.例如,(请注意,我们添加了一个 xsd: 前缀用于转换为 xsd:integer,并且 ?population 值是字符串,因此需要转换到 xsd:integer 以取平均值):

The direct answer, then, is yes you can control which graphs are queried, but in the case it's not at all clear what data you'd want to use. You might be better off grouping by the values that expect to be the same for each location, and then combining the population results in some fashion (e.g., taking the maximum, or the minimum, or concatenating them, or something else. E.g., (note that we added a xsd: prefix for casting to xsd:integer, and that the ?population values are strings, hence the need for casting to xsd:integer in order to take the average):

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 ?geonameuri

SPARQL 结果

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 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"       |
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

这篇关于限制 Factforge sparql 端点使用的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆