我正在尝试使用 SPARQL 和 Wikidata 查询服务从字符串中识别最长的三个子字符串,然后排名
- 字符串中的子字符串的长度
- 由任何最长子字符串的长度组成的字符串。
我设法从字符串中识别出第一个和第二个子字符串,当然可以创建类似的附加行来解决问题,但这看起来很丑陋且效率低下,所以我想知道这里是否有人知道更好的方法。
这是代码的简化版本,尽管我留下了一些辅助变量,用于跟踪过程中的进度。你可以在这里试试。
对此评论的澄清:如果有必要将此查询视为子查询并为其提供来自另一个子查询的结果,这对我来说很好。要了解我想到的使用类型,请参阅此演示。
SELECT * WHERE {
{
VALUES (?title) {
("What are the longest three words in this string?")
("A really complicated title")
("OneWordTitleInCamelCase")
("Thanks for your help!")
}
}
BIND(STRLEN(REPLACE(?title, " ", "")) AS ?titlelength)
BIND(STRBEFORE(?title, " ") AS ?substring1)
BIND(STRLEN(REPLACE(?substring1, " ", "")) AS ?substring1length)
BIND(STRAFTER(?title, " ") AS ?postfix)
BIND(STRLEN(REPLACE(?postfix, " ", "")) AS ?postfixlength)
BIND(STRBEFORE(?postfix, " ") AS ?substring2)
BIND(STRLEN(REPLACE(?substring2, " ", "")) AS ?substring2length)
}
ORDER BY DESC(?substring1length)
预期成绩:
longsubstring substringlength
OneWordTitleInCamelCase 23
complicated 11
longest 7
really 6
string 6
Thanks 6
title 5
three 5
your 4
help 4
实际结果:
title titlelength substring1 substring1length postfix postfixlength substring2 substring2length
Thanks for your help! 18 Thanks 6 for your help! 12 for 3
What are the longest three words in this string? 40 What 4 are the longest three words in this string? 36 are 3
A really complicated title 23 A 1 really complicated title 22 really 6
OneWordTitleInCamelCase 23 0 0 0