SPARQL get dbpedia ressources by category - sparql

I'm a beginner in SPARQL. And I'm having problems to get the latitude and longitude of all university by city on DBpedia.
I tried multiple things without success.
This page shows the universities of Paris on the dbo:campus property, so I like to get the list of the universities with this property and after that get the geographics coordinates.
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name, ?univ, ?lat, ?long WHERE {
?p rdf:type dbo:Place.
?p rdfs:label ?name.
?p dbo:campus ?u.
?u geo:lat ?lat.
?u geo:long ?long.
?u rdfs:label ?univ
FILTER(LANG(?name) = "en").
FILTER(?name = "Paris")
}
I check this post DBpedia SPARQL Query US Universities but it doesn't work with another country.

If you read "is SOME_PROPERTY of"on a rendered DBpedia page, this means the inverse direction, i.e., it shows the triple in its inverted form. Thus, you have to invert the triple pattern in the SPARQL query. For your example, it means that universities are the subject and Paris the object:
?u dbo:campus ?p
The labels are language tagged in DBpedia; thus, FILTER(?name = "Paris") is not enough. Adding the English language tag helps:
FILTER(?name = "Paris"#en)
A working query would be
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
SELECT ?name, ?univ WHERE {
?p rdf:type dbo:Place.
?p rdfs:label ?name.
?u dbo:campus ?p.
?u geo:lat ?lat.
?u geo:long ?long.
?u rdfs:label ?univ
FILTER(LANG(?name) = "en").
FILTER(?name = "Paris"#en)
}
Some comments:
Using the label to match a resource can lead to unwanted results. Resources are identified by URIs; thus, use the URI if possible. The VALUES clause is a cool feature of SPARQL 1.1 to support inline data.
If you use the URI, you could omit the rdf:type triple pattern since you wouldn't have to filter for resources of a specific type given the label.
The official SPARQL standard doesn't allow commas in between the projection variables; this is Virtuoso-specific syntax.
SPARQL supports the more compact Turtle syntax.
A FILTER doesn't need a . at the end.
Try to use LANGMATCHES for matching languages in literals.
A "better" query could be:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT ?name ?univ ?lat ?long
WHERE
{ VALUES ?p { dbo:Paris }
?p rdfs:label ?name .
?u dbo:campus ?p ;
geo:lat ?lat ;
geo:long ?long ;
rdfs:label ?univ
FILTER langMatches(lang(?name), "en")
}

Related

SPARQL query for specific information

I am struggling a lot to create some SPARQL queries. I need 3 specific things, and this is what i have so far:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
select distinct ?title ?author ?country ?genre ?language
where {
?s rdf:type dbo:Book;
dbp:title ?title;
dbp:author ?author;
dbp:country ?country;
dbp:genre ?genre;
dbp:language ?language.
}
This query will bring me a list of all books. What i really need is the ability to add some filters to this code. There are 3 things i want to filter by:
specific title name (e.g., search for title with "harry potter")
specific author name (e.g., search for author with "J. K. Rowling")
specific genre (e.g., search for genre with "adventure")
I've been struggling with this for too long and i simply cannot define these 3 queries. I am trying to implement a function that will execute a SPARQL statement using parameters passed by an user form. I found a few examples here and in the web but i just cannot build these 3 specific queries.
As noted, not every book has every property, and some of your properties may not exist at all. For instance, I changed dbp:genre to dbo:literaryGenre, based on the description of Harry Potter and the Goblet of Fire. See query form, and results.
SELECT *
WHERE
{ ?s rdf:type dbo:Book .
?s rdfs:label ?bookLabel .
FILTER(LANGMATCHES(LANG(?bookLabel), 'en'))
?s dbo:author ?author .
?author rdfs:label ?authorLabel .
FILTER(LANGMATCHES(LANG(?authorLabel), 'en'))
?authorLabel bif:contains "Rowling"
OPTIONAL { ?s dbp:country ?country .
?country rdfs:label ?countryLabel .
FILTER(LANGMATCHES(LANG(?countryLabel), 'en')) }
OPTIONAL { ?s dbo:literaryGenre ?genre .
?genre rdfs:label ?genreLabel .
FILTER(LANGMATCHES(LANG(?genreLabel), 'en')) }
OPTIONAL { ?s dbp:language ?language .
?language rdfs:label ?languageLabel .
FILTER(LANGMATCHES(LANG(?languageLabel), 'en')) }
}

How to extract data from DBpedia using SPARQL

I'm trying to extract some data from dbpedia using SERVICE function of SPARQL.
In fact I want to extract the names, the lat and lot of all New York theaters. To check if an instance is a theater I can use http://dbpedia.org/class/yago/Theater104417809​. One example of a theater could be http://dbpedia.org/resource/Grand_Theatre_(New_York_City).
How to use service function for getting what I need in SPARQL?
** EDIT **
The query that I'm trying is the following one, but is not returning any value.
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT *
WHERE {
SERVICE <http://dbpedia.org/sparql/> {
SELECT ?teatreName ?lat ?long
WHERE {
?teatre rdf:type dbpedia:Theatre .
?teatre foaf:name ?teatreName .
?teatre geo:lat ?lat .
?teatre geo:long ?long .
?teatre dbp:city ?ciutat .
?ciutat rdfs:label "New York City"#en
}
}
}
It's not an issue with federated querying, but with your DBpedia query. dbp:city is not an object property but simply of type rdf:Property, thus it's untyped. In your case, it maps to literals which means, you have to use the literal directly. The weird thing here is, that for some reasons you have to use the datatype http://www.w3.org/1999/02/22-rdf-syntax-ns#langString explicitly instead of "New York City"#en - that's clearly not intuitive for any user. Not sure whether this happened due to the DBpedia extraction or is the expected behaviour of Virtuoso.
PREFIX dbpedia: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX geos: <http://www.opengis.net/ont/geosparql#>
PREFIX geof: <http://www.opengis.net/def/function/geosparql/>
PREFIX : <http://www.semanticweb.org/frubi/ontologies/2017/10/puntsWIFI#>
SELECT *
WHERE {
SERVICE <http://dbpedia.org/sparql/> {
SELECT ?teatreName ?lat ?long
WHERE {
?teatre rdf:type dbpedia:Theatre .
?teatre foaf:name ?teatreName .
?teatre geo:lat ?lat .
?teatre geo:long ?long .
?teatre dbp:city "New York City"^^<http://www.w3.org/1999/02/22-rdf-syntax-ns#langString>
}
}
}

