Reputation: 61
I'm very new to text mining, but I wanted to analysis tweets over a period of time.
I scraped tweets from Twitter weeks ago and am only now getting to analysis it. I saved the DocumentTermMatrix as a matrix and am running into difficulty converting it back to a DocumentTermMatrix to perform latent dirichlet allocation on the data.
scrap<- searchTwitter("#RepealThe8th", n=1500)
twscrap <- sapply(scrap, function(x) x$getText())
corpus1 <- Corpus(VectorSource(twscrap))
corpus1 <- tm_map(corpus1,
content_transformer(function(x) iconv(x, to='UTF-8-MAC', sub='byte')),
mc.cores=1)
corpus1 <- tm_map(corpus1, content_transformer(tolower), mc.cores=1)
corpus1 <- tm_map(corpus1, removePunctuation, mc.cores=1)
corpus1 <- tm_map(corpus1, function(x)removeWords(x,stopwords()), mc.cores=1)
corpus1 <- tm_map(corpus1, stemDocument, mc.cores=1)
myStopwords = c("https", "http");
idx = which(myStopwords == "r");
myStopwords = myStopwords[-idx];
corpus1 = tm_map(corpus1, removeWords, myStopwords);
corpus1 <- tm_map(corpus1, stripWhitespace)
plaincorpus1 <- tm_map(corpus1, PlainTextDocument)
dtm <- DocumentTermMatrix(plaincorpus1, control = list(minWordLength = 3));
m <- as.matrix(dtm)
That was how I originally saved the data
write.csv(m, "matrix.csv")
When I load the data in I can't get it back into DTM form
m <- read.csv("matrix.csv",header=TRUE)
corpNR<-Corpus(DataframeSource(xNR))
dtmNR<-DocumentTermMatrix(corpNR)
dtmNR$dimnames$Terms <- colnames(xNR) #add terms to DocTermMetrix
str(dtmNR)
dtmNR$ncol <- length(dtmNR$dimnames$Terms) #give it the right no. of cols
This gives me a DTM of the right side but I'm not sure how to get the correct data for dtmNR$i, dtmNR$j or dtmNR$v
I also tried
library(qdap)
m1 <- as.Corpus(m)
#Error in data.frame(grouping, text.var, check.names = FALSE, stringsAsFactors = FALSE) :
# arguments imply differing number of rows: 2062, 1500
#dtm1 <- as.DocumentTermMatrix(m1)
dtm1 <- as.TermDocumentMatrix(m1)
#Error in .TermDocumentMatrix(t(x), weighting) :
# argument "weighting" is missing, with no default
Upvotes: 0
Views: 415
Reputation: 9313
Don't write it out to a csv file like that.
Instead, use save(file='myDTM.RData', list=list(dtm)) # or similar
; and load('myDTM.RData')
it later.
Upvotes: 0