2024-03-23

Stupid Back-Off Algorithm

  • First proposed by Brants, et.al in 2007, using this scoring function:

  • Uses a parameter called alpha to discount “scores” instead of smoothing conditional probabilities
  • Scoring follows the overall structure proposed by Brants, et.al. and summarized succinctly by Ferriere in 2016:
if (candidateIs5gram) {
  score = matched5gramCount / input4gramCount
} else if (candidateIs4gram) {
  score = 0.4 * matched4gramCount / input3gramCount
} else if (candidateIs3gram) {
  score = 0.4 * 0.4 * matched3gramCount / input2gramCount
} else if (candidateIs2gram) {
  score = 0.4 * 0.4 * 0.4 * matched2gramcount / input1gramCount
}

Data Gathering/Cleaning Choices

  • Used a 2% random sample from each provided source–twitter, blogs, and news–varied writing styles

  • Removed various artifacts including non-English characters, punctuation, hashtags, emails, urls, etc:

corpus<-iconv(rawdata, "latin1", "ASCII", sub="")
library(stringr)
corpus<-str_remove(corpus, "^#.+") #remove hashtags
corpus<-str_remove(corpus, "^@.+") #remove mentions from Twitter
corpus<-str_remove(corpus, "^.+@.+") #remove emails that got missed
corpus<-tolower(corpus) #lowercase everything
corpus<-str_remove(corpus,"^.*[0-9]+.+")
library(tm)
corpus<-removePunctuation(corpus)

Efficiency Measures

  • Senses how long the input is–puts input into algorithm at the necessary level to avoid unnecessary calculations (below is a simplified framework)
l<-length(unlist(input_token))
    if (l>3){
      #load all ngram tables, run "inputisfourgram" function
    }else if (l==3){
      #load ngram tables 1-4, run "inputistrigram" function
    }else if (l==2){
      #load ngram tables 1-3, run "inputisbigram" function
    }else if (l==1){
      #load ngram tables 1 and 2, run "inputisunigram" function
    }
  • Only loads what ngram tables are necessary to save on space

Important Features

  • Using alexlusco’s Compositr package, input from the user is spell-checked, and best guesses for the correct word replace the original

  • Waiting message when calculating next word (“Reading the auguries”)

  • Brief explanation of algorithm and Stupid Back-Off score for best answer in other tabs