Finding resource label

I'm currently learning SPARQL, and I'm exploring the data from dbpedia. Why does this query work:
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT
?label
WHERE {
dbr:Leipzig rdfs:label ?label.
} LIMIT 20
But this does not (i.e. id does not return anything)
PREFIX geo: <https://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbr: <http://dbpedia.org/resource/>
SELECT
?label ?lat
WHERE {
dbr:Leipzig rdfs:label ?label.
dbr:Leipzig geo:lat ?lat.
} LIMIT 20
Because the protocol of the WGS 84 Geo namespace is http and not https, i.e. http://www.w3.org/2003/01/geo/wgs84_pos#

How to get such data as abstraction, location and link for image in Dbpedia?

I am trying to get some data about museum but it is not successfull. This is my code. I know the name of the museum, so I want to get data about this museum
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?abstract ?location WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name dbpedia:Chekhov_Shop .
}
The dbpprop:name that you have selected (dbpedia:Chekhov_Shop) is in fact a string. If you look at the dbpedia page it has been defined as The Chekhov Shop. Therefore, my suggestion is to filter your query based on the name you like to be displayed:
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT * WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name ?name.
Filter (str(?name)="The Chekhov Shop")
}
And if you need more information about this specific architectural structure, you start exploring. For example,
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT distinct * WHERE {
?architectural_structure rdf:type dbpedia-owl:Museum .
?architectural_structure dbpedia-owl:location dbpedia:Taganrog .
?architectural_structure dbpprop:name ?name.
?architectural_structure dbpprop:location ?location.
?architectural_structure dbpedia-owl:abstract ?abstract.
Filter (str(?name)="The Chekhov Shop")
}
In general, when you are faced with a triple store try to find all ?o ?p ?s and then see where you need to put a specific predicate.

DBpedia SPARQL Query US Universities

I created a SPARQL query that I'm running on the DBpedia SNORQL SPARQL endpoint. The purpose of the query is to get a list of universities or colleges in the United States, including their longitude, latitude, and endowment. The query seems to be working but seems to be missing some records and/or attributes. So, for example, Harvard University doesn't show up in the result, even though its DBpedia record exists and the attributes should match my query. I'm not sure why that record doesn't show up. Another example is University of Massachusetts Boston, which comes up as a query result, but the result doesn't get the longitude and latitude attributes, even though the record contains those attributes. Here's the SPARQL Query:
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX d: <http://dbpedia.org/ontology/>
SELECT ?uni ?link ?lat ?long ?endowment
WHERE {
?s foaf:homepage ?link ;
rdf:type <http://schema.org/CollegeOrUniversity> ;
rdfs:label ?uni
OPTIONAL {?s geo:lat ?lat ;
geo:long ?long .
?s d:endowment ?endowment . }
FILTER (LANGMATCHES(LANG(?uni), 'en'))
{?s dbpedia2:country "U.S."#en . }
UNION
{?s dbpedia2:country "U.S." . }
UNION
{?s d:country :United_States . }
}
ORDER BY ?s
The query you posted will only select entities with a foaf:homepage and Harvard University does not have one. (That is, the resource does not have a foaf:homepage property. Obviously the university does have a homepage.) UMass Boston doesn't match the optional pattern --
OPTIONAL {?s geo:lat ?lat ;
geo:long ?long .
?s d:endowment ?endowment . }
-- because that pattern only matches when ?s has a geo:lat, a geo:long, and a d:endowment. Though the pattern is optional, the whole pattern must either match or not; you do not get partial matches.
Here's your query, reworked to use the built-in namespaces that the DBPedia SPARQL endpoint currently supports (that list is subject to change over time), with the OPTIONAL parts broken down as necessary, and moved to the end. (Moving them to the end is just an aesthetic consideration.) I tried some various constraints, and it is interesting to note that only 32 universities have the dbpprop:country "U.S."#en, but 273 have dbpprop:country "United States"#en. There are 7620 results in total.
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpprop: <http://dbpedia.org/property/>
SELECT ?label ?homepage ?lat ?long ?endowment
WHERE {
?school a <http://schema.org/CollegeOrUniversity>
{ ?school dbpedia-owl:country dbpedia:United_States }
UNION
{ ?school dbpprop:country dbpedia:United_States }
UNION
{ ?school dbpprop:country "U.S."#en }
UNION
{ ?school dbpprop:country "United States"#en }
OPTIONAL { ?school rdfs:label ?label .
FILTER (LANGMATCHES(LANG(?label), 'en')) }
OPTIONAL { ?school foaf:homepage ?homepage }
OPTIONAL { ?school geo:lat ?lat ; geo:long ?long }
OPTIONAL { ?school dbpedia-owl:endowment ?endowment }
}
SPARQL Results
You are looking for foaf:homepage but some of them do not have this assigned. That is the first thing that caught my eyes. Check the rest of the query by removing bit by bit each element and see what the result set has to offer.