Query DBpedia to get abstract for different inputs - sparql

I have a question I need to build a single query to DBpedia such that, If I give any one of these as input like a City name or a person name or a Institute name or a Instrument name can I get its abstract as a output???
For instance,
New York- New York is a state in the Northeastern and Mid-Atlantic regions of the United States......
Mars- Mars is the fourth planet from the Sun and the second smallest planet in the Solar System....
Michael Jackson- Michael Joseph Jackson was an American singer, songwriter, dancer, and actor......
I have tried but its not working for all.
SELECT ?abstract WHERE {
<http://dbpedia.org/resource/New_York>
<http://dbpedia.org/ontology/abstract>
?abstract
FILTER langMatches(lang(?abstract), "en")
}

If you intend to get the abstract for multiple things, supply those multiple things within a VALUES block. I found that matching by ?name worked sufficiently well for name-based searches.
SELECT DISTINCT ?abstract WHERE {
[ rdfs:label ?name
; dbpedia-owl:abstract ?abstract
] .
FILTER langMatches(lang(?abstract),"en")
VALUES ?name { "New York"#en }
}
LIMIT 10

Related

How to get the number of languages a Wikipedia page is available in? (SPARQL query)

I'm trying to have a list of Italian books from the 1980 on and the number of Wikipedia pages their original Wikipedia page has been translated into, for instance I would like to have:
Book, number
The name of the Rose, 5
Where 5 is the number of languages The Name of the Rose has been translated into in Wikipedia, for instance there is an English wiki page, a Dutch one, a Spanish one, a French one, a Greek one.
Here is the query so far:
SELECT ?item ?label
WHERE
{
VALUES ?type {wd:Q571 wd:Q7725634} # book or literary work
?item wdt:P31 ?type .
?item wdt:P577 ?date FILTER (?date > "1980-01-01T00:00:00Z"^^xsd:dateTime) . #dal 1980
?item rdfs:label ?label filter (lang(?label) = "it")
?item wdt:P495 wd:Q38 .
}
I get the list of books, but I can't find the right property to look for.
Did you actually get The Name of the Rose in your example? Because its date of publication is set to 1980 (which is = 1980-01-01 for our purposes), I had to change your query to a >= comparison.
Then, using COUNT() and GROUP BY as mentioned in the comment gets you what you want. But if you really just need the number of sitselinks, there is a shortcut that may be useful. It was added because that number is often used as a good proxy for an item's popularity, and using the precomputed number is vastly more efficient than getting all links, grouping, and counting.
SELECT ?book ?bookLabel ?sitelinks ?date WHERE {
VALUES ?type { wd:Q571 wd:Q47461344 wd:Q7725634 }
?book wdt:P31 ?type;
wdt:P577 ?date;
wdt:P495 wd:Q38;
wikibase:sitelinks ?sitelinks.
FILTER((?date >= "1980-01-01T00:00:00Z"^^xsd:dateTime) && (?date < "1981-01-01T00:00:00Z"^^xsd:dateTime))
SERVICE wikibase:label { bd:serviceParam wikibase:language "it,en". }
}
Query
Note that the values may slightly differ from the version with group & count because here sites such as commons or wikiquote are also included.

Wikidata: Get all non-classical Musicians via SPARQL query

I hope that this kind of question is allowed here as it is more a Wikidata specific question. Anyways, I try to get all non-classical-music musicians from Wikidata by SPARQL. Right now I have this code:
SELECT ?value ?valueLabel ?born WHERE {
{
SELECT DISTINCT ?value ?born WHERE {
?value wdt:P31 wd:Q5 . # all Humans
?value wdt:P106/wdt:P279* wd:Q639669 . # of occupation or subclass of occupation is musician
?value wdt:P569 ?born . # Birthdate
FILTER(?born >= "1981-01-01T00:00:00Z"^^xsd:dateTime) # filter by Birthyear
}
ORDER BY ASC(?born)
#LIMIT 500
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en,ger". }
}
this gets me (theoretically) all People whose occupation is Musician (https://www.wikidata.org/wiki/Q639669) and who were born after 1900. (Theoretically because this query runs way too long and I had to break it into smaller chunks)
What I am after however is to exclude People who are primary classical musicians. Is there any property I am not aware of? Otherwise, how would I change my query to be able to filter by specific properties (like Q21680663, classical composer)?
Thanks!
If you check the Examples tab in the query interface and type music into the search field, you'll find an example that almost hits the spot:
Musicians or singers that have a genre containing 'rock'.
I've used that mostly to just get a list of all musicians with their genres. I finally settled on a MINUS query subtracting any musician who touches western classical music or baroque music, the latter included specifically to get Bach, the old bastard.
SELECT DISTINCT
?human ?humanLabel
(GROUP_CONCAT(DISTINCT ?genreLabel; SEPARATOR = ", ") AS ?genres)
WHERE {
{
?human wdt:P31 wd:Q5;
wdt:P106 wd:Q639669;
wdt:P136 ?genre.
} MINUS {
VALUES ?classics {
wd:Q9730
wd:Q8361
}
?human wdt:P136 ?classics.
}
# This is just boilerplate to get the labels.
# it's slightly faster this way than the label
# service, and the query is close to timing out already
?genre rdfs:label ?genreLabel.
FILTER((LANG(?genreLabel)) = "en")
?human rdfs:label ?humanLabel.
FILTER((LANG(?humanLabel)) = "en")
}
GROUP BY ?humanLabel ?human
In the Query Interface: 25,000 results in 20sec
Here's a taste of what the results look like (from some intermediate version, because I'm not redoing the table now).
artist
genres
Gigi D'Agostino
Latin jazz, Italo dance
Erykah Badu
neo soul, soul music
Yoko Kanno
jazz, blues, pop music, J-pop, film score, New-age music, art rock, ambient music
Michael Franks
pop music, rock music
Harry Nilsson
rock music, pop music, soft rock, baroque pop, psychedelic rock, sunshine pop
Yulia Nachalova
jazz, pop music, soul music, contemporary R&B, blue-eyed soul, estrada
Linda McCartney
pop rock
From the original example, you may want to try also including singers. The following, replacing the existing line with "P106" does that, and results in about twice as many results. But it often times out.
VALUES ?professions {
wd:Q177220
wd:Q639669
}
wdt:P106 ?professions;
Query including singers, 53,000 results but may time out
The example also uses the following to cut down results rather drastically, by including only items with a certain number of statements, assuming those correlate with... something. You may want to experiment with it to focus on the most significant results, or to give you room to avoid the timeout with other changes. Maybe trying lower limits than 50 to find the right balance is a good idea, though.
?human wikibase:statements ?statementcount.
FILTER(?statementcount > 50 )
A query with singers and the statement limit
This is an earlier version. It excludes all the listed genres, but includes any musician linked to any other genre, and there are many of them that would probably qualify as "classics". The filter uses the "NOT IN" construct, which seems cleaner to me than filtering based on labels.
SELECT DISTINCT
?human ?humanLabel
(GROUP_CONCAT(DISTINCT ?genreLabel; SEPARATOR = ", ") AS ?genres)
WHERE {
?human wdt:P31 wd:Q5;
wdt:P106 wd:Q639669;
wdt:P136 ?genre.
# The "MAGIC": Q9730 is "Western Classical Music"
# Q1344 is "opera"
# Then I noticed Amadeus, Wagner, and Bach all slipped through and expanded the list, and it's a really
# ugly way of doing this
FILTER(?genre NOT IN(wd:Q9730, wd:Q1344, wd:Q9734, wd:Q9748, wd:Q189201, wd:Q8361, wd:Q2142754, wd:Q937364, wd:Q1546995, wd:Q1746028, wd:Q207338, wd:Q3328774, wd:Q1065742))
?genre rdfs:label ?genreLabel.
FILTER((LANG(?genreLabel)) = "en")
?human rdfs:label ?humanLabel.
FILTER((LANG(?humanLabel)) = "en")
}
GROUP BY ?humanLabel ?human
This gets me 26,000 results. View in Query Interface
Note that this will still return artists that have "western classical music" among their genres, aw long as they are also linked to other genres. To exclude any musician ever dabbling in the classics, you'll have to start a daytime top-30 radio station use a MINUS construct to, essentially, subtract all those.

Wikidata SPARQL query is missing part of expected results

On Wikidatas SPARQL endpoint i tried to find all cities worldwide with a population greater than 100000.
I'm getting lots of correct results with my query. But when i checked for some particular cities they didn't show up in the list.
My query:
SELECT DISTINCT ?cityLabel ?population ?coord ?countryLabel ?shortCountry ?city WHERE {
?city (wdt:P31/(wdt:P279*)) wd:Q515;
wdt:P1082 ?population;
wdt:P625 ?coord.
FILTER(?population > 100000 )
?city wdt:P17 ?country.
?country wdt:P298 ?shortCountry.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY ASC (?shortCountry)
I looked for the following cities in the results:
Berlin
Beirut
Lyon
But they didn't show up.
Try it out here
Ok i figured something out for myself:
I dug a little deeper into the wikidata object of the cities i mentioned and found out, that they are no instance of "city" (Q515).
Berlin belongs to e.g. "capital", "city with millions of inhabitants", "metropolis"
Lyon belongs to "commune of france" and "big city"
Beirut belongs to e.g. "capital" and "big city"
But none of them is listed as a "city". That's for sure not intentionally because other bigger cities are also an instance of "city" e.g. Paris
So in conclusion:
Check all the categories your filtering for and check your expected results to match them.

How to extract persons on a wikipedia list using dbpedia/sparql

I'm trying to extract all persons that won a (Gold) medal at the Olympics and ideally their birth location using the dbpedia SPARQL query. Basically it's this list I'm aiming at: https://de.wikipedia.org/wiki/Liste_der_olympischen_Medaillengewinner_aus_Spanien
I guess it must somehow work with this piece of code:
yago-res:wikicategory_Olympic_bronze_medalists_for_Spain
This doesn't work:
SELECT ?res
WHERE {
?res yago-res:wikicategory_Olympic_bronze_medalists_for_Spain .
}
any ideas?
To get all the spanish persons that have won the gold medal in olympic
select ?person where
{
?person a <http://dbpedia.org/class/yago/OlympicGoldMedalistsForSpain>
}
If you look at what dbpedia has, there is no class:
http://dbpedia.org/class/yago/OlympicGoldMedalists
but there is
http://dbpedia.org/class/yago/OlympicGoldMedalistsForItaly
and
http://dbpedia.org/class/yago/OlympicGoldMedalistsForFrance
and
http://dbpedia.org/class/yago/OlympicGoldMedalistsForGermany
so a work around could be:
select distinct ?person ?birthPlace where
{
?goldForCountry rdfs:subClassOf yago:Medalist110305062 .
?person a ?goldForCountry .
optional{
?person dbo:birthPlace ?birthPlace
}
filter (contains(str(?goldForCountry), "http://dbpedia.org/class/yago/OlympicGoldMedalistsFor"))
}
The birthPlace should be optional because there are 3994 persons that dbpedia doesn't have their birth place

Wikidata Sparql: how to get a list of actors that where never directed by a given person?

Let's say I want a list of actors that were never directed by Tim Burton among a list of popular movies.
I tried to do it with this steps:
Select all actors that Tim Burton ever directed (sub select)
Select a list of actors from a list of popular movies (by imdb ids)
Exclude all actors from the first selection in the second selection (NOT IN)
Here is a code I tried that do not works (the NOT IN fail, I don't know why):
SELECT DISTINCT ?actor ?actorLabel
WHERE {
?film wdt:P31 wd:Q11424
;wdt:P161 ?actor
;wdt:P345 ?imdbId .
{
SELECT ?excludeActors
WHERE {
?film wdt:P31 wd:Q11424
; wdt:P57 wd:Q56008
; wdt:P161 ?excludeActors .
}
} .
FILTER(?actor NOT IN (?excludeActors)) .
FILTER(?imdbId = "tt1077368" || ?imdbId = "tt0167260") .
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" }
}
Or follow this link
(there is a filter on Christopher Lee that you can remove [last one], it is used to highlight what I explain here:)
In this code I have two movies: Dark Shadows (directed by Tim Burton) and The Lord of the Rings 3. In this example Christopher Lee is present in both movies, which means he should be excluded since Tim Burton directed him in Dark Shadows.
You can see that he his in the list.
I really don't understand why the NOT IN fail with the sub select. I tried the sub Select request and I found Christopher Lee inside which means he should be excluded.
If I understood correctly, you want all actors that acted in the given movies, but have never acted in any movie directed by Tim Burton. I would use FILTER NOT EXISTS:
SELECT DISTINCT ?actor ?actorLabel
WHERE {
VALUES ?imdbId { "tt1077368" "tt0167260" }
?film wdt:P31 wd:Q11424
;wdt:P161 ?actor
;wdt:P345 ?imdbId .
FILTER NOT EXISTS {
[] wdt:P31 wd:Q11424
; wdt:P57 wd:Q56008
; wdt:P161 ?actor .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" }
}
LIMIT 100