Data Science Specialization Capstone

Eric Allen
2/22/2020

Johns Hopkins University/Coursera

The Word Predictor App

Motivations and Objectives

Motivations:

  • Around the world, people are spending an increasing amount of time on their mobile devices for email, social networking, banking and a whole range of other activities. But typing on mobile devices can be a serious pain. Accurately predicting the next word can alleviate some of the effort in typing.
  • Natural Language Processing is key to unlocking insights from unstructured text data, from word prediction to aid with typing and the improve the accuracy of transcriptions, to topic modeling, sentiment analysis and summarizations to understand the meaning of conversations and text.

The objectives of the capstone project are to:

  • Develop a predictive model of text starting with a large, unstructured database of the English language
  • Deploy the model in a Shiny app that takes as input a phrase (multiple words) and it predicts the next word
  • Create a presentation that pitches the algorithm and app

The Data

The corpora are collected from publicly available sources by a web crawler. The crawler checks for language, so as to mainly get texts consisting of the desired language. (The data was downloaded from the Coursera course site for the purposes of this project.)

The data consists of three text files containing blogs, news and tweets:

File Size (MB) Source Line Count Word Count Max Word Count Avg Word Count
210.2 Blog 899,288 37,546,239 6,726 41.8
205.8 News 1,010,242 34,762,395 1,796 34.4
167.1 Twitter 2,360,148 30,093,413 47 12.8

Prior to processing select profanities were removed from the data. Also, in an attempt to limit non-english words non-ASCII characters were also removed. (The frequency of non-english ascii based languages was tested and believed to be immaterial, so they were ignored.) For details on the data see the Capstone Checkpoint report.

The Model

The model is an n-gram model that matches the input last n-1 words in the phase against the n-grams and returns the nth word with the highest frequency. If an insufficient number of matches is found with 4-grams, the model moves to 3-grams, 2-grams and, finally, uni-grams. The quanteda r package was used to tokenize the data and create n-grams, removing punctuation, hash-tags, numbers, hyphens, symbols and urls. The text was also converted to all lower case. Finally to save space, n-grams that had a frequency of 1 were removed and only the 3 most frequent occurrences of root-grams ((n-1)-grams) where kept.

The table below shows some of the sample sizes chosen along with the resulting file size and prediction accuracy.

Sample Size (%) Session Memmory (Mb) Disk Memmory (Mb) Averge Response Time (sec) % Match (1st Word) % Match (Predictions 1-3)
1 4.60 0.34 0.02 12.15 21.95
10 49.19 3.77 0.11 14.65 25.40
20 99.14 7.65 0.21 15.45 26.60
40 198.83 15.43 0.33 17.70 29.75

A sample size off 40% was used in the final app.
(Note: the sampled data was further split into train (80%) and text (20%) sets.)

Reference: Chapter 4: Speech and Language Processing. Daniel Jurafsky & James H. Martin.

The App

The Shiny App reactively monitors a typed phase and returns the single next word prediction as well as a prediction for the 2nd and 3rd most likely words.