Search code examples
rcluster-analysistm

Do not form clusters with text mining in R


I need to cluster the texts (text mining for russian texts). here the code:

mydat=read.csv("C:/Users/Admin/Downloads/kr_csv.csv", sep=";",dec=",")
View(mydat)
    

library("tm") 
library("SnowballC") 
library("textcat")

corpus=Corpus(VectorSource(mydat))

dtm=DocumentTermMatrix(corpus,
                       control=list(stemming=T, stopwords=F,
                        minWorldLenght=3,removeNumbers=T,
                        removePunctuation=T,
                        #stopwords=c(stopwords('SMART'))
                        weighting=function(x)
                          weightTf(x) ))

m<-as.matrix(dtm)

norm_eucl=function(m)
  m/apply(m,1,function(x)sum(x^2)^.5)
m_norm=norm_eucl(m)

res=kmeans(m_norm,3,100)

I chose 3-cluster decision and got this error

Error in sample.int(m, k) : cannot take a sample larger than the population when 'replace = FALSE' It is mean that I can't take more than one cluster, but this can not be. How can I extract multiple clusters? mydat

> dim(m_norm)
[1]    1 4298

head(m_norm, 20) there many docs (all on russian) I copied only part.
> Docs артикул madc артикул madc ту артикулldvoko       аскуэ   аскуэ зао аскуэ зао хакель         асу      асу тп асу тп аскуэ         атх
    Terms
Docs      атх ол  атх ол ооо      ач csb         аяд        аяд        аяд         базе базе протокола базе протокола hart бандажирования
    Terms
Docs бандажирования пучков бандажирования пучков проводов     батарея  батарея gp батарея gp в        без без протяжки без протяжки диам
    Terms
Docs без протяжки фмм без устройствадля без устройствадля ручного без электропривода без электропривода классгерметичности      белая
    Terms
Docs     бирка бирка кабельная бирка кабельная у бирка пластмассовая бирка пластмассовая квадратная         бкп    бкп дакж   бкп дакж 
    Terms
Docs        бкра       бкра       бкра   благовещенск благовещенский благовещенский арматурный благовещенский арматурный завод       блок
    Terms
Docs       блок      блок  м    блок atx  блок atx w блок клапанный блок клапанный метран блок концевых блок концевых переключателей

Solution

  • I found the error was in preprocessing

    here the working code

    mydat=read.csv("C:/Users/Admin/Downloads/kr_csv.csv", sep=";",dec=",")
    
      tw.corpus <- Corpus(VectorSource(mydat$name))
      tw.corpus <- tm_map(tw.corpus, stripWhitespace)
      tw.corpus <- tm_map(tw.corpus, removePunctuation)
      tw.corpus <- tm_map(tw.corpus, removeNumbers)
      tw.corpus <- tm_map(tw.corpus, removeWords, stopwords("russian"))
      tw.corpus = tm_map(tw.corpus, content_transformer(tolower))
      tw.corpus = tm_map(tw.corpus, stemDocument)
    
    
     doc.m <- DocumentTermMatrix(tw.corpus)
    
      dtm_tfxidf<-weightTfIdf(doc.m)
    
    m<-as.matrix(dtm_tfxidf)
    rownames(m)<-1:nrow(m)
    
    norm_eucl=function(m)
      m/apply(m,1,function(x)sum(x^2)^.5)
    m_norm=norm_eucl(m)
    
    
    > dim(m_norm)
    [1] 399 860