我正在尝试使用SPARQL和Wikidata查询服务从字符串中识别出最长的三个子字符串,然后对其进行排名
我设法从字符串中识别出第一个和第二个子字符串,当然可以创建类似的附加行来解决该问题,但这看起来很丑陋且效率低下,所以我想知道这里是否有人知道更好的获取方法在那里。
这是代码的简化版本,尽管我留下了一些辅助变量,因为我正在使用这些变量来跟踪途中的进度。您可以here尝试一下。
对this comment的响应的澄清:如果有必要将此查询视为子查询,并将其与另一个子查询的结果一起提供,这对我来说很好。要了解我要记住的用途,请参阅this demo。
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