1 Introduction to Text Cleaning

Here, we shall use some texts from Gutenbergr package to demonstrate the idea of tidy text.

Here, the tidytext package is mainly used for all the text mining works, the package dplyr provides the piping operator %>% which can readily be used to determine the course of action to be taken with the data, and package gutenbergr is used to download relevant text files from Project Gutenberg. We shall be using ggplot2 and plotly for making interactive plots.

2 Exploring Project Gutenberg

The gutenbergr package provides the data gutenberg_metadata that contains all the information about all books available in Project Gutenberg, as well as their author, title and relevant details.

However, since this contains many books which are written in languages other than English, we first filter out only those which are written in English using gutenberg_works() function.

Now, let us see which genre’s these works fall into.

Now, we shall consider downloading some of the Sherlock Holmes stories, which can be filtered by using regular expressions.

Let us download first five texts of the sherlock holmes stories corresponding to the above ids.

3 Counting Words

3.1 Splitting into Words

Once we have our data containing the texts of the stories, the next thing is to split the story into paragraphs, paragraphs into sentences, sentences into words. We shall treat words as the most basic foundation of text processing.

Now, the gutenberg_download function already splits our story into sentences, hence, we just need to split them up into words. For this, unnest_token function is used.

Now that we have collected the words, let us visualize the frequency of the words.

We see that the most appearing words are the, and, of, a, an etc. which should appear in every text very often, and clearly, they does not give a meaningful feature for the stories of Sherlock Holmes. Therefore, we need to remove these.

3.2 Removing Stopwords

tidytext package comes with a built-in dataset for stopwords in English.

Now that you have an idea of what these stopwords are, we need to remove these words from tidy_books dataframe. This is done by removing the words which are common to both tidy_books and stop_words, which is basically performing an operation opposite of join. Hence, we are going to use anti_join function for this.

Now we look into the same bar diagram for most appearing words.

Now we find that the most appearing word is holmes, which not a stopword since it is not very common in English, however, it should be very common in all texts of Sherlock Holmes. Hence, there are some very common words, which is common for the corpus we are investigating, and we need a proper way to remove these words, based on some automatic thresholding. Tf-idf comes to the rescue!

3.3 Counting tf-idf

Firstly, we shall create term document matrix. For this, we create the grouping by title and word, and then count the number of such appearences in this combination.

Now that we have the term document matrix, we can create the tf-idf using bind_tf_idf function.

Let us see some words for which tf-idf is 0, these words are those which appears in almost all the documents under consideration.

Now we see that holmes is really a very common word, which appears in all of the texts, hence does not have much meaning to understand the differences between the stories of Sherlock holmes.

Let us make an wordcloud out of these most appearing words, which possibly describes the generality of stories of Sherlock Holmes.

So, now we remove these common words from the term document dataframe, which will only retain the meaningful words.

Note that, there are 4 books available in our data. Now, corresponding to each book, we rearrange the rows in decreasing tf-idf, obtaining the important words for each book, so that we can visualize them in a suitable plot.


               A Study in Scarlet The Adventures of Sherlock Holmes 
                             3094                              5193 
   The Memoirs of Sherlock Holmes     The Return of Sherlock Holmes 
                             4793                              5407