0

how to store the sparsity and maximum term length of Term Document Matrix in separate variable in R while finding ngrams ?

library(tm)
library(RWeka)

#stdout <- vector('character')
#con <- textConnection('stdout','wr',local = TRUE)

#reading the csv file
worklog <- read.csv("To_Kamal_WorkLogs.csv");


#removing the unwanted columns
cols <- c("A","B","C","D","E","F");
colnames(worklog)<-cols;
worklog2 <- worklog[c("F")]

#removing non-ASCII characters
z=iconv(worklog2, "latin1", "ASCII", sub="")

#cleaning the data Removing Date and Time
worklog2$F=gsub("[0-9]+/[0-9]+/[0-9]+ [0-9]+:[0-9]+:[0-9]+ [A,P][M]","",worklog2$F);


#loading the vector Data to corpus
a <- Corpus(VectorSource(worklog2$F))

#cleaning the data
a <- tm_map(a,removeNumbers)
a <- tm_map(a,removePunctuation)
a <- tm_map(a,stripWhitespace)
a <- tm_map(a,tolower)
a <- tm_map(a, PlainTextDocument)
a <- tm_map(a,removeWords,stopwords("english")) 
a <- tm_map(a,stemDocument,language = "english")

#removing custom stopwords
stopwords="open";
if(!is.null(stopwords)) a <- tm_map(a, removeWords, words=as.character(stopwords))


#finding 2,3,4 grams
bigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 2, max = 2))
tdm2 <- TermDocumentMatrix(a, control = list(tokenize = bigramTokenizer))
tdm2 <- removeSparseTerms(tdm2, 0.75)

#output

> tdm2
<<TermDocumentMatrix (terms: 27, documents: 8747)>>

Non-/sparse entries: 87804/148365

Sparsity           : 63%

Maximal term length: 20

Weighting          : term frequency (tf)

how to store the above Sparsity, Maximal term length, Weighting, Non-/sparse entries in separate variables.

Ken Benoit
  • 14,454
  • 27
  • 50
PradhanKamal
  • 540
  • 4
  • 18
  • 1
    Can you provide a simpler example that can be reproduced, as well as details as to what answer you want to achieve? What do you mean by "Sparsity" and "Weighting" for instance? – Ken Benoit Oct 07 '15 at 16:16
  • when we write tdm2 variable in R console after compiling the program we get the following output: <> Non-/sparse entries: 87804/148365 Sparsity : 63% Maximal term length: 20 Weighting : term frequency (tf) – PradhanKamal Oct 07 '15 at 16:20
  • i want to store these values in separate variables? so how to do it ? – PradhanKamal Oct 07 '15 at 16:24
  • sparsity and weighting are the properties of the termdocumentmatrix object. – PradhanKamal Oct 07 '15 at 16:25
  • OK, answered. In future questions, try to focus just on the essential part of the question - here saving the stats reported by the `print` method for a `TermDocumentMatrix` class object. The rest of the information you provided is not relevant, and not reproducible. If you can use a built-in object such as `crude`, all the better. And welcome to StackOverflow. – Ken Benoit Oct 07 '15 at 17:58

1 Answers1

0

This will return the stats you need. Your question did not specify what format you desired, so here I have used a named list. (This could easily be returned as a data.frame.)

I took this from the tm package source code, file Matrix.R, where the print method for TermDocumentMatrix objects is defined.

getTDMstats <- function(x) {
    # where x is a TermDocumentMatrix
    list(sparsity = ifelse(!prod(dim(x)), 100, round((1 - length(x$v)/prod(dim(x))) * 100)) / 100,
         maxtermlength = max(nchar(Terms(x), type = "chars"), 0), 
         weightingLong = attr(x, "weighting")[1], 
         weightingShort = attr(x, "weighting")[2], 
         nonsparse = length(x$v), 
         sparse = prod(dim(x)) - length(x$v))
}
data(crude)
tdm2 <- TermDocumentMatrix(crude)
tdm2
## <<TermDocumentMatrix (terms: 1266, documents: 20)>>
## Non-/sparse entries: 2255/23065
## Sparsity           : 91%
## Maximal term length: 17
## Weighting          : term frequency (tf)
getTDMstats(tdm2)
## $sparsity
## [1] 0.91
## 
## $maxtermlength
## [1] 17
## 
## $weightingLong
## [1] "term frequency"
## 
## $weightingShort
## [1] "tf"
## 
## $nonsparse
## [1] 2255
## 
## $sparse
## [1] 23065
Ken Benoit
  • 14,454
  • 27
  • 50