【发布时间】:2015-10-28 19:19:40
【问题描述】:
我有一个句子我只想用数字替换字符串的一部分。如果我们有一个完全匹配的 gsub 函数就可以完美地工作。
gsub('great thing', 5555 ,c('hey this is a great thing'))
gsub('good rabbit', 5555 ,c('hey this is a good rabbit in the field'))
但现在我遇到了以下问题。如果字符串的一部分有错误,如何将模糊匹配函数应用于字符串?
gsub('great thing', 5555 ,c('hey this is a graet thing'))
gsub('good rabbit', 5555 ,c('hey this is a goood rabit in the field'))
算法应该找出“great thing”和“graet thing”或“good rabbit”和“good rabit”非常相似,应该用数字5555替换。最好我们可以使用Jaro Winkler距离来在字符串中找到近似匹配,然后替换近似子字符串。我需要一个非常抽象的算法来做到这一点。
有什么想法吗?
【问题讨论】:
-
或许
gsub('gr[ae][ae]t thing', 5555 ,c('hey this is a graet thing')) -
agrep赢得胜利! -
嗯,我正在考虑使用 jaro winkler 距离应用模糊匹配算法。这可能吗?
-
你可以查看
library(stringdist)它有一些选项。 -
@akrun 但是如果它是“很棒的东西”呢?您不能为每种可能性编写正则表达式条件。
标签: r text matching fuzzy-search