提供了电影名称的向量,我想知道它们查询维基数据的类型。
由于我是一个R用户,我最近发现了WikidataQueryServiceR,它的例子与我所寻找的完全相同:
library(WikidataQueryServiceR)
query_wikidata('SELECT DISTINCT
?genre ?genreLabel
WHERE {
?film wdt:P31 wd:Q11424.
?film rdfs:label "The Cabin in the Woods"@en.
?film wdt:P136 ?genre.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}')
## 5 rows were returned by WDQS不幸的是,这个查询使用静态文本,所以我想用向量替换The Cabin in the Woods。为了做到这一点,我尝试使用以下代码:
library(WikidataQueryServiceR)
example <- "The Cabin in the Woods" # Single string for testing purposes.
query_wikidata(paste('SELECT DISTINCT ?human ?humanLabel ?sex_or_gender ?sex_or_genderLabel WHERE {
?human wdt:P31 wd:Q5.
?human rdfs:label', example, '@en.
?human wdt:P21 ?sex_or_gender.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
OPTIONAL { ?human wdt:P2561 ?name. }
}', sep = ""))但这不像预期的那样有效,因为我得到了以下结果:
Error in FUN(X[[i]], ...) : Bad Request (HTTP 400).
我做错了什么?
发布于 2018-03-09 18:43:27
你试过输出你的SPARQL查询吗?
rdfs:label之后没有空间The Cabin in the Woods的引号在你的R代码中,而不是
?human rdfs:label', example, '@en.第7行应是:
?human rdfs:label "', example, '"@en.虽然query_wikidata()可以接受字符串向量,但我建议使用SPARQ1.1SPARQ1.1VALUES,以避免过多的请求。
library(WikidataQueryServiceR)
example <- c("John Lennon", "Paul McCartney")
values <- paste(sprintf("('%s'@en)", example), collapse=" ")
query <- paste(
'SELECT DISTINCT ?label ?human ?humanLabel ?sexLabel {
VALUES(?label) {', values,
'}
?human wdt:P31 wd:Q5.
?human rdfs:label ?label.
?human wdt:P21 ?sex.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}'
)
query_wikidata(query)对于大量的VALUES,您可能需要使用WikidataQueryServiceR的开发版本:似乎只有开发版本支持POST请求。
https://stackoverflow.com/questions/49193690
复制相似问题