Text Mining with R – Comparing Word Counts in two Text Documents

August 19, 2013
By

(This article was first published on theBioBucket*, and kindly contributed to R-bloggers)

Here’s what I came up with to compare word counts in two pieces of text. If you got any idea, I’d love to learn about alternatives!

1
## a function that compares word counts in two texts<br />wordcount <- function(x, y, stem = F, minlen = 1, marg = F) {<br /><br />                        require(tm)<br /><br />                        x_clean <- unlist(strsplit(removePunctuation(x), "\\s+"))<br />                        y_clean <- unlist(strsplit(removePunctuation(y), "\\s+"))<br /><br />                        x_clean <- tolower(x_clean[nchar(x_clean) >= minlen])<br />                        y_clean <- tolower(y_clean[nchar(y_clean) >= minlen])<br /><br />                        if ( stem == T ) {<br /><br />                          x_stem <- stemDocument(x_clean)<br />                          y_stem <- stemDocument(y_clean)<br />                          x_tab <- table(x_stem)<br />                          y_tab <- table(y_stem)    <br /><br />                          cnam <- sort(unique(c(names(x_tab), names(y_tab))))<br /><br />                          z <- matrix(rep(0, 3*(length(cnam)+1)), 3, length(cnam)+1, dimnames=list(c("x", "y", "rowsum"), c(cnam, "colsum")))<br />                          z["x", names(x_tab)] <- x_tab<br />                          z["y", names(y_tab)] <- y_tab<br />                          z["rowsum",] <- colSums(z)<br />                          z[,"colsum"] <- rowSums(z)<br />                          ifelse(marg == T, return(t(z)), return(t(z[1:dim(z)[1]-1, 1:dim(z)[2]-1])))<br /><br />                          } else { <br /><br />                          x_tab <- table(x_clean)<br />                          y_tab <- table(y_clean)    <br /><br />                          cnam <- sort(unique(c(names(x_tab), names(y_tab))))<br /><br />                          z <- matrix(rep(0, 3*(length(cnam)+1)), 3, length(cnam)+1, dimnames=list(c("x", "y", "rowsum"), c(cnam, "colsum")))<br />                          z["x", names(x_tab)] <- x_tab<br />                          z["y", names(y_tab)] <- y_tab<br />                          z["rowsum",] <- colSums(z)<br />                          z[,"colsum"] <- rowSums(z)<br />                          ifelse(marg == T, return(t(z)), return(t(z[1:dim(z)[1]-1, 1:dim(z)[2]-1])))<br />                          }<br />                        }<br /><br />## example<br />x = "Hello new, new world, this is one of my nice text documents - I wrote it today"<br />y = "Good bye old, old world, this is a nicely and well written text document"<br /><br />wordcount(x, y, stem = T, minlen = 3, marg = T)<br />

To leave a comment for the author, please follow the link and comment on their blog: theBioBucket*.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Mango solutions



RStudio homepage



Zero Inflated Models and Generalized Linear Mixed Models with R

Quantide: statistical consulting and training

datasociety

http://www.eoda.de





ODSC

ODSC

CRC R books series





Six Sigma Online Training









Contact us if you wish to help support R-bloggers, and place your banner here.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)