使用 WikidataQueryServiceR 查询维基数据的字符串向量

Querying a vector of strings to Wikidata using WikidataQueryServiceR

提供了一个电影名字的向量,我想知道他们的类型查询维基数据。

由于我是 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).

我做错了什么?

您尝试过输出 SPARQL 查询吗? —

  • rdfs:label
  • 后没有space
  • The Cabin in the Woods
  • 两边没有引号

在您的 R 代码中,而不是

  ?human rdfs:label', example, '@en.

第 7 行应该是:

  ?human rdfs:label "', example, '"@en.

尽管 query_wikidata() 可以接受字符串向量,但我建议改用 SPARQL 1.1 VALUES,以避免请求太多。

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请求。