Wordcloud of the Arizona et al. v. United States opinion

June 25, 2012
By

(This article was first published on Bommarito Consulting » r, and kindly contributed to R-bloggers)

Here’s one purely for fun – a wordcloud built from the Supreme Court’s opinion on Arizona et al. v United States.  Word clouds, though certainly not the most scientific of visualization techniques, are often engaging and “fun” ways to lead into discussion on NLP or topic modeling.

Arizona  et al. v United States wordcloud

Arizona et al. v United States wordcloud

The process to generate this image is entirely automated with Tika and R.  We convert the PDF to text, strip, normalize, and stopword the text, then convert to histogram and plot as a wordcloud. Here’s the snippet below:

$ java -jar /opt/tika/tika.jar --text arizona_et_al_v_united_states.pdf >  arizona_et_al_v_united_states.txt
$ R
>  library(tm)
>  library(wordcloud)
>  library(RColorBrewer)
>  corpus <- Corpus(DirSource(pattern="*.txt"))
>  corpus <- tm_map(tm_map(tm_map(corpus, removePunctuation), tolower), function(x) removeWords(x, stopwords("english")))
>  tdMatrix <- as.matrix(TermDocumentMatrix(corpus))
>  tdMatrix <- sort(rowSums(tdMatrix), decreasing=T)
>  freqDF <- data.frame(words=names(tdMatrix), freq=tdMatrix)
>  colorPalette <- brewer.pal(8, "RdBu")
>  png(file="arizona_v_usa_wordcloud.png", bg="black")
>  wordcloud(freqDF$words, freqDF$freq, scale=c(8, 0.25), min.freq=10, random.order=F, rot.per=0.2, colors=colorPalette)
>  dev.off()

To leave a comment for the author, please follow the link and comment on his blog: Bommarito Consulting » r.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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.