Skip to content

Instantly share code, notes, and snippets.

@marceloszilagyi
Created September 9, 2017 00:14
Show Gist options
  • Save marceloszilagyi/969034b810899dbee6cee72ab42d1d5b to your computer and use it in GitHub Desktop.
Save marceloszilagyi/969034b810899dbee6cee72ab42d1d5b to your computer and use it in GitHub Desktop.
Backoff logic
if (lengthtext >4) {fivechance = fivegrams %>% filter(third_next_word==word_search_last, sec_next_word == word_search_last_minus_one, next_word == word_search_last_minus_two, word == word_search_last_minus_three) %>% top_n(5,wt = fiveprop) %>% mutate(chance = fiveprop*0.4^(backoff_index-4), origin="fivegrams") %>% ungroup() %>% select(selection = fourth_next_word, chance, origin)}
if (lengthtext >3) {fourchance = fourgrams %>% filter(sec_next_word==word_search_last, next_word==word_search_last_minus_one, word==word_search_last_minus_two) %>% top_n(5, wt=fourprop) %>% mutate(chance = fourprop*0.4^(backoff_index-3), origin = "fourgrams") %>% ungroup() %>% select(selection = third_next_word, chance, origin)}
if (lengthtext >2) {threechance = trigrams %>% filter(next_word==word_search_last,word==word_search_last_minus_one) %>% top_n(5, wt=triprop) %>% mutate(chance = triprop*0.4^(backoff_index-2), origin = "trigrams") %>% ungroup() %>% select(selection = sec_next_word, chance, origin)}
if (lengthtext >1) {bichance = bigrams %>% filter(word==word_search_last) %>% top_n(5, wt=biprop) %>% mutate(chance = biprop*0.4^(backoff_index-1), origin = "bigrams") %>% ungroup() %>% select(selection = next_word, chance, origin)}
if (lengthtext == 1) {unichance = unigrams %>% top_n(5, uniprop) %>% mutate(chance = uniprop*0.4^(backoff_index-1), origin = "unigram") %>% ungroup() %>% select(selection = word, chance, origin)}
chance_final <- rbind(if(exists("fivechance")){fivechance},
if(exists("fourchance")){fourchance},
if(exists("threechance")){threechance},
if(exists("bichance")){bichance},
if(exists("unichance")){unichance})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment