Site icon R-bloggers

Simple Text Mining with R

[This article was first published on indiacrunchin » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I’ve used R for many use cases and Text Mining is one of those. Below is a small snippet to get you started with R and Text Mining.
require(fortunes) require(tm) sentences <- NULL for (i in 1:10) sentences <- c(sentences,fortune(i)$quote) d <- data.frame(textCol =sentences ) ds <- DataframeSource(d) dsc<-Corpus(ds) dtm<- DocumentTermMatrix(dsc, control = list(weighting = weightTf, stopwords = TRUE)) dictC <- Dictionary(dtm) # The query below is created from words in fortune(1) and fortune(2) newQry <- data.frame(textCol = "lets stand up and be counted seems to work undocumented") newQryC <- Corpus(DataframeSource(newQry)) dtmNewQry <- DocumentTermMatrix(newQryC, control = list(weighting=weightTf,stopwords=TRUE,dictionary=dict1)) dictQry <- Dictionary(dtmNewQry) # Below does a naive similarity (number of features in common) apply(dtm,1,function(x,y=dictQry){length(intersect(names(x)[x!= 0],y))})


To leave a comment for the author, please follow the link and comment on their blog: indiacrunchin » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.