This report summarises the exploratory analysis of the HC Corpora dataset — a large collection of English text from three sources: Twitter, News, and Blogs. The goal is to understand the structure of the data and lay the groundwork for building a next-word prediction algorithm.
The corpus was downloaded from the Coursera / SwiftKey dataset. It contains three plain-text files in English.
| Source | Size (MB) | Lines | Avg Line Length | |
|---|---|---|---|---|
| 167.1 | 2,360,148 | 71 | ||
| News | News | 205.8 | 1,010,242 | 204 |
| Blogs | Blogs | 210.2 | 899,288 | 234 |
Key observations:
After cleaning the text (lowercasing, removing punctuation and numbers), we built a frequency table of all unique words across a 5% sample of the corpus.
The most common words are stop words (the, and, to, a, of). These are kept in the model because they are essential for natural language prediction — “I want to”, “thanks a lot”.
This follows Zipf’s Law: a small number of words appear very frequently, while most words are rare. The top 100 words cover roughly 50% of all word occurrences in the corpus.
N-grams are sequences of consecutive words. They form the backbone of our prediction model — if someone types “happy new”, the most common next word in “happy new ___” trigrams is the prediction.
Common trigrams like “one of the” and “a lot of” reflect natural patterns in English that the model will learn to predict reliably.
How many unique words are needed to cover most of the text?
| Coverage Target | Unique Words Needed |
|---|---|
| 50% of all text | 139 |
| 90% of all text | 6,954 |
Only 6,954 unique words are needed to cover 90% of all text. This allows us to build an efficient, compact model that doesn’t require every word in the dictionary.
The model predicts the next word by searching pre-built frequency tables:
| Step | What it does |
|---|---|
| 4-gram | Look up the last 3 words typed |
| 3-gram | If no match, look up the last 2 words |
| 2-gram | If no match, look up the last 1 word |
| Fallback | Return the most common word in the corpus |
This is called Stupid Backoff — it is fast, memory-efficient, and works well in practice for common English phrases.
Live app: https://rameez-captone-project.shinyapps.io/NextWordPrediction/