SPARQL Query to get Movie detail - sparql

To get movie detail from linkedmdb, I used sparql query :
PREFIX mdb: <http://data.linkedmdb.org/resource/movie/film>
SELECT DISTINCT ?Title ?Genre ?Actor ?Country ?Director ?Year WHERE {
?film mdb:id ?uri .
?film dc:title ?Title .
?film movie:genre ?filmgenre.
?filmgenre movie:film_genre_name ?Genre .
?film movie:actor ?cast .
?cast movie:actor_name ?Actor .
?film movie:country ?Ctr .
?Ctr movie:country_name ?Country .
?film dc:date ?Year .
?film movie:director ?Drc .
?Drc movie:director_name ?Director
FILTER regex(?Title, "Kingdom of Heaven")
}
But SPARQL results shown Title same as counted Actor . How to combine the query so that Title is not repeated?
I've try using GROUP_CONCAT but not working in LinkedMDB Endpoint?
Could someone guide me?

Related

Problem with basics queries form dbpedia with sparql

Why can get results with this query:
SELECT *
WHERE {
?person rdfs:label "Kirk Douglas"#en;
dbo:birthPlace ?place. # With dbp: too
}
but not with another one:
SELECT *
WHERE {
?person rdfs:label "Kirk Douglas"#en;
dbo:starring ?film.
}
I'm following tags in https://dbpedia.org/page/Kirk_Douglas
Some tip to understand it.
Thx!
In dbr:Kirk_Douglas you can read:
dbo:birthPlace dbr:Amsterdam_(city),_New_York
...
is dbo:starring of dbr:The_Light_at_the_Edge_of_the_World
dbr:Cast_a_Giant_Shadow
dbr:Two-Fisted_Tales_(film)
...
where is dbo:starring of is a way of simulating an inverse property for dbo:starring.
Indeed, in dbo:starring you can read:
rdfs:domain dbo:Work
...
rdfs:range dbo:Actor
This means that you shouldn't build triples like ?actor dbo:starring ?work, while ?work dbo:starring ?actor is a valid triple.
So your query should be something like:
SELECT *
WHERE {
?person rdfs:label "Kirk Douglas"#en .
?film dbo:starring ?person .
}

SPARQL Query for all Books by George Orwell

I created this query to return all books that are notable works by George Orwell but it returns no result.
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/resource/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
SELECT ?name ?title where {
?person foaf:name ?name .
?title dbo:author ?person .
?title dbo:notableWork dbp:George_Orwell .
}
I cannot seem to figure out why there is no result.
I am running the query in http://dbpedia.org/snorql
Don't you have the triples about notable works in the wrong order?
Try rewriting based on this working query
SELECT *
WHERE {
:George_Orwell dbo:notableWork ?title
}
.
title
:Nineteen_Eighty-Four
:Animal_Farm
You can also bind :George_Orwell to a variable and ask more about that:
SELECT *
WHERE {
values ?author { :George_Orwell } .
?author rdfs:label ?l .
?title ?p ?author .
?title rdf:type dbo:Book .
filter (lang(?l) = "en")
}
and DESCRIBE things
describe :Animal_Farm

Aggregate properties

