Sparql 查询出错,导致 + 上的地理信息不正确我所需的属性的结果不存在

发布于 2024-11-16 04:05:36 字数 1109 浏览 1 评论 0原文

我正在使用 ARC2 php 库来发出 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 中有效,不明白为什么它在我的应用程序中不起作用? 非常感谢任何帮助!

I'm using ARC2 library for php to issue sparql queries, and i got stuck on this issue (I don't think it has something to do with the lib).

This query works just fine - in my app, and in 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.
}

On the other hand, this query doesn't work:

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

Note: the query is done using the same prefixes as listed above.
I just need to take lat & long of a country. I could also try Freebase, but i really need to make it work here. The 2nd query works in snorql, can't see why it doesn't also work in my app?
Any help is much appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

听风念你 2024-11-23 04:05:36

注意:此文本由 @IrinaM 提供,但作为修订版 因为八小时的限制。现在已经晚了很多,@IrinaM 发布作为答案的建议尚未得到执行。这是包含该文本的稍微编辑过的 CW 答案。

问题中的代码有几个错误。

  1. 第二个nd查询(没有“工作”的查询)将返回几个结果。如果我搜索带有 foaf:name "Romania" 的国家/地区,它将返回 "共产主义罗马尼亚""瓦拉几亚罗马尼亚""罗马尼亚” 等。在这些结果中,只有一个包含 geo:latgeo:long
    基本上,当所有结果都不满足所需属性时,将不会返回任何结果。

  2. geo 使用了错误的本体;它应该是

  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 库的人,这是进行查询的方法(注意:don不要忘记包含 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

Note: This text was provided by @IrinaM, but put into the question as a revision because of the eight hour limit. It's now much later and the suggestion that @IrinaM post as an answer hasn't been acted upon. Here's a slightly edited CW answer containing that text.

There are several mistakes in the code in the question.

  1. The 2nd query (the one that didn't “work”) would return several results. If I searched countries with foaf:name "Romania", it would return "Communist Romania", "Wallachian Romania", "Romania", etc. Out of these results, only one would have geo:lat and geo:long.
    Basically, when a required property isn't satisfied by all the results, then no results will be returned.

  2. The wrong ontology is used for geo; it should be <http://www.w3.org/2003/01/geo/wgs84_pos#>.

  3. The other odd results need to be filtered out, to only keep the desired unique results. In this case, filtering after the inexistence of dbpedia-owl:dissolutionDate works.
    Exact matches of foaf:name using FILTER (?name="someCountryName"^^xsd:string) do not work.

Here's code that retrieves successfully the latitude and longitude values for a given country (beware, some other filters may be needed when searching for the right country):

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

For those passionate about ARC2 PHP lib, this is how to make the queries (note:don't forget to include the ARC2.php file):

$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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文