SPARQL replace empty attributes with others - sparql

I made a SPARQL query using the official DBpedia endpoint.
I'll take it for example for the main Question:
SELECT ?museum ?EnglishAbstract ?RussianAbstract
WHERE {
?museum dbpedia-owl:abstract ?EnglishAbstract.
?museum a dbpedia-owl:Museum.
filter(lang(?EnglishAbstract)='en')
optional{
?museum dbpedia-owl:abstract ?RussianAbstract.
?museum a dbpedia-owl:Museum.
filter(lang(?RussianAbstract)='ru')
}}
GROUP BY ?museum
With this query (that works good), I find for every row (museum) a bunch of Abstract in 2 languages: English and Russian.
The Russian abstracts are present only if available because I use "OPTIONAL".
Obviously I get a lot of empty attributes.
I would replace the blank attribute with the English abstract (always present).
Reading the W3C SPARQL page, I found that there is a particular testing value that is true when a variable is set: Bound
I would like to write something like this:
If Bound (?RussianAbstract), "?RussianAbstract", "?EnglishAbstract"
--> if RussianAbstract is present, hold it; else, put the EnglishAbstract instead of it.
Does anyone know how can I make it work?

One way to do this is to use COALESCE:
SELECT ?museum (COALESCE(?RussianAbstract, ?EnglishAbstract) as ?Abstract)

Related

Get every places names in geonames with SPARQL DBPEDIA

