私はRで「agrep」関数を使用しています。これは一致のベクトルを返します。最適な一致、または同点の場合に最適な一致のみを返す agrep に似た関数が必要です。現在、結果のベクトルの各要素に対してパッケージ「cba」の「sdist()」関数を使用してこれを行っていますが、これは非常に冗長に思えます。
/編集: 現在使用している関数は次のとおりです。距離を2回計算するのは冗長に思えるので、スピードアップしたいと思います。
library(cba)
word <- 'test'
words <- c('Teest','teeeest','New York City','yeast','text','Test')
ClosestMatch <- function(string,StringVector) {
matches <- agrep(string,StringVector,value=TRUE)
distance <- sdists(string,matches,method = "ow",weight = c(1, 0, 2))
matches <- data.frame(matches,as.numeric(distance))
matches <- subset(matches,distance==min(distance))
as.character(matches$matches)
}
ClosestMatch(word,words)