简体   繁体   中英

SPARQL, RDF & processing Data

I'm new to this entire semantics stuff, and i've been plunged into a project which requires me to use the SPARQL endpoint from dbpedia to retrieve information about certain things such as a City for example. The idea is that my application collects a few of these keywords ( which are predefined, such as City, Country & Region ) and rely on dbpedia to get some sensible information about this.

After fiddling around with the SPARQL endpoint for some time, I came up with the following query:

PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX prop: <http://dbpedia.org/property/>

SELECT ?city
WHERE {
?city prop:name "X"@en; 
a dbo:PopulatedPlace
}

Where X stands for the name of the city that I'm trying to get information about. When I run this query against the SPARQL endpoint, I receive a URI in a RDF structure that points to a RDF store on DBPedia ( at least that's how I interpret it, please correct me if I'm wrong )

The returned result for the City "Antwerpen" looks like this:

<rdf:RDF><rdf:Description rdf:nodeID="rset"><rdf:type rdf:resource="http://www.w3.org/2005/sparql-results#ResultSet"/><res:resultVariable>city</res:resultVariable><res:solution rdf:nodeID="r0"><res:binding rdf:nodeID="r0c0"><res:variable>city</res:variable><res:value rdf:resource="http://dbpedia.org/resource/Antwerp"/></res:binding></res:solution></rdf:Description></rdf:RDF>

I'm using Ruby on Rails with the RDF for Ruby gem to work with RDF data. But honestly, I'm clueless on how to actually work with this information. When I follow the link, I seem to be receiving an RDF store that contains all information about the city I queried.

Am I right in that I probably need to point my Code to the URI received and parse our the information I desire? Or should it be possible for example to select some information directly through the SPARQL endpoint? Like for example the description & demographic data?

I know this is a pretty vague question, but I'm trying my best to get the hang of this technology, and looking for some examples to help me better understand it.

It's probably easier to get started by getting all the info you want directly from the SPARQL endpoint. You can use a query like this:

SELECT ?city ?property ?value
WHERE {
    ?city prop:name "X"@en; 
          a dbo:PopulatedPlace;
          ?property ?value .
}

This will return a bunch of properties and values that describe the city in some detail and it will give you a flavour for the kind of information that you can get.

A next step would be to query specifically for the properties you're interested in:

SELECT *
WHERE {
    ?city prop:name "Galway"@en; 
          a dbo:PopulatedPlace .
    OPTIONAL { ?city dbo:populationUrban ?pop }
    OPTIONAL { ?city foaf:homepage ?homepage }
    OPTIONAL { ?city geo:lat ?lat }
    OPTIONAL { ?city geo:long ?long }
}

I'm putting each of the triple patterns into an OPTIONAL block because some cities may not have all of the information available. Without the OPTIONAL block, you'd only get a result if all the triple patterns match for the city.

yes, you can use the URI received and parse out the information, or, you can use "bind" in your SPARQL to get the information only. For example, in the following SPARQL code:

PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX prop: <http://dbpedia.org/property/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos/>

SELECT ?city, ?latitude, ?longitude
WHERE {
?city prop:name "Edinburgh"@en; 
    a dbo:PopulatedPlace;
    geo:lat ?lat;
    geo:long ?long.
    bind(strafter(str(?lat), "http://www.w3.org/2003/01/geo/wgs84_pos/") as ?latitude)
    bind(strafter(str(?lat), "http://www.w3.org/2003/01/geo/wgs84_pos/") as ?longitude)
}

instead of get " http://www.w3.org/2003/01/geo/wgs84_pos/55.93 " for latitude, you will get "55.93" only.

Of course, if you do not have to use SPARQL endpoint, Jena (for java) and rdflib (for python) are very good choices to handle SPARQL queries and edit ontology.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM