I am unable to create a term document matrix using tm package in R which throws the following error as I try to create one out of a preprocessed corpus.
Error in UseMethod("TermDocumentMatrix", x) :
no applicable method for 'TermDocumentMatrix' applied to an object of class
"character"
Below is my script that I am using. I am using R v3.4.1 with tm package v0.7-1.
data <- readLines("Data/en_US/en_US_sample.txt", n = 100)
data <- Corpus(VectorSource(data))
data <- tm_map(data, removePunctuation)
data <- tm_map(data, removeNumbers)
data <- tm_map(data, content_transformer(tolower))
data <- tm_map(data, removeWords, stopwords("en"))
data <- tm_map(data, stripWhitespace)
words <- TermDocumentMatrix("data")
I believe TermDocumentMatrix requires the corpus to be in some specified text document format so I tried coercing my corpus to PlainTextDocument using tm_map but it doesn't solve the problem. When I am loading the my text data using Corpus on VectorSource, object created shows the class as SimpleCorpus which might be the problem but I am not totally sure.
Any help would be much appreciated. Thanks!
You did everything right, just in your last line you accidentally passed a character "data" (note the quotation marks) to the function TermDocumentMatrix() instead of the object data.
Related
Using R{tm} package, i create a corpus, per usual:
mycorpus <- Corpus(DirSource(folder,pattern="txt"))
Please note I am not using an encoding variable. The summary (mycorpus) shows document names listed. However after a series of tm_map transforms:
(content_transformer(tolower),content_transformer(removeWords), stopwords("SMART"),stripWhitespace)
ending with mycorpus<- tm_map(mycorpus, PlainTextDocument) and mydtm <- DocumentTermMatrix(mycorpus, control = list(...))
I get an error with inspect(mydtm[1:10, intersect(colnames(dtm), 'toyota')]) to get my variable of choice:
Terms
Docs toyota
character(0) 0
character(0) 0
character(0) 0
character(0) 0
character(0) 1
character(0) 0
character(0) 0
character(0) 0
character(0) 1
character(0) 0
The file names (doc ids) have disappeared. Any idea what could be causing this error? more importantly, how do i reinstate the document names? Many thanks.
Code below will work for single file. You likely could use something like list.files to read all files in the directory.
First, I would wrap the cleaning functions in a custom function. Note the order matters and you have to use content_transformer if the function is not from tm.
clean.corpus<-function(corpus){
corpus <- tm_map(corpus, removePunctuation)
corpus <- tm_map(corpus, stripWhitespace)
corpus <- tm_map(corpus, removeNumbers)
corpus <- tm_map(corpus, content_transformer(tolower))
corpus <- tm_map(corpus, removeWords, custom.stopwords)
return(corpus)
}
Then concatenate English words with custom words. This is passed as the last part of the custom function above.
custom.stopwords <- c(stopwords('english'), 'lol', 'smh')
doc<-read.csv('coffee.csv', header=TRUE)
The CSV is a data frame with a column of tweets in a text document and another column with an ID for each tweet. The file from my workshop with this file is here.
The csv file is now in memory so next step is to read it in tabular fashion with a specific mapping when making a corpus. Here the content is in a column called text and the unique ID is in a column name "id".
custom.reader <- readTabular(mapping=list(content="text", id="id"))
corpus <- VCorpus(DataframeSource(doc), readerControl=list(reader=custom.reader))
corpus<-clean.corpus(corpus)
The corpus creation uses the readerControl and then once done you can apply the pre-processing steps. Without the reader control the package assigns the 0 character as the name.
The corpus content of document 1 can be accessed here
corpus[[1]][1]
You can review the corpus meta data for the first document with this code
corpus[[1]][2]
So I think you are needing to use readTabular and readerControl in your corpus construction no matter the source.
I was having the same problem and I realized that it was due to tolower. tolower, unlike removeNumbers, removePunctuation, removeWords, stemDocument, stripWhitespace are not tranformations defined in the tm package. To get a list of transformations defined in the tm package that can be directly applied to a corpus, type:
getTransformations()
[1] “removeNumbers” “removePunctuation” “removeWords” “stemDocument” “stripWhitespace”
Thus, in order to use tolower it first must make a transformation for tolower for it to handle corpus objects properly.
docs <- tm_map(docs,content_transformer(tolower))
The above line of code should stop the files from being renamed to character(0)
The same trick can be applied to any R function to work with corpuses. For example for gsub, the following syntax applies:
docs <- tm_map(docs, content_transformer(gsub), pattern = “internt”, replacement = “internet”)
First of all, my apology to repeat a question that was asked Aug 1 '13. But I cannot comment to the original question as I must have 50 reputation to be able to comment which I dont have. The original question can be retrieved from R text mining documents from CSV file (one row per doc) .
I am trying to work with the tm package in R, and have a CSV file of article abstracts with each line being a different abstract. I want each line to be a different document within the corpus. There are 2,000 rows in my data set.
I run the following codes as previously suggested by Ben:
# change this file location to suit your machine
file_loc <- "C:/Users/.../docs.csv"
# change TRUE to FALSE if you have no column headings in the CSV
x <- read.csv(file_loc, header = TRUE)
require(tm)
corp <- Corpus(DataframeSource(x))
docs <- DocumentTermMatrix(corp)
When I check class:
# checking class
class(docs)
[1] "DocumentTermMatrix" "simple_triplet_matrix"
The problem is tm transformations do not work on this class:
# Preparing the Corpus
# Simple Transforms
toSpace <- content_transformer(function(x, pattern) gsub(pattern, " ", x))
docs <- tm_map(docs, toSpace, "/")
I get this error:
Error in UseMethod("tm_map", x) :
no applicable method for 'tm_map' applied to an object of class "c('DocumentTermMatrix', 'simple_triplet_matrix')"
or another code:
docs <- tm_map(docs, toSpace, "/|#|nn|")
I get the same error:
Error in UseMethod("tm_map", x) :
no applicable method for 'tm_map' applied to an object of class "c('DocumentTermMatrix', 'simple_triplet_matrix')"
Your help would be greatly appreciated.
The code
docs <- tm_map(docs, toSpace, "/|#|nn|")
must be replaced with
docs <- tm_map(docs, toSpace, "/|#|\\|").
Then it will work fine.
I am using tm package for text mining in R. I performed following steps:
Import the data in R system and Creating Text Corpus
dataorg <- read.csv("Report_2014.csv")
corpus <- Corpus(VectorSource(data$Resolution))
Clean the data
mystopwords <- c("through","might","much","had","got","with","these")
cleanset <- tm_map(corpus, removeWords, mystopwords)
cleanset <- tm_map(cleanset, tolower)
cleanset <- tm_map(cleanset, removePunctuation)
cleanset <- tm_map(cleanset, removeNumbers)
Creating Term Document Matrix
tdm <- TermDocumentMatrix(cleanset)
At this point I export the TDM data into csv in order to perform some manual cleansing of the terms
write.csv(inspect(tdm), file="tdmfile.csv")
Now the problem is that I want to bring back the cleaned tdm csv file into R system and perform further text analysis like clustering, frequency analysis.
But I am not able to convert the csv file back into corpus format acceptable by tm package algorithms so I am not able to proceed further with my text analysis.
It would be really helpful if somebody can help me out to convert cleaned csv file into corpus format which is acceptable by text analysis functions of tm package.
First read the csv back into R
df<-read.csv("tdmfile.csv")
Then convert the vector (referenced by the column name) into a corpus
corpus<-Corpus(VectorSource(df$column))
If the above doesn't work, try converting the df into utf-8 before the corpus
convert <- iconv(df,to="utf-8-mac")
you are using keyword Dataorg...but i did n't see anywhere you are mentioning it in your code....
if you want convert your csv file into Corpus Format just fellow this link
R text mining documents from CSV file (one row per doc)
What I am trying to do is to load a csv file, and convert to an term-document matrix.
Here is part of my code:
myCorpus<-read.csv('alert-sample-data-4-mining.csv', head=TRUE)
TermDocumentMatrix(myCorpus, control=list(wordLengths=c(1,Inf)))
But get an error message said: Error in UseMethod("TermDocumentMatrix", x) :
no applicable method for 'TermDocumentMatrix' applied to an object of class "data.frame"
A few things here -- you're not loading the tm library and you're not creating a corpus. Try something like this (assuming your text data is in a field called "text" in the csv file):
library(tm)
myCorpus <- read.csv("alert-sample-data-4-mining.csv")
corpus <- Corpus(VectorSource(myCorpus$text))
TermDocumentMatrix(corpus)
I am analyzing the Reuters 21578 corpus, all the Reuters news articles from 1987, using the "tm" package. After importing the XML files into an R data file, I clean the text--convert to plaintext, convert to lwer case, remove stop words etc. (as seen below)--then I try to convert the corpus to a document term matrix but receive an error message:
Error in UseMethod("Content", x) :
no applicable method for 'Content' applied to an object of class "character"
All pre-processing steps work correctly up until document term matrix.
I created a non-random subset of the corpus (with 4000 documents) and the document term matrix command works fine on that.
My code is below. Thanks for the help.
##Import
file <- "reut-full.xml"
reuters <- Corpus(ReutersSource(file), readerControl = list(reader = readReut21578XML))
## Convert to Plain Text Documents
reuters <- tm_map(reuters, as.PlainTextDocument)
## Convert to Lower Case
reuters <- tm_map(reuters, tolower)
## Remove Stopwords
reuters <- tm_map(reuters, removeWords, stopwords("english"))
## Remove Punctuations
reuters <- tm_map(reuters, removePunctuation)
## Stemming
reuters <- tm_map(reuters, stemDocument)
## Remove Numbers
reuters <- tm_map(reuters, removeNumbers)
## Eliminating Extra White Spaces
reuters <- tm_map(reuters, stripWhitespace)
## create a term document matrix
dtm <- DocumentTermMatrix(reuters)
Error in UseMethod("Content", x) :
no applicable method for 'Content' applied to an object of class "character"