The objective of the project is to build a web app that is capable of predicting the most probable next word from a user input paragraph.
The main concept behind the prediction algorithm used is the Markov chain assumption which in simple terms states that a good prediction of the next word can be made using the word or words immediately before it.
To build the model, text data was provided from three sources; twitter, blogs and news articles, summing a dataset of around 100 million words.
n-gram models where built with this data, and a smoothing algorithm was used to calculate probability distributions of the n-grams.
Finally a Shiny application was designed to implement the model as a web app that predicts words given a user input.
To address memory limitations, each data source was split into 12 chunks, using *nix system command split, from within R.
Text processing was made using the quanteda package. Text data was first tokenized and then n-gram models where built for; 1-grams, 2-grams, 3-grams, 4-grams, and 5-grams.
Furthermore, ngram - 1 frequencies and ngram - 1 number of tokens where calculated for higher order n-grams, as this informations is needed to speed up further smoothing calculations. This was done using data.table package, which provides a faster solution than standard data frames.
As shown on previous reports, based on their low frequency and low impact on the prediction, some tokens where excluded to save space and computing power. 1-grams with frequency < 4, 2-grams with frequency < 3 and higher order grams with frequency < 1 where excluded from the models
The final model where structured as shown below: (3-grams model)
| ngrams | freq | head | last | first | tail | MLE | head_freq | head_tokens |
|---|---|---|---|---|---|---|---|---|
| one of the | 34624 | one of | the | one | of the | 0.5107011 | 67797 | 153 |
| a lot of | 30044 | a lot | of | a | lot of | 0.7038868 | 42683 | 186 |
| thanks for the | 23852 | thanks for | the | thanks | for the | 0.5743456 | 41529 | 232 |
Once each model was built, it was stored as SQLite database table, using the RSQLite package. The head column was used as the table index, to speed up matching searches.
The final database consisted in 5 tables for the 5 n-grams.
| ngram | entries |
|---|---|
| 1-grams | 90172 |
| 2-grams | 1111010 |
| 3-grams | 1297177 |
| 4-grams | 624132 |
| 5-grams | 206623 |
The Kneser-Ney smoothing method was used, which is method that calculates probability distribution taking into account the history of the n-grams, and is considered the most effective smoothing method.
One disadvantage is that it can be slow to calculate. As described before, the calculation of the ngram-1 frequencies and number of tokens within each ngram model, allows the use of this smoothing method on the fly within a reasonable speed.
The probability was calculated using the methods formula for higher order ngrams, and the special case of unigrams \((^*)\)
\[ p_{KN}(w_i|w^{i-1}_{i-n+1}) = \frac{max(c(w^{i-1}_{i-1+1}, w_i) - \delta, 0)} {\sum_{w'}c(w^{i-1}_{i-n+1}, w')} + \delta \frac{| \left\{ w':0 \lt c(w^{i-1}_{i-n+1}, w') \right\}|}{\sum_{w'}c(w^{i-1}_{i-n+1}, w')} p_{KN}(w_i|w^{i-1}_{i-n+2})\]
\((^*)\)\(\delta\) value was regarded as 0 for the highest n-gram, and \(0.75\) for lower ngrams. This value can be adjusted on the Shiny app to see the effects on the prediction and the perplexity of the model.
Finally a Shiny web app was built, which implemented the model and algorithm.
The application is able to calculate the best prediction using the presented algorithm with enough speed.
It shows the probability for the prediction and lets the user choose how many predictions to should inform.
It allows the user to modify the value of \(\delta\) that is used in the Kneser-Ney smoothing algorithm.
It also shows the perplexity of the model used.