I want to retrieve the entities of the classes along with some information - if exists (e.g. label, comment, image)
I run the following query:
PREFIX schema: <http://schema.org/>
SELECT DISTINCT ?entity ?label ?comment ?image
WHERE {
?entity a ?class .
OPTIONAL {?entity rdfs:label ?label } .
OPTIONAL {?entity schema:image ?image} .
OPTIONAL {?entity rdfs:comment ?comment} .
OPTIONAL {?entity rdfs:label ?label} .
FILTER (langMatches(lang(?comment), "EN") && langMatches(lang(?label), "EN")) .
}
Although, some triples contain this extra information I do not receive them
(even if I re-write and include all the optional statements in one statement)
How could I compose such a query?
Related
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 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)
I have to do a sparql query to the dbpedia endpoint which needs to:
Find all the entities containing "vienna" in the label and "city" in the abstract
Filter them keeping only the ones that have at least one dbo rdf:type
Sort the results by count of dbo types (e.g. if an entity has 5 dbo rdf:type it has to be shown before entities with 4 dbo rdf:type)
I did several attempts, the closest to the result is:
select distinct (str(?s) as ?s) count(?t) as ?total where {{ ?s rdfs:label "vienna"#en. ?s rdf:type ?t.}
UNION { ?s rdfs:label ?l. ?s rdf:type ?t . ?l <bif:contains> '("vienna")'
. FILTER EXISTS { ?s dbo:abstract ?cc. ?cc <bif:contains> '("city")'. FILTER(lang(?cc) = "en").}}
FILTER (!strstarts(str(?s), str("http://dbpedia.org/resource/Category:")))
. FILTER (!strstarts(str(?s), str("http://dbpedia.org/property/")))
. FILTER (!strstarts(str(?s), str("http://dbpedia.org/ontology/")))
. FILTER (strstarts(str(?t), str("http://dbpedia.org/ontology/"))).}
LIMIT 50
Which will (wrongly) count the rdf:type before actually filtering it. I don't want to count rdf:type that are not dbo (ontology).
The idea is to use a subquery in which you search for the entities and to do the counting in the outer query:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?s (count(*) AS ?cnt)
WHERE
{ { SELECT DISTINCT ?s
WHERE
{ ?s rdfs:label ?l .
?l <bif:contains> '"vienna"'
FILTER langMatches(lang(?l), "en")
FILTER EXISTS { ?s dbo:abstract ?cc .
?cc <bif:contains> '"city"'
FILTER langMatches(lang(?cc), "en")
}
?s rdf:type ?t
FILTER ( ! strstarts(str(?s), str("http://dbpedia.org/resource/Category:")) )
FILTER ( ! strstarts(str(?s), str("http://dbpedia.org/property/")) )
FILTER ( ! strstarts(str(?s), str(dbo:)) )
FILTER strstarts(str(?t), str(dbo:))
}
}
?s ?p ?o
FILTER strstarts(str(?p), str(dbo:))
}
GROUP BY ?s
ORDER BY DESC(?cnt)
I'm trying to get list of movies in a specific year (just say 1990) with each movie and it's attributes. I tried different ways to get SPARQL but couldn't. How do I group genres into single column?
The format I would want is with some of the properties from here
http://schema.org/Movie
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?q ?film_title (GROUP_CONCAT(DISTINCT(?genreID); separator=", ") as ?genre) WHERE {
?q wdt:P31 wd:Q11424.
?q rdfs:label ?film_title filter (lang(?film_title) = "en").
?q wdt:P136 ?genreID.
?genreID rdfs:label ?genre.
?q wdt:P161 ?actorID.
?actorID rdfs:label ?actor filter (lang(?actor) = "en").
} LIMIT 100
Usage of aggregate functions usually need to build the group of things/rows on which you want to apply the function, i.e. a GROUP_BY on the projection variables that do not denote the result of an aggregate function:
SELECT ?q ?film_title (GROUP_CONCAT(DISTINCT(?genreID); separator=", ") as ?genres) WHERE {
?q wdt:P31 wd:Q11424 .
?q rdfs:label ?film_title .
FILTER (lang(?film_title) = "en")
?q wdt:P136 ?genreID .
?genreID rdfs:label ?genre .
?q wdt:P161 ?actorID .
?actorID rdfs:label ?actor
FILTER (lang(?actor) = "en")
}
GROUP BY ?q ?film_title
LIMIT 100
Some comments:
it's not clear whether you want to get the genreID or the label of the genre, for the latter indeed you have to use the variable ?genre in the GROUP_CONCAT function: GROUP_CONCAT(DISTINCT(?genre); separator=", ")
it's not allowed to re-assign a variable name that has already been used in the query, that's why I changed the name to ?genres
don't put a comma after a FILTER, I don't know why you did so
you don't select the actor, thus, it remains unclear why you use the last triple pattern + FILTER in your query
your query is quite expensive, thus, its execution might timeout
I'm trying to use a SPARQL query to retrieve information about a DBpedia resource (a Person). I'd like to use the same query to retrieve data about any Person by parameterizing the resource URI. Since some attributes may not exist for a particular resource, I'm making use of the OPTIONAL statement. Here is my query:
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbpprop: <http://dbpedia.org/property/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT DISTINCT ?label ?abstract ?placeOfBirth
?birthPlace ?birthDate ?deathDate ?page ?thumbnail
WHERE {
<http://dbpedia.org/resource/Neil_Simon> rdfs:label ?label ;
dbo:abstract ?abstract ;
foaf:page ?page .
OPTIONAL {
<http://dbpedia.org/resource/Neil_Simon> dbpprop:placeOfBirth ?placeOfBirth ;
dbpprop:birthPlace ?birthPlace ;
dbo:birthDate ?birthDate ;
dbo:deathdate ?deathDate ;
dbo:thumbnail ?thumbnail .
}
FILTER (LANG(?label) = 'en')
FILTER (LANG(?abstract) = 'en')
}
LIMIT 1
I've left everything except label, abstract and page in OPTIONAL, since if I use the same query for another person, they may not have those properties. The problem is, none of those optional attributes are showing up in the results. In Neil Simon's case, you can see that there are values for birthDate, birthPlace and thumbnail: http://dbpedia.org/resource/Neil_Simon. However, those values don't show up when I run the query: DBpedia SPARQL query. What am I doing wrong, and how can I optionally retrieve those properties?
Although you have used an OPTIONAL construct the map pattern itself needs all the attributes within to match. So only if you have birthPlace, birthDate, deathDate and thumbnail the inner optional construct is satisfied
I would suggest breaking the OPTIONAL construct up into multiple OPTIONAL constructs.