代码之家  ›  专栏  ›  技术社区  ›  JD Long

与r中的最大距离相关的意外agrep()结果

  •  3
  • JD Long  · 技术社区  · 15 年前

    编辑: 在R的32位版本中发现了这个错误,在R版本2.9.2中修复了这个错误。


    今天这条微博是@leoniedu发给我的,我没有给他回复,所以我想我会把它贴在这里。

    我已经阅读了agrep()(模糊字符串匹配)的文档,似乎我没有完全理解max.distance参数。下面是一个例子:

    pattern <- "Staatssekretar im Bundeskanzleramt"
    x <- "Bundeskanzleramt"
    agrep(pattern,x,max.distance=18) 
    agrep(pattern,x,max.distance=19)
    

    这和我预料的完全一样。字符串之间有18个字符不同,所以我希望这是匹配的阈值。这就是让我困惑的地方:

    agrep(pattern,x,max.distance=30) 
    agrep(pattern,x,max.distance=31)
    agrep(pattern,x,max.distance=32) 
    agrep(pattern,x,max.distance=33)
    

    为什么是30和33场比赛,而不是31和32场?为了帮你省点钱,

    > nchar("Staatssekretar im Bundeskanzleramt")
    [1] 34
    > nchar("Bundeskanzleramt")
    [1] 16
    
    2 回复  |  直到 8 年前
        1
  •  2
  •   Eduardo Leoni    15 年前

    不久前我把这个贴在R列表上,并在R-bugs-list中报告为bug。我没有有用的回答,所以我抽搐着看这个bug是可复制的还是我遗漏了什么。JD龙能够复制它,并友好地将问题发布在这里。

    请注意,至少在r中,agrep是一个误称,因为它是 匹配正则表达式,而grep代表“全局搜索正则表达式和打印”。它不应该对比目标向量长的模式有问题。(我想!)

    在我的Linux服务器中,一切都很好,但在我的Mac和Windows机器中却不是这样。

    雨衣: 会话信息() R版本2.9.1(2009-06-26) i386-apple-darwin8.11.1版 场所: en-us.utf-8/en-us.utf-8/c/c/en-us.utf-8/en-us.utf-8

    Agrep(模式,X,最大距离=30) 〔1〕1

    Agrep(模式,X,最大距离=31) 整数(0) Agrep(模式,X,最大距离=32) 整数(0) Agrep(模式,X,最大距离=33) 〔1〕1

    Linux: R版本2.9.1(2009-06-26) x86_64-unknown-linux-gnu

    场所: lc_-ctype=en_-us.utf-8;lc_-numeric=c;lc_-time=en_-us.utf-8;lc_-collate=en_-us.utf-8;lc_-monetary=c;lc_-messages=en_-us.utf-8;lc_-paper=en_-us.utf-8;lc_-name=c;lc_-address=c;lc_-telephone=c;lc_-measurement=en_.utf-8;lc_-identification=c

    Agrep(模式,X,最大距离=30) 〔1〕1 Agrep(模式,X,最大距离=31) 〔1〕1 Agrep(模式,X,最大距离=32) 〔1〕1 Agrep(模式,X,最大距离=33) 〔1〕1

        2
  •  0
  •   Dirk is no longer here    15 年前

    我不确定你的例子是否有意义。对于basic grep(),pattern通常是一个简单的或正则表达式,x是一个向量,其元素与pattern匹配。它的图案是一条较长的线,我觉得它很奇怪。

    当我们使用grep而不是substr时,请考虑一下:

    R> grep("vo", c("foo","bar","baz"))   # vo is not in the vector
    integer(0)
    R> agrep("vo", c("foo","bar","baz"), value=TRUE) # but is close enough to foo
    [1] "foo"
    R> agrep("vo", c("foo","bar","baz"), value=TRUE, max.dist=0.25) # still foo
    [1] "foo"
    R> agrep("vo", c("foo","bar","baz"), value=TRUE, max.dist=0.75) # now all match
    [1] "foo" "bar" "baz"
    R>