Reputation: 4686
I am new to text processing with R. I'm trying the simple code below
library(RTextTools)
texts <- c("This is the first document.", "This is the second file.", "This is the third text.")
matrix <- create_matrix(texts,ngramLength=3)
which is one of the answers in the question Finding 2 & 3 word Phrases Using R TM Package
However, it gives an error Error in FUN(X[[2L]], ...) : non-character argument
instead.
I can generate a document term matrix when I drop the ngramLength
parameter, but I do need to search for phrases of certain word length. Any suggestions of alternative or corrections?
Upvotes: 8
Views: 2324
Reputation: 61
ngramLength seems not to work. Here is a workaround:
library(RTextTools)
library(tm)
library(RWeka) # this library is needed for NGramTokenizer
library
texts <- c("This is the first document.",
"Is this a text?",
"This is the second file.",
"This is the third text.",
"File is not this.")
TrigramTokenizer <- function(x) NGramTokenizer(x, Weka_control(min = 3, max = 3))
dtm <- DocumentTermMatrix(Corpus(VectorSource(texts)),
control=list(
weighting = weightTf,
tokenize = TrigramTokenizer))
as.matrix(dtm)
The tokenizer uses RWeka
's NGramTokenizer
instead of the tokenizer called by create_matrix
. You can now use dtm
in the other RTextTools functions, like training a classification model below:
isText <- c(T,F,T,T,F)
container <- create_container(dtm, isText, virgin=F, trainSize=1:3, testSize=4:5)
models=train_models(container, algorithm=c("SVM","BOOSTING"))
classify_models(container, models)
Upvotes: 3
Reputation: 422
I ran into this same error. I found a fix in this pull request https://github.com/timjurka/RTextTools/pull/5/files. I did the change by "trace(create_matrix,edit=T)". Now it works :)
Upvotes: 2
Reputation: 11
I don't think it is an issue with Character (input data type). Same error when I use the NYTimes dataset , which is provided withe the package and run the same code as accompanied in the help manual.
Upvotes: 0