Retrieve country code from RDF source recieved via Sparql query - sparql

I have the following query on this endpoint https://data.bnf.fr/sparql/ :
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdagroup2elements: <http://rdvocab.info/ElementsGr2/>
PREFIX bio: <http://vocab.org/bio/0.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT distinct ?name ?nationality
WHERE {
?oeuvre dcterms:creator ?author.
?author foaf:name ?name.
?author rdagroup2elements:countryAssociatedWithThePerson ?nationality.
}
ORDER BY DESC (?mort) LIMIT 100
Which returns a list of authors with a nationality field that is itself another RDF source :
So, for the first author Jean Martin, I get this link to another RDF source, in the case for the country France : http://id.loc.gov/vocabulary/countries/fr
How could I modify the query to receive the country code (or country name, if not possible) instead of this link, in this case FR (or France)?

An alternative to extracting the country code from the URI, using "the Linked Data way":
The default graph of the endpoint https://data.bnf.fr/sparql/ doesn’t provide any data about the entities under the namespace http://id.loc.gov/vocabulary/countries/, but it provides entities under the namespace http://data.bnf.fr/vocabulary/countrycodes/, which have an owl:sameAs link to them.
For example:
<http://data.bnf.fr/vocabulary/countrycodes/fr> <http://www.w3.org/2002/07/owl#sameAs> <http://id.loc.gov/vocabulary/countries/fr> .
And these http://data.bnf.fr/vocabulary/countrycodes/ entities refer to the country code with skos:notation, and to the country name with skos:prefLabel (language-tagged).
For these cases, getting the country code would be possible with this property path:
?author rdagroup2elements:countryAssociatedWithThePerson/^owl:sameAs/skos:notation ?countryCode .
Unfortunately, only some rdagroup2elements:countryAssociatedWithThePerson values are under the http://id.loc.gov/vocabulary/countries/ namespace, while other values are under the http://data.bnf.fr/vocabulary/countrycodes/ namespace directly.
To find both cases, you could use UNION:
{ ?author rdagroup2elements:countryAssociatedWithThePerson/^owl:sameAs/skos:notation ?countryCode . }
UNION
{ ?author rdagroup2elements:countryAssociatedWithThePerson/skos:notation ?countryCode . }
The full query:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdagroup2elements: <http://rdvocab.info/ElementsGr2/>
PREFIX bio: <http://vocab.org/bio/0.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?name ?countryCode
WHERE {
?oeuvre dcterms:creator ?author.
?author foaf:name ?name.
{ ?author rdagroup2elements:countryAssociatedWithThePerson/^owl:sameAs/skos:notation ?countryCode . }
UNION
{ ?author rdagroup2elements:countryAssociatedWithThePerson/skos:notation ?countryCode . }
}
LIMIT 100
(In case you didn’t intend it: Your query treats different persons with the same name and country as one entry. To prevent this, you could output the person’s URI.)

You can use SPARQL replace function:
SELECT ... (REPLACE(STR(?nationality), "http://id.loc.gov/vocabulary/countries/", "") AS ?nationalityShort)
WHERE ...
to extract the code from the url.
But you can as well link to this resource and retrieve additional fields from it like sos:notation in your case:
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdagroup2elements: <http://rdvocab.info/ElementsGr2/>
PREFIX bio: <http://vocab.org/bio/0.1/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT distinct ?name ?nationalityCode
WHERE {
?oeuvre dcterms:creator ?author.
?author foaf:name ?name.
?author rdagroup2elements:countryAssociatedWithThePerson ?nationality.
?nationality skos:notation ?nationalityCode
}
ORDER BY DESC (?mort) LIMIT 100

Related

SPARQL Syntax correction

It's my first time using SPARQL.
Using DBpedia, I want to list the band names of all Rock bands from 1992
The code I've already developed runs correctly but gives 0 results. Am I filtering the date incorrectly?
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?bandname where {
?band dbo:genre dbp:Rock_music .
?band foaf:name ?bandname .
FILTER (dbo:activeYearsStartYear >= xsd:date("1992-01-01") && dbo:activeYearsStartYear <= xsd:date("1992-12-31"))
}
I expect only the band names in a column.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT DISTINCT ?bandname ?date where {
?band dbo:genre dbp:Rock_music .
?band foaf:name ?bandname .
?band dbo:activeYearsStartYear ?date .
FILTER (?date >= "1992-01-01"^^xsd:dateTime && ?date <= "1992-12-31"^^xsd:dateTime)
}
This does the trick but note that dbo:activeYearsStartYear produces the year only, so FILTER is not using day/month.

