SPARQL query of Person using EasyRdf on DBpedia

白昼怎懂夜的黑 提交于 2019-12-12 06:14:28

问题


I want to get all information about a Person from the DBpedia SPARQL API. First problem is, the request took too long time. Second, I don't get back any information about the person I looked for. I don't know what I am doing wrong?

My Code:

require_once "EasyRdf.php";
require_once "html_tag_helpers.php";
// Setup some additional prefixes for DBpedia
EasyRdf_Namespace::set('category', 'http://dbpedia.org/resource/Category:');
EasyRdf_Namespace::set('dbpedia', 'http://dbpedia.org/resource/');
EasyRdf_Namespace::set('dbo', 'http://dbpedia.org/ontology/');
EasyRdf_Namespace::set('dbp', 'http://dbpedia.org/property/');
$sparql = new EasyRdf_Sparql_Client('http://dbpedia.org/sparql');
$result = $sparql->query(
    'SELECT *
        WHERE {
          ?person foaf:name ?name.
          ?person foaf:name ?name.
          FILTER(regex(?person,"John Lennon"))
        }
    '
);
echo "<pre>";
print_r($result);

My Result:

EasyRdf_Sparql_Result Object
(
    [type:EasyRdf_Sparql_Result:private] => bindings
    [boolean:EasyRdf_Sparql_Result:private] => 
    [ordered:EasyRdf_Sparql_Result:private] => 
    [distinct:EasyRdf_Sparql_Result:private] => 
    [fields:EasyRdf_Sparql_Result:private] => Array
        (
            [0] => person
            [1] => name
        )

    [storage:ArrayIterator:private] => Array
        (
        )

)

回答1:


You're not going to get results with something like:

regex(?person,"John Lennon"

since the value of ?person is a URI, not a string, and URIs don't contain spaces. Instead, you could query for individuals with the actual name (don't forget the language tag), as in:

select * where {
  ?resource foaf:name "John Lennon"@en
}

SPARQL results

I want to get all information

If you want to get all the property values about a person, you could extend the query with something like:

select ?subject ?property ?object { 
  ?resource foaf:name "John Lennon"@en ;
            ?property ?object .
}

Of course, that will only retrieve data where the person is the subject of the triples, but you might also care about things where the person is the object of a triple too, in which case you might go for:

describe ?person where {
  ?person foaf:name "John Lennon"@en
}

SPARQL results (N-Triples)

The results of describe queries are implementation dependent, but it's pretty common to get all the triples of which the resource is the subject or object.



来源:https://stackoverflow.com/questions/29988928/sparql-query-of-person-using-easyrdf-on-dbpedia

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!