我有两个单词向量。与R匹配的字符串:寻找最佳匹配
Corpus<- c('animalada', 'fe', 'fernandez', 'ladrillo')
Lexicon<- c('animal', 'animalada', 'fe', 'fernandez', 'ladr', 'ladrillo')
我需要在词汇和语料库之间做出最好的匹配。 我尝试了很多方法。这是其中之一。
library(stringr)
match<- paste(Lexicon,collapse= '|^') # I use the stemming method (snowball), so the words in Lexicon are root of words
test<- str_extrac_all (Corpus,match,simplify= T)
test
[,1]
[1,] "animal"
[2,] "fe"
[3,] "fe"
[4,] "ladr"
不过,本场比赛应该是:
[1,] "animalada"
[2,] "fe"
[3,] "fernandez"
[1,] "ladrillo"
相反,与之匹配的是与第一个词在我的词汇按字母顺序排列。顺便说一下,这些向量是我拥有的更大列表的样本。
我没有尝试使用正则表达式(),因为我不确定它是如何工作的。也许解决方案就是这样。
你能帮我解决这个问题吗?感谢您的帮助。
我正在用真正的Lexicon测试你的答案。我稍后会通知结果。谢谢你们俩 – pch919