Query DBPedia to get all books of an author - sparql

SELECT ?book
WHERE {
?book a dbpedia-owl:Book .
?book WHAT_PREDICATE 'Agatha Christie'
}
I want to know what predicate to use in the query. I haven't found any list with books or writers predicates on DBPedia.

SELECT ?book
WHERE {
?book a dbpedia-owl:Book .
?book dbpprop:author ?author .
?author dbpprop:name ?name
FILTER regex(?name, "Agatha Christie", "i")
}

Related

how can i get list of book from Wikibooks with SPARQL query

how can i get list of book from Wikibooks with SPARQL query fo example :
PREFIX dbo:http://dbpedia.org/ontology/
PREFIX dba:http://dbpedia.org/ontology/
SELECT ?author ?name ?label ?text ?title ?isbn ?publisher ?literaryGenre ?pages WHERE
{?book a dbo:Book.
?book dbo:author ?author.
?book dbo:numberOfPages ?pages.
?book dbp:title ?title.
?book dba:isbn ?isbn.
?book dba:publisher ?publisher.
FILTER regex(?title , "java") .
}
I'm wondering whether you know that Wikibooks is not Wikipedia and DBpedia is based on Wikipedia?!
And then, why do you have two prefixes dbo and dba for the same namespace http://dbpedia.org/ontology/ ? I really suggest to understand what you're doing and what the query does instead of copy and paste from some other sources. SPARQL and RDF tutorials might help, and also the official documentation is useful.
Next issue, you SELECT variables ?name, ?label, ?text and ?literaryGenre which are not bound in a triple pattern in the WHERE part. It's also not clear what you expect to get for ?text. The whole text of the book?! For sure, this won't exist, think about copyrights.
And what would be the difference between ?name and ?title? I don't think that dbp:title is the appropriate property here, see
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT count(*) WHERE {
?book a dbo:Book ;
dbp:title ?title.
}
which returns 19 only.
My suggestion:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {
?book a dbo:Book .
?book dbo:author ?author .
OPTIONAL { ?book dbo:numberOfPages ?pages }
OPTIONAL { ?book dbo:isbn ?isbn }
OPTIONAL { ?book dbo:publisher ?publisher }
# get the English title
?book rdfs:label ?name.
FILTER(LANGMATCHES(LANG(?name), 'en'))
# get an English description, but not the text
?book rdfs:comment ?text .
FILTER(LANGMATCHES(LANG(?text), 'en'))
# filter for books whose title contains "java"
FILTER regex(str(?name) , "java", "i") .
}
More efficient using the Virtuoso fulltext index predicate bif:contains:
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT * WHERE {
?book a dbo:Book .
?book dbo:author ?author .
OPTIONAL { ?book dbo:numberOfPages ?pages }
OPTIONAL { ?book dbo:isbn ?isbn }
OPTIONAL { ?book dbo:publisher ?publisher }
# get the English title
?book rdfs:label ?name.
FILTER(LANGMATCHES(LANG(?name), 'en'))
# get an English description, but not the text
?book rdfs:comment ?text .
FILTER(LANGMATCHES(LANG(?text), 'en'))
# filter for books whose title contains "java"
?name bif:contains '"java"'
}
As a book might have multiple authors resp. publisher you might get duplicate rows, here GROUP_BY in combination with GROUP_CONCAT is the way to go (grouped by book):
PREFIX dbo: <http://dbpedia.org/ontology/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?book (group_concat(DISTINCT ?author; separator = ", ") as ?authors) (group_concat(DISTINCT ?publisher; separator = ", ") as ?publishers) (sample(?pages) as ?numPages) (sample(?isbn_tmp) as ?isbn) WHERE {
?book a dbo:Book .
?book dbo:author ?author .
OPTIONAL { ?book dbo:numberOfPages ?pages }
OPTIONAL { ?book dbo:isbn ?isbn_tmp }
OPTIONAL { ?book dbo:publisher ?publisher }
# get the English title
?book rdfs:label ?name.
FILTER(LANGMATCHES(LANG(?name), 'en'))
# get an English description, but not the text
?book rdfs:comment ?text .
FILTER(LANGMATCHES(LANG(?text), 'en'))
# filter for books whose title contains "java"
?name bif:contains '"java"'
}
GROUP BY ?book

