SPARQL DBpedia - Retrieve Properties with numbers (DatatypeProperties, xsd) - properties

So I encountered a Problem on DBpedia. Apparently I can retrieve any kind of property from a resource with the query below, but when the property is from the type DatatypeProperty or just a number (xsd:integer or something similar) the DBpedia SPARQL Endpoint returns an empty result.
I would like to know what I have to change or even better optional include in my current query to solve this problem.
Important note: Germany and population total are required inputs in my program. Please remember that I must use them.
prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT ?objectLabel
WHERE {
?subject ?predicate ?object ; rdfs:label "Germany"#en .
?predicate rdfs:label "population total"#en .
?object rdfs:label ?objectLabel
FILTER (LANG(?objectLabel)='en')
}
Thank you for your help.

A DatatypeProperty is used to related an individual to a literal and literals can't have any outgoing edge, thus, no rdfs:label. If you want to have the lexical form of the literal you can use the str function:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT DISTINCT (str(?object) as ?value)
WHERE {
?subject ?predicate ?object ; rdfs:label "Germany"#en .
?predicate rdfs:label "population total"#en
FILTER (LANG(?objectLabel)='en')
}

Related

Case insensitive URI matching in SPARQL

I was hoping someone could help me with a SPARQL query I'm writing. I may get some of the terminology wrong, I'm not a SPARQL expert.
I am trying to get some information from the Nobel prizes SPARQL endpoint (data.nobelprize.org/sparql), retrieving the labels of predicates where the labels of objects match a certain string.
So, for example, if I search for an object with an objectLabel that contains the string 'Robert Burns Woodward', I should receive a number of results including:
predicateLabel, objectLabel
"Laureate","Robert Burns Woodward"
"LaureateAward","Chemistry 1965, Robert Burns Woodward"
"AwardFile","Nobel Lecture Robert Burns Woodward"
I have written the SPARQL below which should work, however it does not retrieve any results:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?predicateLabel ?objectLabel
WHERE
{
?subject ?predicate ?object .
?object rdfs:label ?objectLabel .
?predicate rdfs:label ?predicateLabel .
FILTER contains(?objectLabel, 'Robert Burns Woodward')
}
The reason is that the ?predicate URI is in a different case to the URI which is linked to the corresponding rdfs:label property.
So for example, the predicate
http://data.nobelprize.org/terms/nobelPrize
is used to connect laureates to the prizes they have won. Eg:
sub: http://data.nobelprize.org/resource/laureate/231
pred: http://data.nobelprize.org/terms/nobelPrize
obj: http://data.nobelprize.org/resource/nobelprize/Chemistry/1965
However, the rdfs:label is linked to
http://data.nobelprize.org/terms/NobelPrize
not
http://data.nobelprize.org/terms/nobelPrize
Note the difference in case between the two - the second URI has a lower case 'n' in nobelprize, wheras the first uses an upper case N.
So my question is, is there a way in SPARQL to make a URI case insensitive so that http://data.nobelprize.org/terms/NobelPrize will match with http://data.nobelprize.org/terms/nobelPrize? I know it is possible to search for strings that are case insensitive using FILTER regex or FILTER contains, but I don't know if it is possible with URIs.
You're approaching this the wrong way, I think. That data source has two separate concepts. The resource spelled NobelPrize denotes the class of Nobel Prizes, as can be seen in the ontology. The resource spelled nobelPrize is a different resource, namely the relation between a laureate and a particular Nobel Prize.
In other words: they are distinct, deliberately so, and you shouldn't try to turn them into the same thing by doing case-insensitive matches.
It's somewhat odd that that the nobelPrize property has no label in the SPARQL endpoint, because according to the ontology file it should have one. But given that it doesn't have a label, you're sort of stuck with just getting back the predicate URI itself. You can optionally shorten it by snipping of the namespace part using strafter, like so:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT (strafter(str(?predicate), "http://data.nobelprize.org/terms/") as ?predicateLabel) ?objectLabel
WHERE
{
?subject ?predicate ?object .
?object rdfs:label ?objectLabel .
FILTER contains(?objectLabel, 'Robert Burns Woodward')
}
An alternative, which is somewhat more complex but conceptually neater, is that instead of returning the name of the predicate, you return the name of the type of the object to which the predicate points:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?objectClassName ?objectLabel
WHERE
{
?subject ?predicate ?object .
?object a ?objectClass .
?objectClass rdfs:label ?objectClassName .
?object rdfs:label ?objectLabel .
FILTER contains(?objectLabel, 'Robert Burns Woodward')
}

SPARQL Federated Query Not Returning All Solutions

