Filtering results of SPARQL queries - sparql

I want to retrieve all 20th century italian novelists' name.
I've written this:
SELECT ?label
WHERE{
?novelist a yago:ItalianNovelists.
?novelist rdfs:label ?label.
FILTER (langMatches(lang(?label), "EN"))
}
ORDER BY ?label
How can i filter the results?

There are several options. Here I'd suggest two:
Use the pattern ?novelist dct:subject dbc:20th-century_novelists, if you can rely on that classification.
SELECT ?label
WHERE{
?novelist a yago:ItalianNovelists;
rdfs:label ?label;
dct:subject dbc:20th-century_novelists.
FILTER (langMatches(lang(?label), "EN"))
}
ORDER BY ?label
Define birth range or a life-span range. Example birth range:
SELECT ?label
WHERE{
?novelist a yago:ItalianNovelists;
rdfs:label ?label;
dbo:birthDate ?date.
BIND (YEAR(?date) AS ?year)
FILTER (langMatches(lang(?label), "EN"))
FILTER (?year > 1882 && ?year < 1972)
}
ORDER BY ?label
With option 2 you will get more results but depending on the range, they might include novelists who haven't published anything in 20th century.
A third option would be the filter by the year of publishing. However, I wouldn't recommend it. First, it will give only results for those for whom such information is currently available in DBpedia, and that subset would most likely be smaller than the one from the first option. Second, depending on how you would define a 20 century novelist, the query results will omit those who wrote a novel in 20 century, which was published in 21.

The language tag is in lowercase, so just replace "EN: with "en". Also, I find the following to be just as effective as using langMatches
FILTER (lang(?label) = "en")

Related

Group Concat using sparql query

Can some one help me how to use group_concat in sparql query. When i am using in the database it is pulling out iri from the data. How to pull the the labels for the objects present in the database using group_concat.
You will usually have IRIs with a label. Now, to return labels only, a query like this will work:
SELECT ?item ?label
WHERE {
?item rdfs:label ?label
}
Note that rdfs:label is conventionally used for labels, but any name can be used - it depends on the data that you have.
Now, imagine we want to find a GROUP_CONCAT of children's names, grouped by mother.
First, the query for the children and labels looks like this:
SELECT ?mother ?child ?label
WHERE {
?mother :hasChild ?child .
?child rdfs:label ?label .
}
Notice that ?mother and ?child will be bound to IRIs here, and ?label to a string.
Now for the GROUP_CONCAT, you will just need a query like:
SELECT ?mother (GROUP_CONCAT(?label; SEPARATOR=", ") AS ?concat)
WHERE {
?mother :hasChild ?child .
?child rdfs:label ?label .
}
GROUP BY ?mother

i want to get the names of similar types using sparql queries from dbpedia

I need to find the names of similar types from DBpedia so I'm trying to figure out a query which can return me the names of entities which have same subject type in its dct:subject (example I want to find similar types of white house so i want to write a query for same . I'm considering the dct:subject to find them ). If there is any other approach please mention it
Previously I tried it for rdf:type but the result are not so good and some time it shows time out
I have done my problem by the query mentioned below and now i want to consider dct:subject instead of rdf:type
select distinct ?label ?resource count(distinct ?type) as ?score where {
values ?type { dbo:Thing dbo:Organization yago:WikicatIslam-relatedControversies yago:WikicatIslamistGroups yago:WikicatRussianFederalSecurityServiceDesignatedTerroristOrganizations yago:Abstraction100002137 yago:Act100030358 yago:Cabal108241798 yago:Group100031264 yago:Movement108464601 yago:PoliticalMovement108472335
}
?resource rdfs:label ?label ;
foaf:name ?name ;
a ?type .
FILTER (lang(?label) = 'en').
}
ORDER BY DESC(?score)

Wikidata query duplicates

