2011-06-17 22 views
2

我正在使用ARC2庫的PHP來發布sparql查詢,並且我在這個問題上陷入了困境(我不認爲它有東西與lib一起做)。Sparql查詢出錯了導致不正確的地理位置+不需要的屬性結果我需要

此查詢工作就好了 - 在我的應用程序,並在DBpedia中snorql:

PREFIX dbo:<http://dbpedia.org/ontology/> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX dc: <http://purl.org/dc/elements/1.1/> 
PREFIX : <http://dbpedia.org/resource/> 
PREFIX dbpedia2: <http://dbpedia.org/property/> 
PREFIX dbpedia: <http://dbpedia.org/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX geo: <http://www.geonames.org/ontology#> 

SELECT * WHERE { 
?c rdf:type dbo:Country; 
foaf:name "someCountryName"@en. 
} 

在另一方面,該查詢不起作用:

SELECT * WHERE { 
?c rdf:type dbo:Country; 
foaf:name "someCountryName"@en; 
geo:lat ?lat. 
} 

注:查詢使用與上面列出的相同的前綴完成。 我只需要採取拉長&長的一個國家。我也可以嘗試Freebase,但我真的需要在這裏工作。第二個查詢在snorql中工作,看不出爲什麼它不能在我的應用程序中工作? 任何幫助非常感謝!

+1

現在8個小時已經結束了,你可以拆分問題和答案嗎?並再次讓標題問題成爲一個問題? (尋求沒有未解答的SPARQL問題) –

+0

羅馬尼亞的'dissolutionDate'技巧也出現在[這個問題]中(http://stackoverflow.com/q/5921884/1281433)。 –

回答

0

注:本文是由@IrinaM提供的,但投入的問題爲a revision因爲八個小時的限制。現在已經晚了很多,並且@IrinaM作爲答案發布的建議尚未採取行動。這是一個包含該文本的輕微編輯的CW回答。

問題中的代碼有幾處錯誤。

  1. 的2 第二查詢(在一個沒有「工作」)將返回幾個結果。如果我使用foaf:name "Romania"搜索國家,它將返回"Communist Romania","Wallachian Romania","Romania"等。在這些結果中,只有一個具有geo:latgeo:long。 基本上,當所有結果都不滿足要求的屬性時,則不會返回任何結果。

  2. 錯誤的本體用於geo;它應該是<http://www.w3.org/2003/01/geo/wgs84_pos#>

  3. 其他奇怪的結果需要過濾掉,只保留所需的唯一結果。在這種情況下,dbpedia-owl:dissolutionDate無效後進行篩選。 使用FILTER (?name="someCountryName"^^xsd:string)的foaf:name的精確匹配不起作用。

這裏的代碼,成功地檢索某一國家的緯度和經度值(注意,可能需要一些其他的過濾器正確的國家搜索時):

//setup prefixes 
$prefixes = 'PREFIX dbo:<http://dbpedia.org/ontology/> 
PREFIX owl: <http://www.w3.org/2002/07/owl#> 
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX dc: <http://purl.org/dc/elements/1.1/> 
PREFIX : <http://dbpedia.org/resource/> 
PREFIX dbpedia2: <http://dbpedia.org/property/> 
PREFIX dbpedia: <http://dbpedia.org/> 
PREFIX skos: <http://www.w3.org/2004/02/skos/core#> 
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> 
';  
//query no. 1 - find out "right" country - hoping for a unique result 
$q1 = $prefixes.'SELECT ?c WHERE { 
?c rdf:type dbo:Country; 
foaf:name "'.$userPreferences->country.'"@en. 
OPTIONAL {?c dbo:dissolutionDate ?x} 
FILTER(!bound(?x))}'; 
//note: $userPreferences->country represents a country entered by the user in an input 

//query no. 2 - find out long & lat 
$q2 = $prefixes.'SELECT * WHERE { 
<'.$countryReturned.'> geo:lat ?lat; 
geo:long ?long. 
}'; 
//note: $countryReturned represents the URI of the "unique" country my 1st query 
//retrieved, we use it directly. 

對於那些激情關於ARC2 PHP lib,這是如何進行查詢的(注意:不要忘了包括ARC2.php文件):

$dbpediaEndpoint = array('remote_store_endpoint' =>'http://dbpedia.org/sparql'); 
$dbpediaStore = ARC2::getRemoteStore($dbpediaEndpoint); 
$rows1 = $dbpediaStore->query($q1,'rows'); 
var_dump($rows1); //to see quick results