如何计算两个字符串之间的相等字数?

时间:2014-11-20 20:55:49

标签: xpath xquery

如何计算出现在两个字符串中的单词数?

我在想这样的事情

      let $nequalwords := count($item[text() eq $speech])

这样做的最佳方式是什么?

我想与两个for逐字逐句比较,但我不知道是否有更好的方法来做到这一点。

2 个答案:

答案 0 :(得分:5)

如何在白色空间上分割字符串,以便最终得到单词,然后创建字符串序列并删除那些不相同的字符串,即那些出现在两个字符串中的字符串,然后从计数中减去它在所有单词中你知道两个字符串中出现了多少单词。例如:

  let $distinct-words1 := distinct-values(tokenize($string1, "\s+"))
  let $distinct-words2 := distinct-values(tokenize($string2, "\s+"))
  let $all-words := ($distinct-words1, $distinct-words2)
  return
    count($all-words) - count(distinct-values($all-words))

答案 1 :(得分:2)

怎么样

count(tokenize($string1, "\s+")[. = tokenize($string2, "\s+")])

这是第一个字符串中也出现在第二个字符串中的单词数。哪个可能是你真正想要的,也可能不是。例如,如果两个字符串是“越多越好”和“春天的仪式”,答案将是2。

相关问题