Coursera Capstone Project

  • This presentation provides an introductory example of Natural Language Processing (NLP) techniques
  • This example focuses on predicting the tail (i.e. last) word in a phrase, similar to mobile keyboards applications implemented by Swiftkey.
  • The data contains lines of text from twitter, news and blogs in four languages (we will only use the English files).
  • The corresponding Shiny application is available here: NLP Shiny App

Step 1: Cleaning and tokenizing the data

  • A clean corpus of text is created that will allow for easy processing.
    • A corpus is a complete set of texts. In our example this includes the set of blogs, twitter and news datasets provided by Swiftkey.
    • This corpus was cleaned by converting to lower case, removing the punctuation, links, whitespace, numbers and other characters.
  • This corpus was then “tokenized”. Tokenization is the process of breaking a line/stream of text up into words and phrases.
  • Finally, n-gram files were created. An n-gram is a contiguous sequence of n words from a given sequence of text. Frequencies of each n-gram were also calculated for unigrams (1-word), bigrams(2-word), trigrams (3-word) and quadgram (4-word) files.

Step 2: Creating a simple algorithm to predict the tail word

We create a simple algorithm that calculates the probability of the tail word in a phrase. The following is a simple example of the basic backoff algorithm:

  • The user types in a three word phrase, such as “he is a”.
  • We search through all quad-grams (4-word phrases) from our corpus of text provided by Swiftkey. We calculate all frequencies where the first three words are “he is a” within the quad-gram file. For example “he is a big” may occur 20 times and “he is a very” may occur 10 times.
  • The most common 4-word phrase is used as the text prediction. In other words, if the phrase, “he is a big” is the most common 4-word phrase that starts with “he is a”, we predict the tail word to be “big”.
  • If no perfect match exists, we “backoff” to the next lower order n-gram. Here we would search for all two-word phrases in the tri-grams that start with “is a”. Then predict the most fequent tail word.
  • If no two-word phrase exists within our tri-gram file, we would search the one word phrase “a” in the bi-gram file. If no appropriate phrase is found within the bi-gram, the most common uni-gram word is selected.

Step 3: Evaluating Alternative Algorithms

  • The basic backoff algorithm will overestimate unique phrases that occur within our sample data. For example, if we happened to select a sample 4-word phrase, “my name is Methusala”, the algorithm on the previous page, could likely predict “Methusala”, before much more commonly expected tail words, such as “John” or “Mary”.
  • To account for this, alternative methods such as linear interpolation or discount methods are suggested to improve accuracy.
  • Linear interpolation incorporates weights to each n-gram estimator in order to include features of the unigram, bigram, trigram and quadgram models. Thus, more common uni-gram words, such as “John” or “Mary” have higher probabilities of being predicted.
  • Discounting methods (also called smoothing methods), such as the Katz Backoff Model provide algorithms to estimating probabilities of both the observed and unobserved n-grams and then selecting the tail word from the highest probability n-gram. Other methods include Good-Turing probabilities, or Kneser-Ney smoothing.
  • A second sample of quadrams was created to test the accuracy of the algorithms. None of our methods had over 40% accuracy.

Challenges to consider: Accuracy versus speed and simplicity

  • To increase the speed of the shiny app, n-gram files were created and saved separately (not calculated within the shiny app).
  • In this simple example, punctuation was removed, this could negatively impact our accuracy in predicting words from phrases that use punctuation. For example, “he's a big” is read as “hes a big”.
  • Clearly, including more n-grams with longer phrases can improve accuracy. However, adding 5-gram and 6-gram files did not significantly improve the algorithm accuracy. This agrees with the Markof assumption that the probability of a word depends primarily upon the most recent words. Only quad-gram, tri-gram and bi-gram files were loaded into the shiny app.
  • In order for this algorithm to run quickly, only a 5% sample was used to create the n-grams. Using a larger sample size of text or more sources of text also improves accuracy, but dramatically slows the speed of the app.