首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WikidataQueryServiceR查询字符串向量到维基数据

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

Stack Overflow用户
提问于 2018-03-09 12:19:03
回答 1查看 170关注 0票数 1

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

由于我是一个R用户,我最近发现了WikidataQueryServiceR,它的例子与我所寻找的完全相同:

代码语言:javascript
复制
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。为了做到这一点,我尝试使用以下代码:

代码语言:javascript
复制
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).

我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-09 18:43:27

你试过输出你的SPARQL查询吗?

  • rdfs:label之后没有空间
  • 没有关于The Cabin in the Woods的引号

在你的R代码中,而不是

代码语言:javascript
复制
  ?human rdfs:label', example, '@en.

第7行应是:

代码语言:javascript
复制
  ?human rdfs:label "', example, '"@en.

虽然query_wikidata()可以接受字符串向量,但我建议使用SPARQ1.1SPARQ1.1VALUES,以避免过多的请求。

代码语言:javascript
复制
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请求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49193690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档