Case insensitive URI matching in SPARQL - 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')
}

Related

How to filter results by city?

I am using DBpedia to retrieve data on specific towns' POIs, for instance Barcelona. I've tried to find appropriate answers to my problems on Stack Overflow but almost all the answers provided did not work out for me, and given that I am new in SPARQL, I couldn't manage to figure out whether or not it was the syntax that was at fault or if there was something else. I am using the DBpedia SPARQL Endpoint available at the following address : http://dbpedia.org/sparql
I've tried to use some things such as ?citylabel or ?location, and to either use the FILTER command or directly fill a value for these classes. I've also tried a few other things, without satisfying results (and most of the times syntax errors that I could not resolve). These solutions have been, in most cases, applied and seemed to work just fine for people, so I do not understand what's going on.
BASE <http://www.dbpedia.org/resource/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia3: <http://dbpedia.org/ontology/>
SELECT (SAMPLE(?label) as ?activity_name)
(SAMPLE(?latitude) as ?activity_lat)
(SAMPLE(?longitude) as ?activity_lon)
(SAMPLE(?homepage) as ?URL)
(SAMPLE(?type) as ?activity_type)
(SAMPLE(?abstract) as ?descriptor)
WHERE
{ ?Museum a dbo:Museum ;
rdfs:label ?label ;
dbo:abstract ?abstract ;
dbo:type ?type ;
geo:lat ?latitude ;
geo:long ?longitude ;
foaf:homepage ?homepage .
}
GROUP BY ?Museum ?label
The results of this query are, I think, pretty much any museum that is known by DBpedia and categorized as such. What I'd like to have is a list of museums within Barcelona. Can somebody give me an rather in-depth answer so I can understand how it is working ? Thanks, in advance.

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

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')
}

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 : how to get values for a particular resource?

I am trying this query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?label
WHERE
{
?AGE rdfs:label ?label.
}
I need all the values of AGE from my model but instead this query is giving me other resources values which have the same property label .
For example I have connected the resource gender to have a property rdfs:label. So in my result I get both age values and gender values.
Can anybody tell me where am I wrong ?
It seems you may be assigning some semantics to the variable '?AGE'. SPARQL is a graph pattern matching language and anything with a '?' as the first character is a variable - or better yet, an unknown in the graph pattern match. I.e., the following is an equivalent query to yours:
SPARQL ?label
WHERE
{ ?s rdfs:label ?label .
}
This will find all triples that have a rdfs:label property and select the value of ?label.
If you have a specific resource you want to query, then specify that resource in the subject, for example:
PREFIX ex: <http://example.org/ex>
SPARQL ?label
WHERE
{ ex:AGE rdfs:label ?label .
}
So understanding the difference between an unknown (denoted by '?' (or '$')) and a known (a qname or a full URI) is important to understand how SPARQL performs graph pattern matching.
Lots of SPARQL learning material on the Web, so a suggestion is to look into some of these to learn some basics.

SPARQL query returns no data

Why does this SPARQL query return no data?
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT *
WHERE {
<http://dbpedia.org/resource/Louis,_Prince_of_Condé_(1530–1569)> dbpedia-owl:abstract ?abstract
}
LIMIT 1
If you look at the DBpedia page, it shows the person has an abstract. Is it to do with the brackets in the URL? If so, how can I get round this?
This URI does not lead to the same result as the DBpedia page - for what ever reason. You can see this with
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
SELECT *
WHERE {
<http://dbpedia.org/resource/Louis,_Prince_of_Condé_(1530–1569)> ?p ?o
}
LIMIT 100
But it has an owl:sameAs relation to
http://dbpedia.org/resource/Louis,_Prince_of_Cond%C3%A9_(1530%E2%80%931569)
That means if you use this URI in your query, it should work as expected. But you should indeed apply a FILTER on the language, e.g. 'en' for English abstracts.
As AKSW mentions, the resource actually doesn't have many properties, but is connected to the "canonical" version by an owl:sameAs link. You can keep using the IRI that you're using now, follow owl:sameAs in either direction to any of its equal resources (let's call them ?s), and then ask for the abstract of ?s. (And then it's not a bad idea to filter by language, if that's applicable.) You can do this with a query like this (note that the current DBpedia endpoint uses dbo:, now, not the older dbpedia-owl:):
select ?abstract where {
<http://dbpedia.org/resource/Louis,_Prince_of_Condé_(1530–1569)> (owl:sameAs|^owl:sameAs)* ?s .
?s dbo:abstract ?abstract .
filter langMatches(lang(?abstract),'en')
}
It does not have dbpedia-owl:abstract predicate. If you list its predicates you find the following properties:
http://www.w3.org/2002/07/owl#sameAs
http://xmlns.com/foaf/0.1/name
http://purl.org/dc/elements/1.1/description
http://dbpedia.org/ontology/alias
http://dbpedia.org/ontology/birthYear
http://dbpedia.org/ontology/deathYear
http://dbpedia.org/ontology/viafId
http://dbpedia.org/ontology/deathPlace
http://dbpedia.org/ontology/deathDate
http://dbpedia.org/ontology/birthPlace
http://dbpedia.org/ontology/birthDate