【发布时间】:2015-06-08 18:59:21
【问题描述】:
这是我想要做的: 当我分析的术语是“apples”时,我想知道“apples”需要多少转置才能在字符串中找到。
“立即购买苹果” => 需要 0 次换位(有苹果)。
"cheap aples online" => 需要 1 次换位(apples to aples)。
“在这里找到你的苹果” => 需要 2 次换位(苹果到苹果)。
"aple" => 需要 2 个换位(apples to aple)。
"bananas" => 需要 5 次换位(苹果到香蕉)。
stringdist 和 adist 函数不起作用,因为它们告诉我需要多少转置才能将一个字符串转换为另一个字符串。无论如何,这是我到目前为止所写的:
#build matrix
a <- c(rep("apples",5),rep("bananas",3))
b <- c("buy apples now","cheap aples online","find your ap ple here","aple","bananas","cherry and bananas","pumpkin","banana split")
d<- data.frame(a,b)
colnames(d)<-c("term","string")
#count transpositions needed
d$transpositions <- mapply(adist,d$term,d$string)
print(d)
【问题讨论】:
-
好的,谢谢,我应该把它也添加到标题中还是标签足够?
-
我将您的代码(在我的回答中)编辑为
a <- c(rep("apples",5),rep("bananas",3))中的苹果 -
哎呀,谢谢infominer,让我也纠正一下问题!
标签: r string-comparison string-matching stringdist