Sorry if my english is bad, but I don't really have any place where I can ask this question in my native language.
I've been trying to create SPARQL query for Wikidata that should create a list of all horror fiction that was created in 1925-1950 years, names of authors and, if available, pictures:
SELECT DISTINCT ?item ?itemLabel ?author ?name ?creation ?picture
WHERE
{
?item wdt:P136 wd:Q193606 . # book
?item wdt:P50 ?author . # author
?item wdt:P577 ?creation .
?item wdt:P577 ?end .
?author rdfs:label ?name .
OPTIONAL{ ?item wdt:P18 ?picture }
FILTER (?creation >= "1925-01-01T00:00:00Z"^^xsd:dateTime) .
FILTER (?end <= "1950-12-31T23:59:59Z"^^xsd:dateTime) .
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
However, for some reason this query placing duplicates in the list. DISTINCT doesn't do much. After some time I figured out that the reason is "?item rdfs:label ?name .". If this line is removed, no duplicates are listed. But I need this line to show author name in the list!
Any ideas on how to fix this?
You don't need to use ?item rdfs:label ?name . as you already get items labels as ?itemLabel thank to SERVICE wikibase:label.
Then, you will get duplicate results for every items that have a SELECTed property with possibly multiple values: here, you are SELECTing authors (P50), which will create duplicates for every item with several authors.
The query is actually giving you distinct items. The problem is that some items have multiple rdfs:labels. You can see as an example the item:
SELECT *
WHERE
{
wd:Q2882840 rdfs:label ?label
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
And since there are multiple rdfs:label predicates for some items, they are showing up in separate rows.
You can aggregate your results according to the book title (the item's label) using the
group by
keyword.
Thus, every result will be a group which will show up once, and other fields which have different values, will be aggregated using the separator (in this case, a comma).
The fixed query:
SELECT DISTINCT ?item ?itemLabel
(group_concat(distinct ?author;separator=",") as ?author)
(group_concat(distinct ?name;separator=",") as ?name)
(group_concat(distinct ?creation;separator=",") as ?creation)
(group_concat(distinct ?picture;separator=",") as ?picture)
WHERE
{
?item wdt:P136 wd:Q193606 . # book
?item wdt:P50 ?author . # author
?item wdt:P577 ?creation .
?item wdt:P577 ?end .
?author rdfs:label ?name .
OPTIONAL{ ?item wdt:P18 ?picture }
FILTER (?creation >= "1925-01-01T00:00:00Z"^^xsd:dateTime) .
FILTER (?end <= "1950-12-31T23:59:59Z"^^xsd:dateTime) .
SERVICE wikibase:label
{
bd:serviceParam wikibase:language "en" .
}
}
group by ?item ?itemLabel

how to extract URI with sparql query

I need to extract the URIs of a specific resource from DBpedia.
Example, my specific resource is "France", I extracted its label but I don't know how to get its URI.
Here is my query so far :
SELECT ?label
WHERE{
res:France rdfs:label ?label .
FILTER (lang(?label) = 'fr') .}
Thanks for your help !
You need to filter your resource to be able to extract the URI. Try this:
SELECT distinct *
WHERE{
?uri rdfs:label ?label
filter (?uri=res:France && lang(?label) = 'fr')
}

SPARQL query to retrieve countries population density from DBPedia

Note: This question is different from SPARQL query to retrieve countries population from DBpedia. This question is about population density as understood by DBPedia itself.
How can I retrieve country population density from DBPedia?
I have tried the following, but Virtuoso endpoint returns an empty result set:
PREFIX p: <http://dbpedia.org/property/>
SELECT DISTINCT ?name ?populationDensity
WHERE {
?country a dbpedia-owl:Country .
?country rdfs:label ?name .
?country p:populationDensity ?populationDensity . }
Your current query returns an empty table because there is no ?country that fulfills your query that has the rdf:type dbpedia-owl:Country (represented by 'a'). Check that with this query.
To find the list of rdf:type's that the set of data that does use your populationDensity you could use this query. Following that lead you can just check all properties for Portugal and find that it does have populationDensity, but not the one you used.
This works:
PREFIX dbpedia-ont-PP: <http://dbpedia.org/ontology/PopulatedPlace/>
SELECT DISTINCT ?country ?populationDensity
WHERE {
?country a dbpedia-owl:Country .
?country dbpedia-ont-PP:populationDensity ?populationDensity .
}