Swiftkey Prediction

Hi boss, I just wanted to update you with my exploratory analyses of the data we have and my proposed solution to the problem.

The Problem

As you know, we’re trying to create an algorithm that takes the text a user has entered and predicts the next word they want to say.
So we can predict language people want to use in both professional and personal contexts, we have chosen a series of large data file from twitter, online blogs, and news websites.

The Data

From exploratory analyses, the full data set is very, very large. Let’s see how long it takes to import all three files.

require(tm)
## Loading required package: tm
## Loading required package: NLP
start.time <- proc.time()
c <- VCorpus(DirSource("final/en_US/"))
how.long <- proc.time() - start.time
how.long
##    user  system elapsed 
##  77.305   1.101  79.822

Now as the boss, you probably have a faster computer, but for me it takes over a minute to just read the data. That’s becuase the file is 8.38373310^{8}. Let’s look at how many words and lines there are.

library(quanteda)
## quanteda version 0.99.22
## Using 7 of 8 threads for parallel computing
## 
## Attaching package: 'quanteda'
## The following objects are masked from 'package:tm':
## 
##     as.DocumentTermMatrix, stopwords
## The following object is masked from 'package:utils':
## 
##     View
qdt.corpus <- corpus(c)
summary(qdt.corpus)
## Corpus consisting of 3 documents:
## 
##               Text  Types   Tokens Sentences       datetimestamp
##    en_US.blogs.txt 482434 42840045   2077533 2018-01-05 03:35:30
##     en_US.news.txt 431664 39918309   1868674 2018-01-05 03:35:30
##  en_US.twitter.txt 566950 36719645   2598128 2018-01-05 03:35:30
##                 id language
##    en_US.blogs.txt       en
##     en_US.news.txt       en
##  en_US.twitter.txt       en
## 
## Source:  Converted from tm Corpus 'c'
## Created: Fri Jan  5 14:36:47 2018
## Notes:

No wonder it takes so long to read! With 2-3 million sentences in each document, and about 40 million words in each, this data isn’t going to work for our phone app. Instead, I’ve written a function to take a random sample from the documents, including 10,000 tweets, 1,000 blogs and 1,000 newspaper articles. The function also cleans the strings and removes profanity, then saves it to disk so we can use it in our future app. Let’s see how long it takes to read this saved data file. I haven’t included the code to save you space on the report.

## Loading required package: LaF
##    user  system elapsed 
##   1.095   0.017   1.119

Much better! Now it only takes 4 seconds to read. How big is it?

object.size(c)
## 46400168 bytes

Now the file is only 44 MB we can probably fit this into a phone app or on the server of a web app. How representative is this sample compared with other data sets? Let’s graph the frequency with which words occur. Again, I’ll produce the plots without showing all the code for parsimony.

## 
## Attaching package: 'tokenizers'
## The following object is masked from 'package:quanteda':
## 
##     stopwords
## The following object is masked from 'package:tm':
## 
##     stopwords
## 
## Attaching package: 'ggplot2'
## The following object is masked from 'package:NLP':
## 
##     annotate
##      word freq
## the   the 8017
## and   and 3946
## you   you 2685
## for   for 2461
## that that 1864
## with with 1334

This pattern of most frequently used words is very similar to those in the rest of the english language. As a result, it should be good for our prediction algorithm.

The Plan

Using word frequency to predict the following word is terrible because “the” occurs so many times compared with all other words. Instead, I will aim to create a prediction algorithm that uses the last two words that the person has entered. This will complete what’s called a trigram, which is a three-word string. By using trigrams, we reduce the uncertainty in the predicted word. To give you a simple example, lets look at the most frequent trigrams in our data:

##                          word freq
## i don t               i don t  123
## thanks for the thanks for the  105
## i can t               i can t   81
## can t wait         can t wait   67
## it s a                 it s a   61
## i m not               i m not   51

Our algorithm will use these trigrams to predict the third word the user enters.
- If they write “thanks for” there is a reasonable chance that the next word will be “the”
- If they write “a lot” there’s a good chance the next word will be “of”
- If they write “I love” there’s a good chance the next word will be “you”

Becuase many trigrams have never been seen, we’ll need to fill in the blank probabilities by “smoothing”. You don’t really need to know what this is, but I’ll be using the best technique for large databases (Stupid backoff; Professor Dan Jurafsky; Stanford).

Please let me know if you have any suggestions, but I hope you find this update helpful.