I have a data set with some phrases.
samp$reviewText[1:5]
[1] "good use worked expected"
[2] "brother got screen protector nice finish"
[3] "however bubbles really hard get also got huge rip middle"
[4] "never stay stuck phone"
[5] "use iphone good product drop phones save glass"
My objective is to build clusters of phrases with a similar meaning.
I execute this task by running the following operations.
Converting the phrases to a bag of words representation.
library(tm)
corpus = Corpus(VectorSource(samp$reviewText))
dtm = DocumentTermMatrix(corpus, control = list(weightTfIdf))
dtm = as.data.frame(as.matrix(dtm))
colnames(dtm) = make.names(colnames(dtm), unique = T)
Perform the clustering (I use 3 centers in this example) and merging the clusters to samp
.
clust = kmeans(dtm, 3)
samp$cluster = clust$cluster
So the result would like this:
samp_cluster[1:5,]
reviewText cluster
1 good use worked expected 3
2 brother got screen protector nice finish 1
3 however bubbles really hard get also got huge rip middle 3
4 never stay stuck phone 3
5 use iphone good product drop phones save glass 3
Is this the right way to do this task?