2015-04-06 146 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