0
votes

I have taken generic text of 1000 rows and performed the below in the process of textmining. While using the document Term matrix I am not getting the count of words as output in the matrix.

>def<-read.csv("Defect.csv",header = T)
>docs<-Corpus(VectorSource(def$Summary))
>docs<-tm_map(docs,content_transformer(tolower))
>docs<-tm_map(docs,removeNumbers)
>docs<-tm_map(docs,removeWords,stopwords("english"))
>docs<-tm_map(docs,removePunctuation)
>docs<-tm_map(docs,stripWhitespace)
>docs<-tm_map(docs,stemDocument,language = "english")

>docs[[1]]$content
[1] "access logout access employe separ modul"

>dtm<-DocumentTermMatrix(docs)
>data.matrix(dtm)

Below is the output I got for DTM

Terms Docs access logout modul separ approv button click display error

I am not getting the word count in a matrix. Not sure of what could be the error here.

1
use m <- as.matrix(dtm); sort(rowSums(m), decreasing = TRUE) to get your desired output - parth
i am getting 837 789 554 727 6 93 40 38 29 29 28 28 but not the word count respectively - mpc
names(m) is giving me a NULL output - mpc

1 Answers

1
votes
def<-read.csv("Defect.csv",header = T)
docs<-Corpus(VectorSource(def$Summary))
docs<-tm_map(docs,content_transformer(tolower))
docs<-tm_map(docs,removeNumbers)
docs<-tm_map(docs,removeWords,stopwords("english"))
docs<-tm_map(docs,removePunctuation)
docs<-tm_map(docs,stripWhitespace)
docs<-tm_map(docs,stemDocument,language = "english")

Note : use TermDocumentMatrix over DocumentTermMatrix

dtm <- TermDocumentMatrix(docs)
m <- as.matrix(dtm)
v <- sort(rowSums(m),decreasing=TRUE)
d <- data.frame(word = names(v),freq=v)
rownames(d) <- NULL

Now, your dataframe should look like..

> head(d,10)
        word freq
1       file  157
2       data  151
3  incorrect  136
4     target  120
5       issu   95
6       tabl   82
7      sourc   69
8     column   63
9        get   61
10   process   56