How to apply global language filter across several fields in Sparql - sparql

I'm learning semantic web and using dbpedia to learn sparql. Below is an example to get all the schools in IL, USA. If you look at the filters, I'm filtering 2 fields (name and city) for the English language only.
Question - instead of 2 filter expressions or 'n' number of filter expressions per field, is it possible to set a global filter condition that can be applied to set of fields?
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
select ?link ?name ?city ?website where {
SERVICE <https://dbpedia.org/sparql> {
?link rdf:type dbo:School .
?link rdfs:label ?name .
?link dbo:country dbr:United_States .
?link dbo:state dbr:Illinois .
?link dbo:city ?city_link .
?city_link rdfs:label ?city .
OPTIONAL {
?link dbp:website ?website .
}
}
FILTER (lang(?name)="en") .
FILTER (lang(?city)="en") .
}

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

Sparql dbpedia search article by containing word

I've created sparql query for searching person and article
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?name ?person ?description
WHERE {
?person rdf:type dbo:Person.
?person foaf:name ?name .
?person rdfs:comment ?description .
FILTER (regex(?description, 'morgan freeman' ,'i')) .
FILTER (lang(?description) = 'in')
}
ORDER BY ?name
and its working when searching morgan freeman
but not working when searching article God Created All Things or Birth of Jesus
how to search acticle on dbpedia with sparql query?

Get birthplace from DBpedia people in SPARQL

I have the following code to retrieve all people that was born in Barcelona
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?person ?birthPlace
WHERE {
?person rdfs:label ?label.
?person rdf:type dbo:Person.
?person <http://dbpedia.org/property/birthPlace>
<http://dbpedia.org/resource/Barcelona>.
}
However, I do not know how to get the birthPlace. I want a variable that says next to each name that Barcelona is the place of birth. Any ideas?
How about this:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?person ?birthPlace
WHERE {
?person rdfs:label ?label.
?person rdf:type dbo:Person.
?person <http://dbpedia.org/property/birthPlace> ?birthPlace.
FILTER (?birthPlace = <http://dbpedia.org/resource/Barcelona>)
}
Note that your query has a pattern to match labels, but the labels are not returned. That leads to duplicate results because some people have multiple labels (in different languages). Remove the pattern, or add ?label to the SELECT clause.
You can abbreviate <http://dbpedia.org/property/birthPlace> to dbp:birthPlace.

Writing Query result to .ttl file format

Any ideas on how to save the results of this SPARQL query into a TURTLE format?
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
select distinct ?city ?labelEn{
?city a dbo:City.
?city rdfs:label ?labelEn.
filter(lang(?labelEn) = 'en').
}
LIMIT 10
One way is to just ask the DBpedia endpoint to do so. Of course, you may not be expecting the reification that results there.
Later comments suggest what you really want is rather different from your original question... More like --
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
CONSTRUCT
{ ?city a dbo:City .
?city rdfs:label ?labelEn .
}
WHERE
{ ?city a dbo:City .
?city rdfs:label ?labelEn .
FILTER(lang(?labelEn) = 'en')
}
-- which again you can ask the endpoint to serialize as Turtle.

SPARQL: Federated query gives no result when using local file, while same query on dbpedia does

the local file uploaded on stardog:
#prefix dbo: <http://dbpedia.org/ontology/> .
#prefix dbr: <http://dbpedia.org/resource/> .
dbr:United_States dbo:leader dbr:John_Roberts ,
dbr:Joe_Biden ,
dbr:Barack_Obama ,
dbr:Paul_Ryan .
1.query using the local file:
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?person ?o
FROM <http://example.com/leaders.ttl>
WHERE{
dbr:United_States dbo:leader ?person .
SERVICE <http://dbpedia.org/sparql> { ?person dbo:abstract ?o .}
}
2.Same query using only dbpedia will give results:
PREFIX dbr: <http://dbpedia.org/resource/>
PREFIX db: <http://dbpedia.org/>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?person ?o
FROM <http://example.com/leaders.ttl>
WHERE{
#dbr:United_States dbo:leader ?person .
SERVICE <http://dbpedia.org/sparql> { dbr:United_States dbo:leader ?person. ?person dbo:abstract ?o.}
}
Using the second query will result in a colum with the leaders and a column of abstract of the leaders in all languages available from dbpedia. Why does the first query where I use the local rdf file not work? The select query on the local file with dbr:United_States dbo:leader ?person . returns exactly the same column with the same leaders as running it directly on the dbpedia endpoint: dbpedia:John_Roberts, dbpedia:Joe_Biden, dbpedia:Barack_Obama, dbpedia:Paul_Ryan.
Why does the first query give no results?