这是我想做的事情:当我分析的术语是“苹果”时,我想知道“苹果”需要多少转置才能在字符串中找到。
“立即购买苹果” => 需要 0 次换位(有苹果)。
“网上便宜的苹果” => 需要 1 次换位(苹果到苹果)。
“在这里找到你的苹果” => 需要 2 个换位(苹果到苹果)。
"aple" => 需要 2 次换位(从苹果到苹果)。
"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)