How to get the movies which are based on english novels using SPARQL in DBPEDIA

Using SPARQL, I am trying the get the list of all english novels and their properties.
I would also like to find if a movie was taken based on that novel and get the movie name and its director, If a movie relationship exists.
Code:
SELECT ?movie ?director ?book ?author ?publisher ?illustrator
WHERE {
?movie dcterms:subject <http://dbpedia.org/resource/Category:films> ;
dbpedia-owl:basedOn ?book .
?movie dbp:director ?director .
?book a dbpedia-owl:Book .
?book dbp:author ?author .
?book dbp:publisher ?publisher .
?book dbp:illustrator ?illustrator .
}
limit 200
You can get a lot of correct results, if you modify your query like this...
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>
PREFIX dbp: <http://dbpedia.org/property/>
SELECT ?book ?author ?movie ?director ?publisher ?illustrator
WHERE {
?book a dbpedia-owl:Book .
OPTIONAL {?book dbp:author ?author .}
OPTIONAL {?book dbp:publisher ?publisher .}
OPTIONAL {?book dbp:illustrator ?illustrator .}
OPTIONAL {?book ^dbpedia-owl:basedOn ?movie . ?movie a dbpedia-owl:Film }
OPTIONAL {?movie dbp:director ?director .}
}
LIMIT 200
...but keep in mind that there are many movies that are not classified as dbpedia-owl:Film. Then of course you make a union with a few other popular classifications but that would still not guarantee there there won't be a movie based on a book, which will not be omitted.
And by the way what do you call "English novels" -- those written originally in English or those by English authors?

How to query DBPEDIA SPARQL to include the desired values while using LIMIT

I am trying to populate my local ontology with 10 random books. I would like to include 2 known books in the list for testing purpose.i.e out of the 10 I want to specify the names of 2 books and the remaining 8 can be random.
I do not know how to combine FILTER and LIMIT together.
I want the 2 books in the query along with 8 random books.
FILTER (str(?name) IN ("Panther in the Basement", "Endless Night"))
My code
SELECT ?book ?date
WHERE {
?book rdf:type dbo:Book .
?book foaf:name ?name .
?book dbp:releaseDate ?date .
}
limit 10
It's not clear why you want to do this, but it can be done by a more complex query which is the UNION of
the data of 8 other books
the data for the two given books
SELECT DISTINCT ?book ?date
WHERE
{ { SELECT DISTINCT ?book ?date
WHERE
{ ?book rdf:type dbo:Book ;
foaf:name ?name ;
dbp:releaseDate ?date
FILTER ( ?book NOT IN (dbr:Panther_in_the_Basement, dbr:Endless_Night) )
}
LIMIT 8
}
UNION
{ VALUES ?book { dbr:Panther_in_the_Basement dbr:Endless_Night }
?book rdf:type dbo:Book ;
foaf:name ?name ;
dbp:releaseDate ?date
}
}
Note, since the DBpedia dataset is not that clean and has some books with multiple values for releaseDate this query might return duplicate books. To overcome this, you have to use GROUP BY + SAMPLE (or GROUP_CONCAT)
Actually, I'd think that a more compact version of this query should also work, but it doesn't return any results on DBpedia:
SELECT DISTINCT ?book ?date
WHERE
{ ?book foaf:name ?name ;
dbp:releaseDate ?date
{ { SELECT ?book
WHERE
{ ?book rdf:type dbo:Book }
LIMIT 8
}
}
UNION
{ VALUES ?book { dbr:Panther_in_the_Basement dbr:Endless_Night } }
}

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.

SPARQL Query to get Movie detail

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?