在 R 中使用 agrep() 进行模糊匹配的替代方法

Alternative approach to using agrep() for fuzzy matching in R

我有一个很大的行政数据文件,大约有 100 万条记录。个人可以在此数据集中多次表示。大约一半的记录具有将记录映射到个人的识别代码;对于没有的一半,我需要模糊匹配名称以标记可能属于同一个人的记录。

通过查看带有识别码的记录,我创建了一个列表,列出了同一个人的姓名记录中出现的差异:

考虑到我所追求的匹配类型,是否有比使用 agrep()/levenshtein 的距离更好的方法,并且可以在 R 中轻松实现?

编辑:R 中的 agrep() 不能很好地解决这个问题——因为大量的插入和替换,我需要考虑到名称的不同记录方式,很多错误的比赛被抛出。

我会多次通过。

"Jon .* Snow" - 中间名

"Jon .*Snow" - 第二个姓氏

昵称需要从长格式到短格式的映射字典,没有正则表达式可以处理他的昵称。

"Snow Jon" - 逆转(废话)

agrep 将处理较小的拼写错误。

您可能还想将您的名字标记为名字、中间和最后。

合成器包 (https://cran.r-project.org/web/packages/synthesisr/index.html) 可能会有帮助。它使用 R 代码模仿 fuzzywuzzy Python 包和 fuzzywuzzyR 中的一些模糊匹配功能。从 fuzzywuzzy 中有类似的不同指标;分数越低意味着相似度越高。这些方法可以通过不同的方式访问,如下所示。

具体来说,在这种情况下,“标记”函数可能会有用,因为字符串由空格标记,然后按字母顺序排列以处理反转等情况。

library(synthesisr)

fuzz_m_ratio("this is a test", "this is a test!")
fuzzdist("this is a test", "this is a test!", method = "fuzz_m_ratio")

fuzz_partial_ratio("this is a test", "this is a test!")
fuzzdist("this is a test", "this is a test!", method = "fuzz_partial_ratio")

fuzz_token_sort_ratio("this is a test", "this is a test!")
fuzzdist("this is a test", "this is a test!", method = "fuzz_token_sort_ratio")

fuzz_token_set_ratio("this is a test", "this is a test!")
fuzzdist("this is a test", "this is a test!", method = "fuzz_token_set_ratio")