2

我知道 q-gram 距离是两个字符串的 q-gram 向量之间的绝对差之和。但是当其中一个字符串比所选的 q 短时,我看到了一些奇怪的行为。

所以对于这两个字符串,虽然qgrams函数是正确的:

> qgrams("a", "the cat sat on the mat", q = 2)
   th he t  sa on n  ma e   c ca at  s  t  o  m
V1  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
V2  2  2  2  1  1  1  1  2  1  1  3  1  1  1  1

stringdist函数返回:

> stringdist("a", "the cat sat on the mat", q = 2, method = "qgram")
[1] Inf

而不是返回:

> sum(qgrams("a", "the cat sat on the mat", q = 2)[2,])
[1] 21

我错过了什么还是这是一个错误?谢谢。

stringdist 版本:0.9.4.1 和 0.9.4.2

4

1 回答 1

2

当 q 大于字符串长度时,当前stringdist::stringdist假定未定义 ( ) 距离。Inf

我当时的推理可能是,如果 q 小于输入字符串长度,则从 {the set of all strings over an alphabet Sigma} 到 {positive integer vectors of length |Sigma|^q} 的映射没有明确的定义。这也是我在stringdist 论文中写下来的方式。

qgrams将这种情况映射到 0 向量,这确实是不一致的。

如果我采用Ukkonen (1992)论文中的定义,映射到 0 向量确实是正确的选择,这意味着stringdist.

会修复。

于 2016-10-19T09:52:00.813 回答