为什么 R stringdist return Inf in q-gram distance with one string shorter than q?

Why does R stringdist return Inf in q-gram distance with one string shorter than q?

我了解到 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函数returns:

> 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

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

我当时的推理可能是,如果 q 小于输入字符串长度。这也是我在 stringdist paper.

中写下的方式

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

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

中存在错误

会修复。