Reputation: 101
I have been doing some text mining. I created the DTM matrix using the following steps.
corpus1<-VCorpus(VectorSource(resume1$Dat1))
corpus1<-tm_map(corpus1,content_transformer(tolower))
corpus1<-tm_map(corpus1,content_transformer(trimWhiteSpace))
dtm<-DocumentTermMatrix(corpus1,
control = list(removePunctuation = TRUE,
removeNumbers = TRUE,
removeSparseTerms=TRUE,
stopwords = TRUE))
After all the run I am still getting words like -quotation, "fun, model" , etc in dtm.Also getting blanks like " " in the data
What can I do about it? I do not need this dahses and extra quotations.
Upvotes: 3
Views: 1719
Reputation: 1005
I know i might be a bit too late for a reply, but I was having a similar problem and did not find an answer easily. I hope this helps others facing the same issue.
To recreate the problem, you can use this example with two problematic text excerpts:
library("tm")
library("textclean")
dt <- c("Vi ville också att husmodellen skulle ” ta in” det fina älvläget så mycket som möjligt.”",
"Det är betydligt trivsammare att jobba härifrån än att sitta och ” arbeta” i ett kontorsrum i centrum.")
The dt
looks like this:
> dt
[1] "Vi ville också att husmodellen skulle ” ta in” det fina älvläget så mycket som möjligt.”"
[2] "Det är betydligt trivsammare att jobba härifrån än att sitta och ” arbeta” i ett kontorsrum i centrum."
In my case, the problem arises because I have curly braces in the text. removePunctuation
does not identify this type of braces as punctuation, so after applying it to my text I still have the curly braces.
> removePunctuation(dt)
[1] "Vi ville också att husmodellen skulle ” ta in” det fina älvläget så mycket som möjligt”"
[2] "Det är betydligt trivsammare att jobba härifrån än att sitta och ” arbeta” i ett kontorsrum i centrum"
I found package textclean
(2018) has a function that replaces the curly braces with \"
which can then be removed using removePunctuation
:
> removePunctuation(replace_curly_quote(dt))
[1] "Vi ville också att husmodellen skulle ta in det fina älvläget så mycket som möjligt"
[2] "Det är betydligt trivsammare att jobba härifrån än att sitta och arbeta i ett kontorsrum i centrum"
If you still want help to solve the other problems you mentioned, please add a code example for your data set so we can reproduce the errors and possibly fix them.
Upvotes: 3
Reputation: 1247
I'm not sure why DocumentTermMatrix isn't working for you, but you could try using tm_map to pre-process the corpus before transforming it into a dtm. This works for me (Note that I reorder the default stoplist because otherwise it removes the stems of apostrophe words before the entire word, leaving stranded 's'):
corpus1 <- VCorpus(VectorSource(resume1$dat))
reorder.stoplist <- c(grep("[']", stopwords('english'), value = TRUE),
stopwords('english')[!(1:length(stopwords('english')) %in% grep("[']", stopwords('english')))])
corpus1 <- tm_map(corpus1, content_transformer(tolower))
corpus1 <- tm_map(corpus1, removeWords, reorder.stoplist)
corpus1 <- tm_map(corpus1, removePunctuation)
corpus1 <- tm_map(corpus1, removeNumbers)
corpus1 <- tm_map(corpus1, stripWhitespace)
corpus1 <- DocumentTermMatrix(corpus1)
Upvotes: 2