I'm developing my own Fuseki endpoint from some DBpedia data.
I'm in doubt on how to aggregate properties related to a single resource.
SELECT ?name ?website ?abstract ?genre ?image
WHERE{
VALUES ?s {<http://dbpedia.org/resource/Attack_Attack!>}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image } .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}
SPARQL endpoint: http://dbpedia.org/sparql/
This query returns 2 matching results. They are different just for the dbo:genre value. There is a way I can query the knowledge base and retrieving a single result with a list of genres?
#chrisis's query works well on the DBpedia SPARQL Endpoint, which is based on Virtuoso.
However, if you are using Jena Fuseki, you should use more conformant syntax:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT
?name
(SAMPLE(?website) AS ?sample_website)
(SAMPLE(?abstract) AS ?sample_abstract)
(SAMPLE(?image) AS ?sample_image)
(GROUP_CONCAT(?genre; separator=', ') AS ?genres)
WHERE {
VALUES (?s) {(<http://dbpedia.org/resource/Attack_Attack!>)}
?s foaf:name ?name ;
dbo:abstract ?abstract .
OPTIONAL { ?s dbo:genre ?genre } .
OPTIONAL { ?s dbp:website ?website } .
OPTIONAL { ?s dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
} GROUP BY ?name
The differences from the #chrisis's query are:
Since GROUP_CONCAT is an aggregation function, it might be used with GROUP BY only;
Since GROUP BY is used, all non-grouping variables should be aggregated (e.g. via SAMPLE);
GROUP_CONCAT syntax is slightly different.
In Fuseki, these AS in the projection are in fact superfluous: see this question and comments.
Yes, the GROUP_CONCAT() function is what you want.
SELECT ?name ?website ?abstract (GROUP_CONCAT(?genre,',') AS ?genres) ?image
WHERE{
<http://dbpedia.org/resource/Attack_Attack!> a dbo:Band ;
foaf:name ?name;
dbo:abstract ?abstract .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:genre ?genre } .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbp:website ?website} .
OPTIONAL{ <http://dbpedia.org/resource/Attack_Attack!> dbo:image ?image} .
FILTER LANGMATCHES(LANG(?abstract ), "en")
}

After (GROUP_CONCAT can't get single results

I am not very used to SPARQL, but I managed to write my own query for books and their genres, authors and their dates of birth and death at https://query.wikidata.org
SELECT
?title ?titleLabel
(GROUP_CONCAT(DISTINCT(?authorLabel); separator="//") as ?authors)
(GROUP_CONCAT(DISTINCT(?dateborn); separator="//") as ?date_born)
(GROUP_CONCAT(DISTINCT(?datedeath); separator="//") as ?date_death)
(GROUP_CONCAT(DISTINCT(?datepub); separator="//") as ?date_pub)
(GROUP_CONCAT(DISTINCT(?genre_titleLabel); separator="//") as ?genre_title)
WHERE
{
?title wdt:P31 wd:Q571 .
?title wdt:P50 ?author .
?title wdt:P577 ?date_pub .
?author wdt:P569 ?date_born .
?author wdt:P570 ?date_death .
?title wdt:P136 ?genre_title .
BIND(CONCAT(STR(DAY(?date_pub)),"-",STR(MONTH(?date_pub)),"-",STR(YEAR(?date_pub))) AS ?datepub )
BIND(CONCAT(STR(DAY(?date_born)),"-",STR(MONTH(?date_born)),"-",STR(YEAR(?date_born))) AS ?dateborn )
BIND(CONCAT(STR(DAY(?date_death)),"-",STR(MONTH(?date_death)),"-",STR(YEAR(?date_death))) AS ?datedeath )
FILTER (?date_born > "1900-01-01T00:00:00Z"^^xsd:dateTime) .
FILTER (?date_born < "1905-12-31T23:59:59Z"^^xsd:dateTime)
SERVICE wikibase:label {
bd:serviceParam wikibase:language "en" .
?title rdfs:label ?titleLabel .
?author rdfs:label ?authorLabel .
?date_pub rdfs:label ?date_pubLabel .
?date_born rdfs:label ?date_bornLabel .
?date_death rdfs:label ?date_deathLabel .
?genre_title rdfs:label ?genre_titleLabel .
}
}
GROUP BY ?title ?titleLabel
It works and I get all the genres in the same field. The only way I could get this was with
(GROUP_CONCAT(DISTINCT(
for genres and dates. But If you loook in the results there are records with more than one date. And I want to get only the first date, and not all the stored dates in the database for this record, but I can't.
For example, If I ask for
?dateborn
instead of
(GROUP_CONCAT(DISTINCT(?dateborn); separator="//") as ?date_born)
I get an error. Do you know where is my mistake?
Greets
N.

How to use Sparql Contains to match similar String?

I'm trying to grab some definition in dbpedia inside my thesaurus.
Although can find country that have a label that match my country, i don't get all of them. So i try to match similar label with contains but it does not work.
Any idea why.
SELECT distinct ?idbcountry ?label ?labelDb ?def
WHERE {
?idbcountry a skos:Concept .
?idbcountry rdfs:label ?label .
?idbcountry skos:inScheme iadb:IdBCountries .
FILTER(lang(?label) = "en")
Service <http://dbpedia.org/sparql> {
?s a <http://dbpedia.org/ontology/Country> .
?s rdfs:label ?labelDb .
FILTER(CONTAINS (?labelDb, ?label)).
?s rdfs:comment ?def .
FILTER(lang(?def) = "en") .
FILTER(lang(?labelDb) = "en") .
}}
The exact matching query that works is as follows:
SELECT distinct ?idbcountry ?label ?def
WHERE {
?idbcountry a skos:Concept .
?idbcountry rdfs:label ?label .
?idbcountry skos:inScheme iadb:IdBCountries .
FILTER(lang(?label) = "en")
Service <http://dbpedia.org/sparql> {
?s a <http://dbpedia.org/ontology/Country> .
?s rdfs:label ?label .
?s rdfs:comment ?def
FILTER(lang(?def) = "en")
}
}
EDIT1
Data Samples:
<http://thesaurus.iadb.org/publicthesauri/10157002136735779158437>
rdf:type skos:Concept ;
dct:created "2015-03-27T16:43:48.052-04:00"^^xsd:dateTime ;
rdfs:label "BO"#en ;
rdfs:label "Bolivia"#en ;
rdfs:label "Bolivia"#es ;
rdfs:label "Bolivie"#fr ;
rdfs:label "Bolívia"#pt ;
skos:altLabel "BO"#en ;
skos:definition "Bolivia (/bəˈlɪviə/, Spanish: [boˈliβja], Quechua: Buliwya, Aymara: Wuliwya), officially known as the Plurinational State of Bolivia (Spanish: Estado Plurinacional de Bolivia locally: [esˈtaðo pluɾinasjoˈnal de βoˈliβja]), is a landlocked country located in western-central South America."#en ;
skos:inScheme :IdBCountries ;
skos:prefLabel "Bolivia"#en ;
skos:prefLabel "Bolivia"#es ;
skos:prefLabel "Bolivie"#fr ;
skos:prefLabel "Bolívia"#pt ;
skos:topConceptOf :IdBCountries ;
<http://xmlns.com/foaf/0.1/focus> <http://dbpedia.org/resource/Bolivia> ;
Without seeing your data, we can't know why your query isn't working. However, using contains is pretty straightforward. It's just a matter of contains(string,substring). As Jeen said, we can't reproduce your problem without knowing what your data looks like, but here's an example of contains in action:
select distinct ?country ?label {
?country a dbpedia-owl:Country ; #-- select countries
rdfs:label ?label . #-- and get labels
filter langMatches(lang(?label),"en") #-- but only English labels
filter contains(?label,"land") #-- containing "land"
}
SPARQL results