1. Introduction

The objective of this project is to explore the SwiftKey text corpus and understand word frequencies, common word combinations and vocabulary patterns.

This analysis will help prepare the data for building a predictive text application.

2. Dataset Overview

The analysis uses three English-language datasets:

The original datasets contain the following number of lines:

Source Original lines Approx. file size Sampled documents
Blogs 899,288 200.4 MB 10,000
News 77,259 196.3 MB 10,000
Twitter 2,360,148 159.4 MB 10,000

A sample of 10,000 documents was selected from each dataset to reduce memory usage. The analysis therefore uses 30,000 sampled documents in total.

3. Load Required Packages

4. Load Sampled Datasets

## [1] 10000
## [1] 10000
## [1] 10000

5. Combine the Datasets

## [1] 30000     3

Dataset Word Counts

## # A tibble: 3 × 4
##   source  documents total_words average_words_per_document
##   <chr>       <int>       <int>                      <dbl>
## 1 blogs       10000      414499                       41.4
## 2 news        10000      345925                       34.6
## 3 twitter     10000      127043                       12.7

The sample contains 30,000 documents and approximately 887,467 words, based on the word-count calculation. Blogs have the highest average document length (41.45 words), followed by news (34.59 words) and Twitter (12.70 words). This reflects differences in content and writing style across the three sources.

6. Tokenisation

## [1] 1047294       3

7. Word Frequency Analysis

##    token     n
## 1    the 43723
## 2     to 24252
## 3    and 22874
## 4      a 21607
## 5     of 18725
## 6     in 15012
## 7      i 13273
## 8   that  9385
## 9    for  9343
## 10    is  9201
## 11    it  7854
## 12    on  6854
## 13   you  6585
## 14  with  6402
## 15   was  5849
## 16  this  4879
## 17    as  4799
## 18    at  4746
## 19    be  4699
## 20    my  4597

8. Word Frequency Distribution

# 9. Bigram Analysis

##      bigram    n
## 1    of the 4122
## 2    in the 3760
## 3    to the 1935
## 4   for the 1712
## 5    on the 1689
## 6     to be 1476
## 7   and the 1243
## 8    at the 1229
## 9      in a 1104
## 10 with the  956
## 11   it was  939
## 12     is a  919
## 13 from the  853
## 14    for a  805
## 15    and i  782
## 16   with a  775
## 17    i was  772
## 18    it is  771
## 19     of a  715
## 20     as a  713

10. Top 20 Bigrams

# 11. Trigram Analysis

##           trigram   n
## 1      one of the 344
## 2        a lot of 294
## 3         to be a 170
## 4      out of the 150
## 5     some of the 145
## 6      as well as 144
## 7      the end of 138
## 8        it was a 136
## 9     going to be 135
## 10     be able to 115
## 11 thanks for the 115
## 12      i want to 113
## 13    part of the 101
## 14    a couple of  99
## 15      this is a  97
## 16    it would be  89
## 17       i have a  88
## 18 the first time  88
## 19     end of the  86
## 20  for the first  84

12. Top 20 Trigrams

# 13. Vocabulary Coverage

##   unique_words words_for_50 words_for_90
## 1        48146          144         7223

14. Vocabulary Coverage Graph

15. Dictionary-Based Analysis

##   unrecognised_unique_words total_occurrences percentage_of_occurrences
## 1                     19840             53571                  6.131896

16. Vocabulary Optimisation

##   original_vocabulary stemmed_vocabulary vocabulary_reduction
## 1               48146              33101                15045
##   reduction_percentage
## 1              31.2487

17. Key Findings

The exploratory analysis produced the following findings:

  1. The study analysed 30,000 sampled documents from blogs, news and Twitter.
  2. Tokenisation generated 1,047,294 tokens.
  3. The word vocabulary contained 48,146 unique words..
  4. The most frequent word was “the”, appearing 43,723 times.
  5. The most frequent bigram was “of the”, appearing 4,122 times.
  6. The most frequent trigram was “one of the”, appearing 344 times.
  7. Only 144 unique words accounted for approximately 50% of word occurrences.
  8. Approximately 6.13% of word occurrences were unrecognised by the English dictionary.
  9. Stemming reduced the vocabulary size by approximately 31.25%.

18. Limitations

19. Prediction Algorithm and Shiny Application Plan

The next stage will involve developing a predictive text model using n-gram language modelling.

The planned approach includes:

  1. Building unigram, bigram and trigram frequency tables from the cleaned text data.
  2. Predicting the next word based on the most recent words entered by the user.
  3. Using a backoff approach to shorter n-grams when a longer word combination is not available.
  4. Evaluating prediction accuracy using a separate test dataset.
  5. Optimising the model for memory usage and response speed.

A Shiny application will be developed to provide an interactive interface. Users will enter text, and the application will display suggested next words based on the prediction model.

20. Conclusion

This exploratory data analysis provided an understanding of word frequencies, common word combinations and vocabulary patterns in the SwiftKey corpus.

The analysis identified frequently occurring words, bigrams and trigrams. Vocabulary coverage showed that a relatively small number of common words account for a large proportion of word occurrences.

Dictionary-based analysis and stemming also provided insights into vocabulary characteristics and possible optimisation techniques.

These findings will support the next stage of the project: developing a predictive text model using n-grams to suggest the next word based on previously entered words.