I'm quite new to sparql.
I founded this query to get all country in the UN
select distinct ?s
where { ?s a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> }
So I tried to adapt it to Geonames with:
select distinct ?s
where { ?s a <http://dbpedia.org/page/GeoNames> }
But it doesn't work. How can I get every place's name in geonames?
I hope someone can help me with that!
Every publisher uses its own namespace and method to generate URIs of the published entities. The nice thing about Linked Open Data is that it allows such independence while URIs can still be linked using agreed open standards. When different URI represent the same thing, this is declared by linking them with owl:sameAs.
Your query attempt assumes that DPpedia and Geonames use the same URIs, if I understood correctly the intention (I'm not sure qhat you mean by "to adapt"). What you need to do is use two separate variables, and then specify that from the owl:sameAs mappings, you want only those from Geonames.
select distinct *
where { ?cuntryDBpedia a <http://dbpedia.org/class/yago/WikicatMemberStatesOfTheUnitedNations> ;
owl:sameAs ?countryGeonames .
FILTER REGEX (?countryGeonames,"geonames.org")
}

Get movie(s) based on book(s) from DBpedia

I am new to SPARQL and trying to fetch a movie adapted from specific book from dbpedia. This is what I have so far:
PREFIX onto: <http://dbpedia.org/ontology/>
SELECT *
WHERE
{
<http://dbpedia.org/page/2001:_A_Space_Odyssey> a ?type.
?type onto:basedOn ?book .
?book a onto:Book
}
I can't get any results. How can I do that?
When using any web resource, and in your case the property :basedOn, you need to make sure that you have declared the right prefix. If you are querying from the DBpedia SPARQL endpoint, then you can directly use dbo:basedOneven without declaring it, as it is among predefined. Alternatively, if you want to use your own, or if you are using another SPARQL client, make sure that whatever short name you choose for this property, you declare the prefix for http://dbpedia.org/ontology/.
Then, first, to get more result you may not restrict the type of the subject of this triple pattern, as there could be movies that actually not type as such. So, a query like this
select distinct *
{
?movie dbo:basedOn ?book .
?book a dbo:Book .
}
will give you lots of good results but not all. For example, the resource from your example will be missing. You can easily check test the available properties between these two resource with a query like this:
select ?p
{
{<http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey> }
UNION
{ <http://dbpedia.org/resource/2001:_A_Space_Odyssey> ?p <http://dbpedia.org/resource/2001:_A_Space_Odyssey_(film)>}
}
You'll get only one result:
http://www.w3.org/2000/01/rdf-schema#seeAlso
(note that the URI is with 'resource', not with 'page')
Then you may search for any path between the two resource, using the method described here, or find a combination of other patterns that would increase the number of results.

dbpedia sparql movie query sql

Hi I'm trying to learn how to query DBpedia using SPARQL. I can't find any website/source that shows me how do this and I'm finding it difficult to learn how to use all the properties (like the ones available at http://mappings.dbpedia.org/index.php?title=Special%3AAllPages&from=&to=&namespace=202 ). Any good source I can learn from?
So for example if I want to check if the wikipedia page http://en.wikipedia.org/wiki/Inception is a movie (property film) or not, how do I do that?
The wikipedia URL http://en.wikipedia.org/wiki/Inception maps to the dbpedia URI http://dbpedia.org/resource/Inception. Dbpedia has a SPARQL endpoint at: http://dbpedia.org/sparql, which you may use to run queries either programmatically or via the html interface.
To check if http://dbpedia.org/page/Inception is a "movie", you have many options. To give you an idea:
If you know the URI of "movie" in dbpedia (it is http://schema.org/Movie), then run an ASK query to check against that type. ASK will return true/false based on whether the pattern in the where clause is valid against the data:
ASK where {
<http://dbpedia.org/resource/Inception> a <http://schema.org/Movie>
}
If you don't know the URI of "movie" then you have a number of options. For example:
Execute an ASK query with a filter on whether the resource has a type that contains the word "movie" somewhere in its uri (or its associated rdfs:label, or both). You would use a regular expression for this:
ASK where {
<http://dbpedia.org/resource/Inception> a ?type .
FILTER regex(str(?type), "^.*movie", "i")
}
Same idea, but return all matches and post-process the results (programmatically I pressume) to see if they match your request:
select distinct ?type where {
<http://dbpedia.org/resource/Inception> a ?type .
FILTER regex(str(?type), "^.*movie", "i")
}
Return all the types of the resource without applying a filter and post-process to see if they match your request:
select distinct ?type where {
<http://dbpedia.org/resource/Inception> a ?type
}
Many options. The SPARQL spec is you number one resource.
First I suggest you start reading up on what exactly SPARQL is. There are tons of really good tutorials such as: this.
If you want to write SPARQL queries on dbpedia, there are various endpoints that you can use. They don't always accept all features that are supported by SPARQL, but if you don't want to go through the trouble of installing one locally, they can be a relatively reliable test environment. The queries that I am going to write below, have been tested on Virtuoso endpoint.
Let's say you want to find all the movies in dbpedia. You first need to know what is the URI for a movie type in dbpedia. If you open Inception in dbpedia, you can see that the type dbpedia-owl:Film is associated to it. So if you want to get the first 100 movies, you just need to call:
select distinct *
where {
?s ?o dbpedia-owl:Film
} LIMIT 100
If you want o know more about each of these movies, you just need to expand your queries by expanding the triples.
select distinct *
where {
?s ?p dbpedia-owl:Film.
?s ?x ?y.
} LIMIT 100

SPARQL query: How to use wikiPageDisambiguates for redirect

I'm trying to retrieve thumbnail links for a given entity name. My current query looks like the following and works for most of the cases.
select ?value {
<http://dbpedia.org/resource/Angela_Merkel> dbpedia-owl:thumbnail ?value
}
However, for some cases e.g "CDU" it fails, because the entity is ambiguous. See this Example in the SPARQL Explorer.
In these cases I would like to return the thumbnail of the first wikiPageDisambiguates entry. So, for "CDU" it would be the thumbnail of this page. Can somebody tell me how to do this in SPARQL?
In these cases I would like to return the thumbnail of the first wikiPageDisambiguates entry. So, for "CDU" it would be the thumbnail of this page. Can somebody tell me how to do this in SPARQL?
There's no order on these. Any representation necessarily has to put them in some order, but they're not actually ordered in the underlying RDF. You can retrieve an arbitrary one, but not "the first". For instance, look at the results from select * where { dbpedia:CDU ?p ?o }. There are a bunch of disambiguation links. Now, you can follow those links, if they are there, to get thumbnails:
select ?thumbnail where {
dbpedia:CDU dbpedia-owl:wikiPageDisambiguates?/dbpedia-owl:thumbnail ?thumbnail
}
SPARQL results
The property path dbpedia-owl:wikiPageDisambiguates?/dbpedia-owl:thumbnail uses a question mark after the wiki page disambiguation property. That means that there can be either zero or one occurrences of the the property. Then it has to be followed by a dbpedia-owl:thumbnail link. That means that if dbpedia:CDU has a thumbnail property, you'll get it, or if it has a a disambiguation that has a thumbnail, you'll get that.
If you do want to impose some ordering, you can do that, but you'll have to determine what it should be. You can use order by to specify the ordering, and limit to specify that you want just the first one. E.g., you could do:
select ?thumbnail where {
dbpedia:CDU dbpedia-owl:wikiPageDisambiguates? ?cdu .
?cdu dbpedia-owl:thumbnail ?thumbnail ;
rdfs:label ?label
}
order by ?label
limit 1
SPARQL results

Wikidata - Is this query possible?

I want to do a query on WikiData where I get all items, that are somehow connect to another item.
For example I have Item "Vienna" (Q1741). Now I want to get all Items, that have Item Vienna in any property.
The API I currently use is the one from wmflabs. Here I can do a query like
claim[189:1741]
This gives me every item with property "Place of discovery" (P189) = "Vienna" (Q1741).
But what I want is something like
claim[*:1741]
to get all items where any property fits "Vienna", for example "Birthplace" (P19), "Place of Death" (P20) or anything else. But wildcards are not working here.
Is this possible? How?
PS: I'm not bound to this API, I could use any API to wikidata accessible via JS. There are also some SparQL endpoints to Wikidata-Dumps available (like wikidataldf), but I don't know how stable they are. But if anyone could help with a solution using SPARQL, I would be glad, too.
you can use sparql query to Dbpedia to get result for you particular resource, which here is Vienna.
To get all property and their value of resource Vienna use can use
select ?property ?value where {
<http://dbpedia.org/resource/Vienna> ?property ?value
}
Check here
You can choose specific properties of resource using sparql query like this .
select ?country ?density ?timezone ?thumbnail where {
<http://dbpedia.org/resource/Vienna> dbpedia-owl:country ?country;
dbpedia-owl:populationDensity ?density;
dbpedia-owl:timeZone ?timezone;
dbpedia-owl:thumbnail ?thumbnail.
}
Check
maybe something like this:
SELECT ?node WHERE {?node ?pred wd:Q1741}
see on Wikidata Query Service
But what I want is something … to get all items where any property fits "Vienna"[.]
In SPARQL this is very easy. E.g., on DBpedia's SPARQL endpoint:
select ?resource where {
?resource ?property dbpedia:Vienna
}
SPARQL results (limited to 100)
There are already some SPARQL endpoints for Wikidata available. However, they are still beta and only reflect the data from the last dump.
Your query would be this one
See also this help page on Wikidata