Wednesday, December 17, 2014

Peter Norvig's Spell Checker in Two Lines of Base R

Who does not need a spell checker?



Via Publishable Stuff:

sorted_words <- names(sort(table(strsplit(tolower(paste(readLines("http://www.norvig.com/big.txt"), collapse = " ")), "[^a-z]+")), decreasing = TRUE))
correct <- function(word) { c(sorted_words[ adist(word, sorted_words) <= min(adist(word, sorted_words), 2)], word)[1] }

No comments:

Post a Comment