1

Until I used this phrase, "TermDocumentMatrix" was good.

doc <- tm_map(doc, gsub, pattern = "buy", replacement = "bought")

However, after using this phrase, "TermDocumentMatrix" will generate an error.

Error in UseMethod("meta", x) : 
no applicable method for 'meta' applied to an object of class "character"

I need a word replacement. So I have used this phrase.

My doc structure is as follows.

1. so I bought it.
2. I bought the EH AC line in November 2014 
3. 3rd product bought from AC and all no good.
(skip)

How can I use "TermDocumentMatrix"?

library(tm)
library(XML)
library(SnowballC)

doc<-VCorpus(VectorSource(readLines(file.choose())))

doc <- tm_map(doc, stripWhitespace)

doc <- tm_map(doc, stemDocument)

doc<-tm_map(doc, content_transformer(tolower))

doc<-tm_map(doc, removeWords, stopwords("english"))

myStopwords <- c(stopwords("english"), "can", "will")
myStopwords <- setdiff(myStopwords, c("will","can"))
doc <- tm_map(doc, removeWords, myStopwords)

doc<-tm_map(doc,removeNumbers)


#If you omit this step, the error will not appear in "TermDocumentMatrix".
doc <- tm_map(doc, gsub, pattern = "buy", replacement = "bought")

doc <- TermDocumentMatrix(doc, control=list(removePunctuation=T))
jiji
  • 125
  • 1
  • 3
  • 9

1 Answers1

1

You need to pass a proper content transformer to tm_map, not an arbitrary character manipulating function

doc <- tm_map(doc, content_transformer(function(x) 
    gsub(x, pattern = "buy", replacement = "bought")))
MrFlick
  • 195,160
  • 17
  • 277
  • 295