Sparql howto select one result to ask another service in federated search - sparql

How I can select one of rdf possible labels to send it to the Federated Search?
I have a rdf record who has several links to another databases:
<owl:sameAs rdf:resource="http://cantic.bnc.cat/registres/CUCId/a10035400"/>
<owl:sameAs rdf:resource="http://viaf.org/viaf/61502721"/>
<owl:sameAs rdf:resource="http://dbpedia.org/resource/Delfi_Abella"/>
<owl:sameAs rdf:resource="http://id.loc.gov/authorities/names/nr2005020137"/>
<owl:sameAs rdf:resource="http://d-nb.info/gnd/158288475"/>
I just get the dbpedia link to ask in dbpedia some information using It.
I create this federated search but It not works:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl/>
PREFIX rdaGr2: <http://rdvocab.info/ElementsGr2/>
PREFIX dbo:<http://dbpedia.org/ontology/>
SELECT *
WHERE {
?autid rdaGr2:dateOfBirth "1991" .
?autid owl:sameAs ?dbpediaid
FILTER regex(?dbpediaid,'dbpedia','i') .
SERVICE <http://dbpedia.org/sparql>
{
?dbpediaid dbo:birthPlace ?naixement
}
}
What I'm doing wrong?
If I use the single search:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX owl: <http://www.w3.org/2002/07/owl/>
PREFIX rdaGr2: <http://rdvocab.info/ElementsGr2/>
PREFIX dbo:<http://dbpedia.org/ontology/>
SELECT *
WHERE {
?autid rdaGr2:dateOfBirth "1991" .
?autid owl:sameAs ?dbpediaid
FILTER regex(?dbpediaid,'dbpedia','i') .
}
I get the autid and the dbpedia link.
autid dbpediaid
http://arties.cbuc.cat/openccuc/a10985256/ http://dbpedia.org/resource/Emma_Roberts
How I can tell to the SERVIE just use It.
Regards
Update (originally posted as an answer)
Maybe I'm asking badly the question:
I try to do two different sparql in just one. Using the results from the first part of question (Filter aplied) i try to ask into DBPEDIA to get the results I need.
From the first part without any Filter
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX void: <http://rdfs.org/ns/void#>
PREFIX owl: <http://www.w3.org/2002/07/owl/>
PREFIX rdaGr2: <http://rdvocab.info/ElementsGr2/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbo:<http://dbpedia.org/ontology/>
SELECT *
WHERE {
?autid rdaGr2:dateOfBirth "1991" .
?autid owl:sameAs ?dbpediaid
}
I get this results:
http://arties.cbuc.cat/openccuc/a10603050/ http://cantic.bnc.cat/registres/CUCId/a10603050
http://arties.cbuc.cat/openccuc/a10603050/ http://viaf.org/viaf/169463957
http://arties.cbuc.cat/openccuc/a10667283/ http://cantic.bnc.cat/registres/CUCId/a10667283
http://arties.cbuc.cat/openccuc/a10667283/ http://viaf.org/viaf/303916880
http://arties.cbuc.cat/openccuc/a10895814/ http://cantic.bnc.cat/registres/CUCId/a10895814
http://arties.cbuc.cat/openccuc/a10895814/ http://viaf.org/viaf/107201286
http://arties.cbuc.cat/openccuc/a10895814/ http://d-nb.info/gnd/140511695
http://arties.cbuc.cat/openccuc/a10895814/ http://id.loc.gov/authorities/names/no2010103795
http://arties.cbuc.cat/openccuc/a10985256/ http://cantic.bnc.cat/registres/CUCId/a10985256
http://arties.cbuc.cat/openccuc/a10985256/ http://viaf.org/viaf/85925873
http://arties.cbuc.cat/openccuc/a10985256/ http://id.loc.gov/authorities/names/no2005021393
http://arties.cbuc.cat/openccuc/a10985256/ http://d-nb.info/gnd/141167203
http://arties.cbuc.cat/openccuc/a10985256/ http://dbpedia.org/resource/Emma_Roberts
http://arties.cbuc.cat/openccuc/a11019682/ http://cantic.bnc.cat/registres/CUCId/a11019682
http://arties.cbuc.cat/openccuc/a11019682/ http://viaf.org/viaf/157747131
http://arties.cbuc.cat/openccuc/a11045553/ http://cantic.bnc.cat/registres/CUCId/a11045553
http://arties.cbuc.cat/openccuc/a11045553/ http://viaf.org/viaf/126161085
I apply the filter
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX void: <http://rdfs.org/ns/void#>
PREFIX owl: <http://www.w3.org/2002/07/owl/>
PREFIX rdaGr2: <http://rdvocab.info/ElementsGr2/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbo:<http://dbpedia.org/ontology/>
SELECT *
WHERE {
?autid rdaGr2:dateOfBirth "1991" .
?autid owl:sameAs ?dbpediaid
FILTER regex(?dbpediaid,'dbpedia','i') .
}
to jut get one result:
http://arties.cbuc.cat/openccuc/a10985256/ http://dbpedia.org/resource/Emma_Roberts
And I want to send It to the Service to the DBPEDIA but just send this URL:
http://dbpedia.org/resource/Emma_Roberts
Not all the URL I get from the first part:
http://cantic.bnc.cat/registres/CUCId/a10603050
http://viaf.org/viaf/169463957
http://cantic.bnc.cat/registres/CUCId/a10667283
http://viaf.org/viaf/303916880
http://cantic.bnc.cat/registres/CUCId/a10895814
http://viaf.org/viaf/107201286
http://d-nb.info/gnd/140511695
http://id.loc.gov/authorities/names/no2010103795
http://cantic.bnc.cat/registres/CUCId/a10985256
http://viaf.org/viaf/85925873
http://id.loc.gov/authorities/names/no2005021393
http://d-nb.info/gnd/141167203
http://dbpedia.org/resource/Emma_Roberts
Because some of them are not related to the Dbpedia and then the DBPEDIA send me an error like this:
Virtuoso RDFZZ Error DB.DBA.SPARQL_REXEC('http://dbpedia.org/sparql', ...) returned Content-Type 'text/plain' status 'HTTP/1.1 400 Bad Request
'
Virtuoso 37000 Error SP030: SPARQL compiler, line 3: syntax error at '_' before ':'
SPARQL query:
define sql:big-data-const 0 SELECT ?naixement
WHERE { <http://dbpedia.org/resource/Emma_Roberts> <http://dbpedia.org/ontology/birthPlace> ?naixement .
FILTER (_::default-20-7 = <http://arties.cbuc.es:8990/CCUCAF>) }
SPARQL query:
define sql:big-data-const 0
#output-format:text/html
define sql:signal-void-variables 1 define get:soft "soft" define input:default-graph-uri <http://arties.cbuc.es:8990/CCUCAF> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX void: <http://rdfs.org/ns/void#>
PREFIX owl: <http://www.w3.org/2002/07/owl/>
PREFIX rdaGr2: <http://rdvocab.info/ElementsGr2/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbo:<http://dbpedia.org/ontology/>
SELECT ?naixement
WHERE {
?autid rdaGr2:dateOfBirth "1991" .
?autid owl:sameAs ?dbpediaid
filter regex( str(?dbpediaid),'dbpedia','i')
SERVICE <http://dbpedia.org/sparql>
{
?dbpediaid dbo:birthPlace ?naixement
}

FILTER regex(?dbpediaid,'dbpedia','i') should probably fail, since ?dbpediaid isn't a string; but a URI. Instead, you should be doing FILTER regex(str(?dbpediaid),'dbpedia','i'). The fact that your second query returns some results, though, makes it seem like the URI is implicitly being converted to a string. That might be an extension of the particular implementation you're using, though.
However, when I use Apache Jena to execute the following query, I get empty results:
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
select * where {
bind( dbpedia:Emma_Roberts as ?dbpediaid )
filter regex( ?dbpediaid,'dbpedia','i')
service <http://dbpedia.org/sparql> {
?dbpediaid dbpedia-owl:birthPlace ?naixement
}
}
-------------------------
| dbpediaid | naixement |
=========================
-------------------------
If I make the change to using str(?dbpediaid), I get results:
prefix dbpedia: <http://dbpedia.org/resource/>
prefix dbpedia-owl: <http://dbpedia.org/ontology/>
select * where {
bind( dbpedia:Emma_Roberts as ?dbpediaid )
filter regex( str(?dbpediaid),'dbpedia','i')
service <http://dbpedia.org/sparql> {
?dbpediaid dbpedia-owl:birthPlace ?naixement
}
}
--------------------------------------------------------------------------------------
| dbpediaid | naixement |
======================================================================================
| dbpedia:Emma_Roberts | <http://dbpedia.org/resource/Rhinebeck_(village),_New_York> |
--------------------------------------------------------------------------------------
Note that this could be a very expensive query if the query engine does the service part first, as it would retrieve all the dbpedia-owl:birthPlace triples first, bring them back, and then match against the particular one that you care about. There may be different behavior if rather than using bind, like I did above, the value of ?dbpediaid is simply coming from some triple matches.

Related

Retrieve country code from RDF source recieved via Sparql query

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

Querying dbpedia, not getting expected result not sure what's the mistake

My query is
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?craft where
{
?craft <http://dbpedia.org/property/title> ?v.
}
Now this returning lot of results,
but nothing related to Steve Jobs or Tim Cook, even though in their page there is a property named title.
dbp:title dbr:List_of_Apple_Computer_CEOs
http://dbpedia.org/page/Steve_Jobs
The query:
PREFIX dbp: <http://dbpedia.org/property/>
SELECT (COUNT(*) AS ?nb_result)
WHERE {
?craft dbp:title ?v .
}
returns:
nb_result
---------
1566113
The public query endpoint for DBpedia limits the number of results to 10,000, among other restrictions. So your chances of retrieving any specific statement there are very small. If you worry that the data at the query endpoint is different than at the front end, you can check that the data is there with the query:
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?prop ?value
WHERE {
dbr:Steve_Jobs ?prop ?value .
}
and compare with what's displayed at http://dbpedia.org/page/Steve_Jobs.

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.

Getting a list of American physicists from DBpedia using SPARQL

I want to query the American Physicsts and get the list of physicists. How can I do this?
The SPARQL you need would look like this ....
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX dcterms: <http://purl.org/dc/terms/>
SELECT *
WHERE {
?s dcterms:subject category:American_physicists .
}
see results here
If you want the list with some extra predicates you need to join more triple patterns using the variable ?s. For instance, to retrieve the birthdate for each physicist ...
PREFIX category: <http://dbpedia.org/resource/Category:>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbpedia: <http://dbpedia.org/ontology/>
SELECT *
WHERE {
?s dcterms:subject category:American_physicists .
?s dbpedia:birthDate ?bithdate .
}
results here