我正在使用下面的代码从FOAF链接数据中查询性别。当我搜索"鲍伊"而不是"大卫鲍伊"时,该功能有效。
sparql_foaf <- function(term) {
endpoint <- "http://live.dbpedia.org/sparql"
prefix <- c("db","http://dbpedia.org/resource/",
"rdfs","http://www.w3.org/2000/01/rdf-schema#",
"foaf","http://xmlns.com/foaf/0.1/")
query <- paste0("
SELECT str(?lbl) as ?names_r, str(?gender) as ?gender WHERE {
?sub a foaf:Person .
?sub rdfs:label ?lbl .
FILTER regex(?lbl, 'Bowie')
FILTER(langMatches(lang(?lbl), 'en'))
OPTIONAL {?sub foaf:gender ?gender}
}
LIMIT 1")
SPARQL(endpoint,query,ns=prefix)$results
}
问题是,鲍伊工作
FILTER regex(?lbl, 'Bowie')
而大卫鲍伊没有
FILTER regex(?lbl, 'David Bowie')
这个问题可能是我在SPARQL库中忽略的简单问题。但是,查询在查询环境中确实按预期工作:https://api.triplydb.com/s/r8cBeIuo
我是否缺少SPARQL R库中空格的某种字符封闭技术?
使用 ASKW 注释中的信息,从而删除"人员要求",我们看到以下结果:
sparql_foaf <- function(term) {
endpoint <- "http://live.dbpedia.org/sparql"
prefix <- c("db","http://dbpedia.org/resource/",
"rdfs","http://www.w3.org/2000/01/rdf-schema#",
"foaf","http://xmlns.com/foaf/0.1/")
query <- paste0("
SELECT str(?lbl) as ?names_r, str(?gender) as ?gender WHERE {
?sub rdfs:label ?lbl .
FILTER regex(?lbl, 'Bowie')
FILTER(langMatches(lang(?lbl), 'en'))
OPTIONAL {?sub foaf:gender ?gender}
}
LIMIT 100")
SPARQL(endpoint,query,ns=prefix)$results
}
grep("David", sparql_foaf (1)[[1]] )
[1] 4 5 6 9 10 11 12 33 40 41 42 43 46 47 48 50 55 59 63 69 71 73 74 75 78 83
[27] 85 91 96 97
和:
sparql_foaf (1)[[1]][4]
[1] "Albums produced by David Bowie"