This is an evolution of this question.
Basically I am having trouble getting all the solutions to a SPARQL query from a remote endpoint. I have read through section 2.4 here because it seems to describe a situation almost identical to mine.
The idea is that I want to filter my results from DBPedia based on information in my local RDF graph. The query is here:
PREFIX ns1:
<http://www.semanticweb.org/caeleanb/ontologies/twittermap#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT *
WHERE {
?p ns1:displayName ?name .
SERVICE <http://dbpedia.org/sparql> {
?s rdfs:label ?name .
?s rdf:type foaf:Person .
}
}
And the only result I get is dbpedia:John_McCain (for ?s). I think this is because John McCain is the only match in the first 'x' results, but I can't figure out how to get the query to return all matches. For example, if I add a filter like:
SERVICE <http://dbpedia.org/sparql> {
?s rdfs:label ?name .
?s rdf:type foaf:Person .
FILTER(?name = "John McCain"#en || ?name = "Jamie Oliver"#en)
}
Then it correctly returns BOTH dbpedia:Jamie_Oliver and dbpedia:John_McCain. There are dozens of other matches like Jamie Oliver that do not come through unless I specifically add it to a Filter like this.
Can someone explain a way to extract the rest of the matches? Thanks.
It looks like the cause of this issue is that the SERVICE block is attempting to pull all foaf:Persons from DBPedia, and then filter them based on my local Stardog db. Since there is a 10,000 result limit when querying DBPedia, only matches which occur in that set of 10,000 arbitrary Persons will be found. To fix this, I wrote a script to put together a FILTER block containing every string name in my Stardog db and attached it to the SERVICE block to filter remotely and thereby avoid hitting the 10,000 result limit. It looks something like this:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX ns1: <http://www.semanticweb.org/caeleanb/ontologies/twittermap#>
CONSTRUCT{
?s rdf:type ns1:Person ;
ns1:Politician .
}
WHERE {
?s rdfs:label ?name .
?s rdf:type dbo:Politician .
FILTER(?name IN ("John McCain"#en, ...)
}

SPARQL dbpedia select data not working

I'm new to sparql and I'm learning it right now.
I took this code from a tutorial
prefix dbpo: <http://dbpedia.org/ontology/>
prefix dbpr: <http://dbpedia.org/resource/>
select distinct ?Predicate ?Object where {
?Subject ?Predicate ?Object
filter(?Subject = dbpr:Markiplier)
}
and executed it on http://dbpedia.org/sparql, it returns empty result
as you can see on this image
I tried many codes and I always get empty result, is there anything I must do to get data ? Any advice will be appreciated.
I didn't understand why you put space for publishing the question but you should reunite them again. Besides you can also ask for ?Subjectif you like:
prefix dbpo: <http://dbpedia.org/ontology/>
prefix dbpr: <http://dbpedia.org/resource/>
select distinct ?Subject ?Predicate ?Object where {
?Subject ?Predicate ?Object
filter(?Subject = dbpr:Markiplier)
}
By the way if you have troubles with prefixes, I would reccomend to work on the http://dbpedia.org/snorql/ endpoint. So that you can use the predefined prefixes directly like this.

Sparql to recover the Type of a DBpedia resource

I need a Sparql query to recover the Type of a specific DBpedia resource. Eg.:
pt.DBpedia resource: http://pt.dbpedia.org/resource/Argentina
Expected type: Country (as can be seen at http://pt.dbpedia.org/page/Argentina)
Using pt.DBpedia Sparql Virtuoso Interface (http://pt.dbpedia.org/sparql) I have the query below:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
select ?l ?t where {
?l rdfs:label "Argentina"#pt .
?l rdf:type ?t .
}
But it is not recovering anything, just print the variable names. The virtuoso answer.
Actually I do not need to recover the label (?l) too.
Anyone can fix it, or help me to define the correct query?
http in graph name
I'm not sure how you generated your query string, but when I copy and paste your query into the endpoint and run it, I get results, and the resulting URL looks like:
http://pt.dbpedia.org/sparql?default-graph-uri=http%3A%2F%2Fpt.dbpedia.org&sho...
However, the link in your question is:
http://pt.dbpedia.org/sparql?default-graph-uri=pt.dbpedia.org%2F&should-sponge...
If you look carefully, you'll see that the default-graph-uri parameters are different:
yours: pt.dbpedia.org%2F
mine: http%3A%2F%2Fpt.dbpedia.org
I'm not sure how you got a URL like the one you did, but it's not right; the default-graph-uri needs to be http://pt.dbpedia.org, not pt.dbpedia.org/.
The query is fine
When I run the query you've provided at the endpoint you've linked to, I get the results that I'd expect. It's worth noting that the label here is the literal "Argentina"#pt, and that what you've called ?l is the individual, not the label. The individual ?l has the label "Argentina"#pt.
We can simplify your query a bit, using ?i instead of ?l (to suggest individual):
select ?i ?type where {
?i rdfs:label "Argentina"#pt ;
a ?type .
}
When I run this at the Portuguese endpoint, I get these results:
If you don't want the individual in the results, you don't have to select it:
select ?type where {
?i rdfs:label "Argentina"#pt ;
a ?type .
}
or even:
select ?type where {
[ rdfs:label "Argentina"#pt ; a ?type ]
}
If you know the identifier of the resource, and don't need to retrieve it by using its label, you can even just do:
select ?type where {
dbpedia-pt:Argentina a ?type
}
type
==========================================
http://www.w3.org/2002/07/owl#Thing
http://www.opengis.net/gml/_Feature
http://dbpedia.org/ontology/Place
http://dbpedia.org/ontology/PopulatedPlace
http://dbpedia.org/ontology/Country
http://schema.org/Place
http://schema.org/Country

Director Filmography - all queries are returning empty

All, I'm trying to get a director's filmography from dbpedia:
Both the queries below (and other attempts not shown) return empty sets. Query below doesn't work:
PREFIX d: <http://dbpedia.org/ontology/>
SELECT ?filmName WHERE {
?film d:director :woody_allen .
?film rdfs:label ?filmName .
}
Or (this is from) :
PREFIX m: <http://data.linkedmdb.org/resource/movie/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?filmTitle WHERE {
?film rdfs:label ?filmTitle.
?film m:director ?dir.
?dir m:director_name "Sofia Coppola".
}
Not sure what would be the problem with such simple queries. Any ideas here?
The problem with your first query is the use of :woody_allen (besides the fact that you haven't actually defined the default prefix and so the query should technically be illegal SPARQL) the term doesn't actually appear in the data as written.
Try rewriting your query like so:
PREFIX d: <http://dbpedia.org/ontology/>
SELECT ?filmName WHERE {
?film d:director <http://dbpedia.org/resource/Woody_Allen> .
?film rdfs:label ?filmName .
}
The above does give results.
As for your second query DBPedia does not use the Linked MDB ontologies so that query can't match anything