【问题标题】:Limiting graphs to be used by Factforge sparql endpoint限制 Factforge sparql 端点使用的图表
【发布时间】:2013-10-16 19:36:13
【问题描述】:

使用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 不提供开放的 SPARQL 端点,这就是我使用 Factforge 的原因。

【问题讨论】:

    标签: service rdf sparql


    【解决方案1】:

    让我们从稍微更改您的查询开始。让我们将?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 results

    现在,我们可以将 service 块中的查询包装在 graph ?g { ... } 中,以找出这些三元组的来源。也就是说,我们现在有:

       SERVICE <http://factforge.net/sparql> {
         graph ?g { ?geonameuri gn:population ?population. }
       }
    

    SPARQL results

    ----------------------------------------------------------------------------------------------------------------------------
    | poiname        | poi                            | geonameuri                         | population | g                    |
    ============================================================================================================================
    | "Amsterdam"@en | <http://ophileon.com/ox/poi/1> | <http://sws.geonames.org/2759794/> | "741636"   | <http://nytimes.com> |
    ----------------------------------------------------------------------------------------------------------------------------
    

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

    您可以通过这种方式使用graph 关键字指定要查询的图形。详细信息在 SPARQL 1.1 建议的13.3 Querying the Dataset 中进行了描述。

    通过在查询中使用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 results

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

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

    { graph ?g { ?geonameuri gn:population ?population. } }
    union
    { ?geonameuri gn:population ?population. }
    

    SPARQL results

    ------------------------------------------------------------------------------------------------------------------------------
    | 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 数据在默认图表中重复,这在荷兰的情况下很好,否则可能没有值,但在阿姆斯特丹的情况下很糟糕,默认图表已经有一个值,并且与命名图中的值不同。

    那么,直接的答案是,您可以控制查询哪些图表,但在这种情况下,您完全不清楚您想要使用哪些数据。您最好按每个位置预期相同的值进行分组,然后以某种方式组合总体结果(例如,取最大值或最小值,或将它们连接起来,或其他方式。例如,(请注意,我们添加了一个 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 ?geonameuri
    

    SPARQL results

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

    【讨论】:

    • oke @JoshuaTaylor 再次感谢您提供了相当详尽的答案。在我完全掌握它的深度之前,我必须研究它。会带我几天,因为我会去乡村游。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多