The Capstone

Rob Davidson
December 13, 2014

Objective

To build a SwiftKey-like Shiny application that predicts the next word based on the words the user has entered.

  • Built using three data sets: News, Blogs and Twitter
  • Fits in default Shiny app size of 256 MB
  • Responds quickly with little to no user waiting
getPred<-function(ngram) {
    Key<-spooky.32(ngram)
    if (!(is.na(collisionTable[.(Key)]$n1gram[1]))) {return(getCollision(ngram))}
    return(finalTable[.(Key)]$predicted)
}

Features

  • Profanity filter: over 350 words and phrases removed
  • Smart Proper Pronoun Handling: Multi-word names identified and handled
  • Hashed Keys for Quick Retrieval with Collision Management: Keys for access hashed using spooky.32 algorithm. Collisions for known ngrams identified and correctly resolved

alt text

Performance

  • Model trained on 90% of each data set
  • App built for speed sacrificing accuracy for response time
  • Able to correctly predict over 10% of randomly selected ngrams from data sets
  • 3-grams and 4-grams with only one occurrence removed from model. This reduced the size of model 10-fold but drastically reduced accuracy.

    text<-removeWords(text,profane)
    text<-removeWords(text,stopwords("english"))
    text<-gsub("(http)[^ ]*"," ",text)
    text<-gsub("(www)[^ ]*"," ",text)
    text<-gsub("\\w*\\.(com)"," ",text)
    

Lessons Learned

  • 90,000 profane words/phrases found in Twitter data set
  • Would have preferred to use hash tables but because R uses environments to implement hashes, they produce too much overhead for basic shinyapps.io
  • data.table package offers a wonderful option for R objects providing keyed retrieval
  • Wasted a great deal of time working with Term/Document Matrices and running into resource limits on my computer - ended up expanding my old Macbook Pro to 8GB ram