hooliaquoolia hooliaquoolia - 3 months ago 16
R Question

Document term matrix in R

I have the following code:

rm(list=ls(all=TRUE)) #clear data
setwd("~/UCSB/14 Win 15/Issy/text.fwt") #set working directory
files <- list.files(); head(files) #load & check working directory

fw1 <- scan(what="c", sep="\n",file="fw_chp01.fwt")

library(tm)
corpus2<-Corpus(VectorSource(c(fw1)))
skipWords<-(function(x) removeWords(x, stopwords("english")))

#remove punc, numbers, stopwords, etc
funcs<-list(content_transformer(tolower), removePunctuation, removeNumbers, stripWhitespace, skipWords)
corpus2.proc<-tm_map(corpus2, FUN = tm_reduce, tmFuns = funcs)

corpus2a.dtm <- DocumentTermMatrix(corpus2.proc, control = list(wordLengths = c(1,110))) #create document term matrix


I'm trying use some of the operations detailed in the tm reference manual (http://cran.r-project.org/web/packages/tm/tm.pdf) with little success. For example, when I try to use the findFreqTerms, I get the following error:

Error: inherits(x, c("DocumentTermMatrix", "TermDocumentMatrix")) is not TRUE


Can anyone clue me in as to why this isn't working and what I can do to fix it?

Edited for @lawyeR:

head(fw1) produces the first six lines of the text (Episode 1 of Finnegans Wake by James Joyce):

[1] "003.01 riverrun, past Eve and Adam's, from swerve of shore to bend"
[2] "003.02 of bay, brings us by a commodius vicus of recirculation back to"
[3] "003.03 Howth Castle and Environs."
[4] "003.04 Sir Tristram, violer d'amores, fr'over the short sea, had passen-"
[5] "003.05 core rearrived from North Armorica on this side the scraggy"
[6] "003.06 isthmus of Europe Minor to wielderfight his penisolate war: nor"


inspect(corpus2) outputs each line of the text in the following format (this is the final line of the text):

[[960]]
<<PlainTextDocument (metadata: 7)>>
029.36 borough. #this part differs by line of course


inspect(corpus2a.dtm) returns a table of all the types (there are 4163 in total( in the text in the following format:

Docs youths yoxen yu yurap yutah zee zephiroth zine zingzang zmorde zoom
1 0 0 0 0 0 0 0 0 0 0 0
2 0 0 0 0 0 0 0 0 0 0 0

Answer

Here is a simplified form of what you provided and did, and tm does its job. It may be that one or more of your cleaning steps caused a problem.

> library(tm) 
> fw1 <- c("riverrun, past Eve and Adam's, from swerve of shore to bend      
+                                  of bay, brings us by a commodius vicus of recirculation back to
+                                  Howth Castle and Environs.      
+                                  Sir Tristram, violer d'amores, fr'over the short sea, had passen-
+                                  core rearrived from North Armorica on this side the scraggy    
+                                  isthmus of Europe Minor to wielderfight his penisolate war: nor")
> 
> corpus<-Corpus(VectorSource(c(fw1)))
> inspect(corpus)
<<VCorpus (documents: 1, metadata (corpus/indexed): 0/0)>>

[[1]]
<<PlainTextDocument (metadata: 7)>>
riverrun, past Eve and Adam's, from swerve of shore to bend      
                                 of bay, brings us by a commodius vicus of recirculation back to
                                 Howth Castle and Environs.      
                                 Sir Tristram, violer d'amores, fr'over the short sea, had passen-
                                 core rearrived from North Armorica on this side the scraggy    
                                 isthmus of Europe Minor to wielderfight his penisolate war: nor

> dtm <- DocumentTermMatrix(corpus)
> findFreqTerms(dtm)
 [1] "adam's,"       "and"           "armorica"      "back"          "bay,"          "bend"         
 [7] "brings"        "castle"        "commodius"     "core"          "d'amores,"     "environs."    
[13] "europe"        "eve"           "fr'over"       "from"          "had"           "his"          
[19] "howth"         "isthmus"       "minor"         "nor"           "north"         "passen-"      
[25] "past"          "penisolate"    "rearrived"     "recirculation" "riverrun,"     "scraggy"      
[31] "sea,"          "shore"         "short"         "side"          "sir"           "swerve"       
[37] "the"           "this"          "tristram,"     "vicus"         "violer"        "war:"         
[43] "wielderfight" 

As another point, I find it useful at the start to load a few other complementary packages to tm.

library(SnowballC); library(RWeka); library(rJava); library(RWekajars)

For what its worth, as compared to your somewhat complicated cleaning steps, I usually trudge along like this (replace comments$comment with your text vector):

comments$comment <- tolower(comments$comment)
comments$comment <- removeNumbers(comments$comment)
comments$comment <- stripWhitespace(comments$comment) 
comments$comment <- str_replace_all(comments$comment, "  ", " ") 
# replace all double spaces internally with single space   
# better to remove punctuation with str_ because the tm function doesn't insert a space
library(stringr)
comments$comment <- str_replace_all(comments$comment, pattern = "[[:punct:]]", " ") 
comments$comment <- removeWords(comments$comment, stopwords(kind = "english"))