Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中意义相似的聚类短语_R_Tm - Fatal编程技术网

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