Retrieve all individuals based on Data Property Assertions

I am trying to retrieve information based on Data Property Assertions in Protege (SPARQL Query), however, my code is not working. I am trying to get all individuals that were born after 1960.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
SELECT ?person ?birthYear
WHERE {
?a rdfs:label ?person .
?person xsd:hasBirthYear ?birthYear .
FILTER(?birthYear>=1960)
}

How to find the nobel prize winners

I'm trying to find the nobel prize winners (http://data.nobelprize.org/snorql) born in the 20 th century and their field of study from dbpedia.
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX nobel: <http://data.nobelprize.org/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
SELECT (?x as ?Label) (count(?NAME) as ?Count)
WHERE {
?Name a nobel:Laureate;
dbpprop:dateOfBirth ?Date;
rdfs:label ?NAME.
FILTER ( ?Date >= "1900-01-01"^^xsd:date )
FILTER (?Date < "2000-01-01"^^xsd:date) .
?Name owl:sameAs ?n
Service <http://dbpedia.org/sparql>
{?n dbo:field ?x} }
GROUP BY ?x
ORDER BY desc(count(?Name))
The results shows me error 400 because of dbo:field. Where is the fault?

Different order of results in DBpedia through SNORQL vs. SERVICE query

I am trying to retrieve some data about movies from DBpedia. This is my query:
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 onto: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT *
{{SELECT *
WHERE {
SERVICE<http://dbpedia.org/sparql>{
?movie dcterms:subject <http://dbpedia.org/resource/Category:American_films> ;
a onto:Film ;
rdfs:label ?title ;
dbpedia2:gross ?revenue .
?movie onto:starring ?actorUri .
?actorUri rdfs:label ?actor .
OPTIONAL {
?movie onto:imdbId ?imdbId .
}
BIND(xsd:integer(?revenue) as ?intRevenue) .
FILTER ((datatype(?revenue) = 'http://dbpedia.org/datatype/usDollar') && (LANGMATCHES(LANG(?title), 'en')) && (LANGMATCHES(LANG(?actor), 'en'))) .
}
}
}}
ORDER BY DESC (?intRevenue)
LIMIT 40000
OFFSET 0
Running this query on http://dbpedia.org/snorql/ (without the SERVICE keyword) returns the correct result. However, doing so from a third party triplestore doesn't yield the same order (ex: Hobbit and Lord of the Rings are missing).
What do I need to change in the query to get identical results?
The best way to overcome this specific limitation is to have your own DBpedia mirror, on which you can set your own limits (including none), and which you can then use as either your primary or remote data store and/or query engine.
(ObDisclaimer: OpenLink Software provides the public DBpedia SPARQL endpoint, produces Virtuoso and the DBpedia Mirror AMI, and employs me.)

DBPedia SPARQL query should return results but is empty

I want to get all pages which have a specified category and a specified key. My query:
PREFIX dbpedia: <http://dbpedia.org/>
PREFIX dbpedia2: <http://dbpedia.org/property/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dcterms: <http://dublincore.org/2010/10/11/dcterms.rdf#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX grs: <http://www.georss.org/georss/point>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?page ?cat ?key ?value (lang(?value) as ?language) WHERE {
?page dcterms:subject ?cat .
?page ?key ?value .
FILTER(
regex(?cat, "category:Amusement_parks_in_the_Netherlands") &&
?key = foaf:depiction
)
}
But it return zero results. See here: SNORQL query
It should at least return this page: http://dbpedia.org/page/Duinrell (because it matches the criteria).
Any ideas?
There's a couple of things wrong with your query. First of all, don't use a regex when comparing URIs. Instead of:
regex(?cat, "category:Amusement_parks_in_the_Netherlands")
use:
?cat = <http://dbpedia.org/resource/category:Amusement_parks_in_the_Netherlands>
Second: your namespace prefix for dublin core seems wrong. You are querying the property dcterms:subject, and in your query, the prefix dcterms is mapped to the namespace http://dublincore.org/2010/10/11/dcterms.rdf#. However, the actual property in DBPedia is http://purl.org/dc/terms/subject, so your namespace prefix should map to http://purl.org/dc/terms/ instead.