2014-02-22 36 views
1

我的问题是有点前传中Visualise distances between texts比较句子或段落的表

问我有一个表有两个句子来比较每个观察的问题。

compare <- read.table(header=T,sep="|", text= 
"person | text1 | text2 
person1 | the quick brown fox jumps over the lazy dog | the quick cat jumps on the fast fog 
person2 | I dont want to work today | I feel like working today 
" 
) 

我想要一列,其中的值表示每个观察的两个句子之间的差异。 基本上我正在寻找类似于agrep的功能,但用于比较句子或段落。

+0

该示例的预期输出是多少? –

+0

这有帮助吗? http://stackoverflow.com/questions/3182091/fast-levenshtein-distance-in-r – Superbest

回答

0

您可以使用adist函数计算字符串之间的差异。 mapply允许您将它应用于所有行:

mapply(adist, compare$text1, compare$text2) 
# [1] 17 15 
0

我不得不学习一点文本挖掘。使用tm我创建了一个函数来比较两个句子或段落并给出一个数字值。

library(tm) 

dis <- function(text1,text2){ 
#creating a corpus 
text_c <- rbind(text1,text2) 
myCorpus <- Corpus(VectorSource(text_c)) 
#creating a term document matrix 
tdmc <- TermDocumentMatrix(myCorpus, control = list(removePunctuation = TRUE, stopwords=TRUE)) 
#computing dissimilarity 
return(dissimilarity(tdmc, method = "cosine")) 
} 

compare$dis <- mapply(dis, compare$text1, compare$text2) 


person           text1         text2 dis 
person1 the quick brown fox jumps over the lazy dog the quick cat jumps on the fast fog 0.63 
person2      I dont want to work today    I feel like working today 0.75