按字符串长度排序 SPARQL 查询结果?

Order SPARQL query results by length of a string?

我正在尝试使用 DBpedia 中的术语自动完成用户在输入中写入的内容,类似于 this jsFiddle example。尝试在该 jsFiddle 的输入中写入 dog,您将在建议中看到 'Dog' 术语。

我有以下代码,问题是我得到的 10 项列表不包含 "Dog" 选项。因此,如果我可以按 ?concept(的字符串表示形式)的长度对列表进行排序,那么我就可以获得该术语。这可能吗?

SELECT DISTINCT ?concept
WHERE {
  ?concept a skos:Concept . 
  FILTER regex(str(?concept), "dog", "i")
}
ORDER BY ASC(?concept) LIMIT 10

So, if I could order the list by the lenght of the ?concept it is possible to get the term. But I can't find the right statement to do it. Is it possible?

听起来你在找 strlen.

order by strlen(str(?concept))

例如,

select distinct ?concept where {
  ?concept a skos:Concept . 
  filter regex(str(?concept), "dog", "i")
}
order by strlen(str(?concept))
limit 10

SPARQL results

就是说,如果您只是检查字符串成员资格,则不需要正则表达式的所有功能,使用 可能 更有效 contains and lcase 检查小写 ?concept 是否包含 "dog"过滤器喜欢:

filter contains(lcase(str(?concept)), "dog")

SPARQL 规范中的 table 内容有大量函数供您浏览。特别是,您需要查看 17.4 Function Definitions.

的小节