Joh*_*ith 6 string r levenshtein-distance
我在R中有两个类型字符向量
我希望能够使用jarowinkler将引用列表与原始字符列表进行比较,并指定%相似度得分.因此,例如,如果我有10个参考项目和20个原始数据项目,我希望能够获得比较的最佳分数以及算法与之匹配的内容(因此2个向量为10).如果我有大小为8和10个参考项目的原始数据,我应该只得到8个项目的2个向量结果,每个项目的匹配和得分最高
item,match,matched_to ice,78,ice-cream
下面是我的代码,没什么可看的.
NumItems.Raw = length(words)
NumItems.Ref = length(Ref.Desc)
for (item in words)
{
for (refitem in Ref.Desc)
{
jarowinkler(refitem,item)
# Find Best match Score
# Find Best Item in reference table
# Add both items to vectors
# decrement NumItems.Raw
# Loop
}
}
Run Code Online (Sandbox Code Playgroud)
Jim*_* M. 11
使用玩具示例:
library(RecordLinkage)
library(dplyr)
ref <- c('cat', 'dog', 'turtle', 'cow', 'horse', 'pig', 'sheep', 'koala','bear','fish')
words <- c('dog', 'kiwi', 'emu', 'pig', 'sheep', 'cow','cat','horse')
wordlist <- expand.grid(words = words, ref = ref, stringsAsFactors = FALSE)
wordlist %>% group_by(words) %>% mutate(match_score = jarowinkler(words, ref)) %>%
summarise(match = match_score[which.max(match_score)], matched_to = ref[which.max(match_score)])
Run Code Online (Sandbox Code Playgroud)
给
words match matched_to
1 cat 1.0000000 cat
2 cow 1.0000000 cow
3 dog 1.0000000 dog
4 emu 0.5277778 bear
5 horse 1.0000000 horse
6 kiwi 0.5350000 koala
7 pig 1.0000000 pig
8 sheep 1.0000000 sheep
Run Code Online (Sandbox Code Playgroud)
编辑:作为对OP注释的响应,最后一个命令使用管道方法dplyr,并将原始单词和引用的每个组合分组为原始单词,将一个列match_score与jarowinkler分数相加,并仅返回一个摘要最高匹配分数(由which.max(match_score)索引),以及也由最大match_score索引的引用.