我如何使用 SPARQL 查询从 Wikibooks 中获取书籍列表

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

如何使用 SPARQL 查询从 Wikibooks 获取图书列表,例如: 前缀 dbo:http://dbpedia.org/ontology/ 前缀 dba:http://dbpedia.org/ontology/
SELECT ?author ?name ?label ?text ?title ?isbn ?publisher ?literaryGenre ?pages WHERE
{?预订 dbo:Book.
?book dbo:author ?作者。 ?book dbo:numberOfPages ? 页。 ?book dbp:title ?书名。 ?book dba:isbn ?isbn。 ?book dba:publisher ?publisher.

FILTER regex(?title , "java") 。 }

不知道你知不知道Wikibooks不是维基百科,DBpedia是基于维基百科的?!

然后,为什么同一个命名空间 http://dbpedia.org/ontology/ 有两个前缀 dbodba?我真的建议了解您在做什么以及查询的作用,而不是从其他来源复制和粘贴。 SPARQL 和 RDF 教程可能会有所帮助,而且官方文档也很有用。

下一期,您 SELECT 变量 ?name?label?text?literaryGenreWHERE 部分。也不清楚您希望 ?text 得到什么。这本书的全文?!当然,这不会存在,请考虑版权。 ?name?title 之间有什么区别?我认为 dbp:title 在这里不合适 属性,请参阅

PREFIX dbo: <http://dbpedia.org/ontology/> 
PREFIX dbp: <http://dbpedia.org/property/>

SELECT count(*) WHERE {
?book a dbo:Book ;
      dbp:title ?title. 
}

仅 returns 19

我的建议:

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") . 
}

使用 Virtuoso 全文索引谓词更高效 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"'
}

因为一本书可能有多个作者。出版商,你可能会得到重复的行,这里 GROUP_BYGROUP_CONCAT 结合是要走的路(按书分组):

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