R中意义相似的聚类短语
我想把意思相似的短语进行聚类并绘制一个树状图。我还想显示分组短语的列表。 我似乎只能显示一个以索引号作为输出的树状图,而不是短语本身。 此外,我有数百个短语,我想显示为一个按最大组排序的分组列表R中意义相似的聚类短语,r,tm,R,Tm,我想把意思相似的短语进行聚类并绘制一个树状图。我还想显示分组短语的列表。 我似乎只能显示一个以索引号作为输出的树状图,而不是短语本身。 此外,我有数百个短语,我想显示为一个按最大组排序的分组列表 strings.to.cluster <- c("how do i find the bus times", "where do i find the bus time tables", "where is the
strings.to.cluster <- c("how do i find the bus times",
"where do i find the bus time tables",
"where is the bus times",
"is there a bus time table",
"where is the bus time table",
"what is the meaning of life",
"the quick brown fox",
"how do i find the bus times",
"where is the bus times")
library(tm)
library(Matrix)
x <- TermDocumentMatrix( Corpus( VectorSource( strings.to.cluster ) ) )
y <- sparseMatrix( i=x$i, j=x$j, x=x$v, dimnames = dimnames(x) )
plot( hclust(dist(t(y))) )
strings.to.cluster如果您正在使用tm软件包和sparseMatrix
您正在将字符串转换为单词。你的树状图是单词而不是句子。检查如果不转置矩阵并使用绘图(hclust(dist(y))
会发生什么情况。你会看到你画的是单词,而不是句子
使用软件包stringdist,我们可以计算所有句子之间的距离,然后使用该距离矩阵计算hclust。使用选项useNames=“strings”将字符串作为标签添加到距离矩阵中,这些字符串将用作hclust对象中的标签
cl <- hclust(stringdist::stringdistmatrix(strings.to.cluster, method = "cosine", useNames = "strings"))
plot(cl)
cl