A Swift port of Faroo's symmetric delete spelling correction algorithm.
With the advent of iOS 8, Apple gave developers the ability to create third-party keyboards, an announcement which was met with much initial excitement. And yet, with little to no features and a somewhat unstable API, elation at the news quickly died down.
SymSpell is an attempt at providing iOS developers with a fast, working and language-independent add-on for their keyboard extensions at no cost to themselves. It is a direct port of Faroo's symmetric delete algorithm which reduces dictionary lookup times of 'suggested words' for a given Damerau-Levenshtein distance by focusing solely on 'deletes'. As a result, it is six orders of magnitude faster than most standard approaches and language-independent; this comes at a cost of increased memory usage and build time during initial pre-calculation (i.e. 11K words in 14.3 seconds with an edit distance of 2 on an iPhone 6s).
With the appropriate corpus or data, the algorithm has a wide variety of applications such as in DNA matching and gene analysis, etc. (ResearchKit anyone?)
- Import a corpus of your choice into Xcode as a
let autocorrect = Symspell(editDistance: 2, verbose: 1)
- Create the dictionary
autocorrect.createDictionary("corpus", language: "en").
autocorrect.correct("autcrorct", language: "en")
- Adding words to the dictionary is simple, just run:
autocorrect.createDictionaryEntry("Hippopotomonstrosesquippedaliophobia", language: "en")
- For the same calculated Damerau-Levenshtein edit distance, words that appear with a higher frequency in the corpus will be suggested first.
- Make sure to import 'MDCDamerauLevenshtein.h' in a bridging header.
- Keyboard extensions cannot handle memory intensive processes: dictionary should be built in the container app once, cached, and passed on to the extension.