This milestone explores the three US English text files supplied for the Coursera Data Science Capstone and turns the findings into a practical plan for a next-word prediction product. The corpus contains blogs, news articles and Twitter posts, giving the model a useful mix of long-form, edited and conversational English.
The source is the Coursera SwiftKey US English corpus. Lines represent newline-delimited text records. Word totals use whitespace-delimited tokens as a transparent baseline; a production tokenizer may give slightly different totals because of contractions, punctuation, links and emojis.
| Source | File | Size (MB) | Lines | Words | Average words per line |
|---|---|---|---|---|---|
| Blogs | en_US.blogs.txt | 200.42 | 899,288 | 37,334,114 | 41.5 |
| News | en_US.news.txt | 196.28 | 1,010,242 | 34,372,520 | 34.0 |
| en_US.twitter.txt | 159.36 | 2,360,148 | 30,373,543 | 12.9 |
Across all sources there are 4,269,678 lines, 102,080,177 words and approximately 556.06 MB of text.
The raw text will be processed in reproducible, memory-aware batches:
Read each source in UTF-8 and draw a fixed-seed stratified training sample, with separate validation and test sets.
Convert text to lowercase while retaining apostrophes inside contractions.
Replace or remove URLs, email addresses, user handles, repeated punctuation, numbers and unsupported symbols.
Remove profanity from candidate predictions without deleting surrounding training context.
Tokenise sentences and count unigrams, bigrams, trigrams and four-grams.
Prune rare n-grams, store compact lookup tables and record source-level diagnostics.
A held-out test set will never be used to construct the frequency tables. This keeps the reported accuracy honest.
When a user enters a phrase, the predictor will clean it using the same rules as the training data and inspect the final words. It will first search for phrases that match the last three words. If no reliable match exists, it will back off to the last two words, then the last one. Candidate next words will be ranked using frequency, backoff weight and a small probability discount so that common phrases are preferred without letting one source dominate.
| Step | Action |
|---|---|
| 1 | Normalise the user’s phrase |
| 2 | Extract the last one to three words |
| 3 | Search four-gram, trigram and bigram tables |
| 4 | Back off and rank candidate next words |
| 5 | Return the highest-ranked safe word |
The Shiny app will contain one text box, a Predict next word button and a clearly highlighted result. The model will load once when the application starts, so each prediction requires only a small table lookup. Optional ranked alternatives can be added after the core one-word output is reliable.
Performance will be evaluated on:
The corpus reflects public web writing rather than every type of English. It may contain bias, slang, personal information and offensive language. Source balancing, profanity filtering and careful validation will reduce these risks, but they cannot remove them completely.
The next steps are to run the cleaning pipeline on a stratified sample, build and prune the n-gram tables, tune the backoff weights on validation data, measure top-one accuracy, and deploy the smallest accurate model in Shiny.
The exploratory analysis shows that the three sources differ sharply in record length and contribution. A source-balanced, backoff n-gram model is therefore a practical design: it is explainable, fast enough for an interactive Shiny application and small enough to deploy after pruning.