Last active
October 5, 2019 15:37
-
-
Save matthandlersux/d5d5933ece968a929ec2c6c2ebdc7e59 to your computer and use it in GitHub Desktop.
symspell in js (https://medium.com/@wolfgarbe/1000x-faster-spelling-correction-algorithm-2012-8701fcd87a5f)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { uniq, map, range, zip, mapValues, groupBy, flatMap } = require('lodash'); | |
const expandWordToDeletions = (word) => { | |
const wordArray = word.split(""); | |
const deletionList = map( | |
range(word.length), | |
i => { | |
const copied = [...wordArray]; | |
copied.splice(i, 1); | |
return copied.join(""); | |
} | |
) | |
return [word, ...deletionList]; | |
}; | |
const createMap = (words) => ( | |
mapValues( | |
groupBy( | |
flatMap(words, word => | |
zip(expandWordToDeletions(word), (new Array(word.length + 1)).fill(word)) | |
), | |
([key, value]) => key | |
), | |
listOfKeyValues => map(listOfKeyValues, ([key, value]) => value) | |
) | |
); | |
const search = (word, dictionary) => { | |
if (word in dictionary) return dictionary[word]; | |
else { | |
return uniq( | |
flatMap( | |
expandWordToDeletions(word), | |
deleteWord => dictionary[deleteWord], | |
).filter(Boolean) | |
) | |
} | |
}; | |
const dictionary = createMap(["matt handler", "mutt handler"]); | |
console.log(search("patt handler", dictionary)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment