2015-04-06 144 views
0

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

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

您的反馈和你的想法是非常感谢!

直到

回答

1

有可能包含了联邦国家或地区值几个不同的属性。有时是dbpprop:landkreis(用于区 德语单词,在其他情况下,它是DBpedia的猫头鹰:该区 有可能在情况下,只有其中一人是 集

SPARQL这两个结合起来?财产路径是为这个伟大的你就可以说

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

如果有更多的属性,你可能会喜欢一个版本

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

此外,我想读出城市的名字在华北,西北 ...。有时候,这些城市在dbpprop引用:北 等为每个方向的基本查询是相同的:再次

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,你可以实际使用属性路径来缩短很多检索标签的结果。例如: -

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

+0

这工作都非常好!非常感谢。我正在使用'(group_concat(?directionPlaceName; separator =“,”)as?directionPlaceNames)'来获取北部,南部等地方的列表(参见示例)(http://bit.ly/1NYfSuO) )。 但是,这不会获得地方的方向。 (这对我来说没有任何问题,但是我对这样一个解决方案感兴趣,它会为每个方向创建几个列,每个列都包含一个特定方向的地方列表。并且还没有想出如何将'group_by'和'group_concat'组合起来 – Till 2015-04-06 16:19:53

+0

@Till你可以在group_concat中使用任意表达式,为什么不能这样:'group_concat(concat(str(?directionProp),“:”,directionPlaceName ); separator =“;”)'得到类似**的东西:Place1; north:Place2; west:Place3 **? – 2015-04-06 16:31:19