all 是始终适用的上限,无论其他max.distance 控件(cost 除外)如何。默认为 10%。
# one characters can change
agrep(pattern = "abcdXX", x = "abcdef", value = TRUE,
max.distance = list(sub = 2, ins = 0, del = 0, all = 0.1))
# character(0)
# two characters can change
agrep(pattern = "abcdXX", x = "abcdef", value = TRUE,
max.distance = list(sub = 2, ins = 0, del = 0, all = 0.2))
# [1] "abcdef"
# one character can change
agrep(pattern = "abcdXX", x = "abcdef", value = TRUE,
max.distance = list(sub = 1, ins = 1, del = 0, all = 0.1))
# character(0)
# two characters can change
agrep(pattern = "abcdXX", x = "abcdef", value = TRUE,
max.distance = list(sub = 1, ins = 1, del = 0, all = 0.2))
# [1] "abcdef"
设置all 的小数模式在 1 处切换到整数模式有一点问题。
# 8 insertions allowed
agrep(pattern = "abcdXXef", x = "abcdef", value = TRUE,
max.distance = list(sub = 0, ins = 2, del = 0, all = 1 - 1e-9))
# [1] "abcdef"
# 1 insertion allowed
agrep(pattern = "abcdXXef", x = "abcdef", value = TRUE,
max.distance = list(sub = 0, ins = 2, del = 0, all = 1))
# character(0)
当您通过将 all 设置为小于 1 来抑制 all 时,将应用距离模式的限制。
# two substitutions allowed
agrep(pattern = "abcdXX",
x = c("abcdef", "abcXdef", "abcefg"), value = TRUE,
max.distance = list(sub = 2, ins = 0, del = 0, all = 1 - 1e-9))
# [1] "abcdef"
设置成本的目的是让您在变异空间中以不同的速率在不同的方向上移动。这将取决于您的用例。例如,某些语言方言可能更可能添加字母。您可能会选择让删除花费两次插入。默认情况下,当costs = NULL,即costs = c(ins = 1, del = 1, sub = 1)时,所有的权重相同。
编辑:关于您关于为什么某些模式匹配而其他模式不匹配的评论,10% 是指模式中的字符数,四舍五入。
agrep(pattern = "01234567XX89", x = "0123456789", value = TRUE,
max.distance = list(sub = 0, ins = 2, del = 0))
# [1] "0123456789"
agrep(pattern = "01234567XX", x = "0123456789", value = TRUE,
max.distance = list(sub = 2, ins = 0, del = 0))
# character(0)
num_mutations <- nchar(c("01234567XX89", "01234567XX")) * 0.1
num_mutations
# [1] 1.2 1.0
ceiling(num_mutations)
[1] 2 1
第二个模式只有 10 个字符,所以只允许替换一个。