The First step was to load the necessary libraries for the task
I tried many ways to predict next word, using the files provided by the assignment, I first selected a thousand rows randomly, but the prediction of my model wasn't accurate enough, the words predicted were not those expected. By my understanding the predicted word should be one of the most commonly used. For example if the input phrase is "I love my" I expect a series of common words like: "mom, wife, family, dad and so on" but is probable that in a small database these choices doesn't show up and we could end up in a prediction like "I love my nose" or "my jeep", that are of course grammatically correct but wouldn't be my first choice and probably the model wouldn't be of much use. After many tests such as increasing the numbers of rows until 30.000 randomly I've decided to use the entire database and clean it from numbers, punctuation, not-English words and NA. Therefore I downloaded the full texts and subset them as explained above. This took a considerable amount of time and memory to do the job, but I needed to do just the first time. For the next times I uploaded the cleaned data in a public space and the app will download as required.
In this section, I tokenized the text in chunks of 3 words and counted the occurrences of each token, I had then added an extra column with the last word of the token. In total there are 3 columns to begin with. I then ordered according to the frequency of the trigram in the dataset. Here is the result of the first rows
News
words n last_w
7390368 in the first 5193 first
15041366 the united states 4477 states
14597490 the first time 4169 time
5609207 for the first 3446 first
10257579 of the year 3416 year
7394916 in the past 3036 past
Tweet
words n last_w
3559172 for the follow 7243 follow
4284496 have a great 5256 great
4726023 i dont know 4531 know
3563564 for the rt 4136 rt
5766602 let me know 4112 know
9716301 thanks for following 3417 following
Blog
words n last_w
7404232 i dont know 4765 know
1917598 at the end 3673 end
15270885 the first time 3559 time
7784174 in the world 3481 world
7404597 i dont think 2753 think
7777021 in the middle 2600 middle
I binded now the three dataset changing the column names in order to have a single file containing all the most frequent occurrences of the three sets. The first column is the trigram, the second column the frequency of the trigram in the text, the third column is the last word extracted from the trigram and the forth the 2-words extracted from the trigram.
Here's the result of the combined dataset:
trigram freq last bigram
35591722 for the follow 7243 follow for the
42844961 have a great 5256 great have a
7390368 in the first 5193 first in the
74042321 i dont know 4765 know i dont
47260232 i dont know 4531 know i dont
15041366 the united states 4477 states the united
And here's a plot of the most frequent words in the corpora:
In this section I've wrote a function to predict the next word of an input sentence. The function does a very simple task, select the trigram and detect in the whole dataset the last words matching the last 2 tokens of the trigram, resulting in an output of a series of words. I selected the first 5 occurrences in order of their frequency in the corpora. Here's an example:
As one can see is not so bad, of course depending on the input text the output could be weird and unlikely but the result shows not less efficiency than the "Stupid backoff method" or "Markov chain model". Surely I could have made it more efficient trimming for different sequences of tokens but it would have required a more efficient hardware, a nvidia graphic card to use the gpu capabilities, more ram and processors with more performance. It took a considerable amount of time doing all the tests and many times my computer crashed out of memory. I delved into AI testing regression models, LSTM, B-LSTM, NPL, RNN, trying various models, but at the end the result was not satisfactory as expected and I found my method the best compromise. Of course I understand that the model is not of much practical use, just a good exercise and the occasion to explore the world of Data Science.
I have also developed a Shiny application where the user can input a sentence and the app will spit out few predicted next words. Thanks to all.