I have the following function defined in R:
CreateTestFromRaw <- function(text, dict) {
_corpus <- Corpus(VectorSource(text))
corpus_clean <- tm_map(_corpus, removeNumbers)
corpus_clean <- tm_map(corpus_clean, removeWords, stopwords('english'))
corpus_clean <- tm_map(corpus_clean, removePunctuation)
corpus_clean <- tm_map(corpus_clean, stripWhitespace)
#dtm <- DocumentTermMatrix(corpus_clean)
test <- DocumentTermMatrix(corpus_clean, control = list(dictionary = dict))
test <- apply(test, MARGIN = 2, convert_counts)
return test
}
I am using the text mining package:
install.packages("tm")
library(tm)
When I try to run the code above to create the function, it gives me these errors:
> CreateTestFromRaw <- function(text, dict) {
+ _corpus <- Corpus(VectorSource(text))
Error: unexpected input in:
"CreateTestFromRaw <- function(text, dict) {
_"
> corpus_clean <- tm_map(_corpus, removeNumbers)
Error: unexpected input in " corpus_clean <- tm_map(_"
> corpus_clean <- tm_map(corpus_clean, removeWords, stopwords('english'))
> corpus_clean <- tm_map(corpus_clean, removePunctuation)
> corpus_clean <- tm_map(corpus_clean, stripWhitespace)
> #dtm <- DocumentTermMatrix(corpus_clean)
> test <- DocumentTermMatrix(corpus_clean, control = list(dictionary = dict))
Error in stopifnot(is.list(control)) : object 'dict' not found
> test <- apply(test, MARGIN = 2, convert_counts)
Error in apply(test, MARGIN = 2, convert_counts) :
object 'test' not found
> return test
Error: unexpected symbol in " return test"
> }
Error: unexpected '}' in "}"
Why do I get these errors when trying to create this function?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…