简化SPARQL查询

时间:2015-04-06 00:31:17

标签: sparql dbpedia

我正在尝试使用SPARQL查询对DBPedia进行相当复杂的调用。我想获得一些关于城市的信息(地区,联邦州/»Bundesland«,邮政编码,坐标和地理相关的城市)。

Try online!

SELECT * WHERE {
  #input
  ?x rdfs:label "Bentzin"@de.

  #district
  OPTIONAL {
    ?x dbpedia-owl:district ?district
    # ?x dbpprop:landkreis ?district
    { SELECT * WHERE {
       ?district rdfs:label ?districtName
       FILTER(lang(?districtName) = "de")

       ?district dbpprop:capital ?districtCapital
       { SELECT * WHERE {
         ?districtCapital rdfs:label ?districtCapitalName
         FILTER(lang(?districtCapitalName) = "de")
       }}
    }}
  }

  #federal state
  OPTIONAL {
    # ?x dbpprop:bundesland ?land
    ?x dbpedia-owl:federalState ?land
    { SELECT * WHERE {
        ?land rdfs:label ?landName
        FILTER(lang(?landName) = "de")
    }}
  }

  #postal codes
  ?x dbpedia-owl:postalCode ?zip.

  #coordinates
  ?x geo:lat ?lat.
  ?x geo:long ?long

  #cities in the south
  OPTIONAL {
    ?x dbpprop:south ?south
    {SELECT * WHERE {
      ?south rdfs:label ?southName
      FILTER(lang(?southName) = "de")
    }}
  }

  #cities in the north
  OPTIONAL {
    ?x dbpprop:north ?north
    { SELECT * WHERE {
       ?north rdfs:label ?northName
       FILTER(lang(?northName) = "de")
    }}
  }

  #cities in the west
  ...

}

这在某些情况下有效,但是有一些主要问题。

  1. 有几种不同的属性可能包含联邦州或地区的价值。有时它是dbpprop:landkreis(区域的德语单词,在其他情况下是dbpedia-owl:district。在只设置其中一个的情况下,是否可以将这两者结合起来?

  2. 此外,我想读出北方,西北,......的城市名称。有时,这些城市在dbpprop:north等中被引用。每个方向的基本查询都是相同的:

    OPTIONAL {
      ?x dbpprop:north ?north
      { SELECT * WHERE {
        ?north rdfs:label ?northName
        FILTER(lang(?northName) = "de")
      }}
    }
    

    我真的不想为每个方向重复八次,有没有办法简化这个?

  3. 有时,引用了多个其他城市(example)。在这些情况下,返回了多个数据集。是否有可能在单个数据集中获取这些城市的名称列表?

    +---+---+---------------------------------------------------------------+
    | x | … |                            southName                          |
    +---+---+---------------------------------------------------------------+
    | … | … | "Darmstadt"@de, "Stuttgart"@de, "Karlsruhe"@de, "Mannheim"@de |
    +---+---+---------------------------------------------------------------+
    
  4. 非常感谢您的反馈和您的想法!

    直到

1 个答案:

答案 0 :(得分:1)

  

有几种不同的属性可能包含联邦州或地区的价值。有时它是dbpprop:landkreis(   区的德语单词,在其他情况下它是dbpedia-owl:区。是   在只有其中一个的情况下,可以将这两者结合起来   设置?

SPARQL属性路径非常适用于此。你可以说

?subject dbprop:landkreis|dbpedia-owl:district ?district

如果有更多属性,您可能更喜欢的版本:

values ?districtProperty { dbprop:landkreis dbpedia-owl:district }
?subject ?districtProperty ?district
  

此外,我想读出北方城市的名字,   西北, …。有时,这些城市在dbpprop:north中引用   每个方向的基本查询都是相同的:

OPTIONAL {
  ?x dbpprop:north ?north
  { SELECT * WHERE {
    ?north rdfs:label ?northName
    FILTER(lang(?northName) = "de")
  }}
}

再次,它是来救援。另外,请勿使用 lang(...)= ... 来过滤语言,请使用 langMatches

optional {
  values ?directionProp { dbpprop:north
                          #-- ...
                          dbpprop:south }
  ?subject ?directionProp ?direction 
  optional { 
    ?direction rdfs:label ?directionLabel
    filter langMatches(lang(?directionLabel),"de")
  }
}
  

有时,引用了多个其他城市(示例)。在   这些情况下,返回了多个数据集。有没有   可以在一个单一的城市中获得这些城市名称的列表   而不是数据集?

+---+---+---------------------------------------------------------------+
| x | … |                            southName                          |
+---+---+---------------------------------------------------------------+
| … | … | "Darmstadt"@de, "Stuttgart"@de, "Karlsruhe"@de, "Mannheim"@de |
+---+---+---------------------------------------------------------------+

这是分组 group_concat 的用途。见Aggregating results from SPARQL query。我实际上并没有在你提供的查询中看到这些结果,所以我没有很好的数据来测试结果。

你似乎也做了很多不必要的子选择。你可以在图形模式中添加额外的三元组;您不需要嵌套查询来获取其他信息。

考虑到这些因素,您的查询将变为:

select * where {
  ?x rdfs:label "Bentzin"@de ;
     dbpedia-owl:postalCode ?zip ;
     geo:lat ?lat ;
     geo:long ?long

  #-- district
  optional {
    ?x dbpedia-owl:district|dbpprop:landkreis ?district .
    ?district rdfs:label ?districtName
    filter langMatches(lang(?districtName),"de")
    optional {
      ?district dbpprop:capital ?districtCapital .
      ?districtCapital rdfs:label ?districtCapitalName
      filter langMatches(lang(?districtCapitalName),"de")
    }
  }

  #federal state
  optional  {
    ?x dbpprop:bundesland|dbpedia-owl:federalState ?land .
    ?land rdfs:label ?landName
    filter langMatches(lang(?landName),"de")
  }

  values ?directionProp { dbpprop:south dbpprop:north }
  optional {
    ?x ?directionProp ?directionPlace .
    ?directionPlace rdfs:label ?directionName 
    filter langMatches(lang(?directionName),"de")
  }
}

SPARQL results

现在,如果你只是在寻找这些东西的名称,而没有关联的URI,你实际上可以使用属性路径来缩短检索标签的很多结果。 E.g:

select * where {
  ?x rdfs:label "Bentzin"@de ;
     dbpedia-owl:postalCode ?zip ;
     geo:lat ?lat ;
     geo:long ?long

  #-- district
  optional {
    ?x (dbpedia-owl:district|dbpprop:landkreis)/rdfs:label ?districtName
    filter langMatches(lang(?districtName),"de")
    optional {
      ?district dbpprop:capital/rdfs:label ?districtCapitalName
      filter langMatches(lang(?districtCapitalName),"de")
    }
  }

  #-- federal state
  optional  {
    ?x (dbpprop:bundesland|dbpedia-owl:federalState)/rdfs:label ?landName
    filter langMatches(lang(?landName),"de")
  }

  optional {
    values ?directionProp { dbpprop:south dbpprop:north }
    ?x ?directionProp ?directionPlace .
    ?directionPlace rdfs:label ?directionName
    filter langMatches(lang(?directionName),"de")
  }
}

SPARQL results

相关问题