Skip to content

Instantly share code, notes, and snippets.

@matthandlersux
Last active October 5, 2019 15:37
Show Gist options
  • Save matthandlersux/d5d5933ece968a929ec2c6c2ebdc7e59 to your computer and use it in GitHub Desktop.
Save matthandlersux/d5d5933ece968a929ec2c6c2ebdc7e59 to your computer and use it in GitHub Desktop.

Revisions

  1. matthandlersux revised this gist Oct 5, 2019. No changes.
  2. matthandlersux revised this gist Oct 5, 2019. No changes.
  3. matt handler created this gist Apr 10, 2019.
    41 changes: 41 additions & 0 deletions symspell.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    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));