In the previous article we created our first word cloud. A word cloud help us to find quickly the focus of the document by means of the size of the words in the plot.
The problem we saw in the first word cloud is that we were seeing words of common use such as using, use, new, approach and case. These words will distract our attention of the technical orientation of the papers we are researching.
In this session, we will eliminate these common usage words with a customized dictionary or list of words.
library(petro.One) library(tm) library(tibble) use_example(1) p1 <- onepetro_page_to_dataframe("neural_network-s0000r1000.html") p2 <- onepetro_page_to_dataframe("neural_network-s1000r1000.html") p3 <- onepetro_page_to_dataframe("neural_network-s2000r1000.html") p4 <- onepetro_page_to_dataframe("neural_network-s3000r1000.html") nn_papers <- rbind(p1, p2, p3, p4) nn_papers
Note that here we are removing some elemental common words, the ones supplied by the a text mining package called tm
. This is the same function we used in the previous session. It does not eliminate words like using, use, etc.
vdocs <- VCorpus(VectorSource(nn_papers$book_title)) vdocs <- tm_map(vdocs, content_transformer(tolower)) # to lowercase vdocs <- tm_map(vdocs, removeWords, stopwords("english")) # remove stopwords
We can take a look at what words to stop if we see the dataframe tdm.df
in the previous article. Here are some:
# our custom vector of stop words my_custom_stopwords <- c("approach", "case", "low", "new", "north", "real", "use", "using" )
# this is one way to remove custom stopwords vdocs <- tm_map(vdocs, removeWords, my_custom_stopwords)
tdm <- TermDocumentMatrix(vdocs) tdm.matrix <- as.matrix(tdm) tdm.rs <- sort(rowSums(tdm.matrix), decreasing=TRUE) tdm.df <- data.frame(word = names(tdm.rs), freq = tdm.rs, stringsAsFactors = FALSE) as.tibble(tdm.df) # prevent long printing of dataframe
You see now that using is not at the top of the table as it was before. Let's plot the wordcloud.
library(wordcloud) set.seed(1234) wordcloud(words = tdm.df$word, freq = tdm.df$freq, min.freq = 50, max.words=200, random.order=FALSE, rot.per=0.35, colors=brewer.pal(8, "Dark2"))
Now the wordcloud looks more technical oriented. Words of common use have been removed. That bring us more clarity.
There are a couple of things that we will notice in this phase of the text mining: (1) words that have similar root (log, logs, network, networks, system vs systems, etc.); and (2) words that are similar but are separated differently by dashes (real time vs. real-time, 3D vs 3-D, etc.); and (3) words that are similar but have puctuation signs such as commas, dots, exclamation sign, etc. (-time, field,).
We will work on them inn the next articles.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.