Last active
February 1, 2021 05:12
-
-
Save shalvah/be36d03f51cb931f3d46ce3f23efbe98 to your computer and use it in GitHub Desktop.
This file contains 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 wordList = { | |
sacrilege: {popularity: 0.5207411547}, | |
sarcasm: {popularity: 0.1913595276}, | |
said: {popularity: 0.736293578}, | |
sardine: {popularity: 0.2439570583}, | |
sars: {popularity: 0.6371759558}, | |
saw: {popularity: 0.1695272778}, | |
say: {popularity: 0.8312312658}, | |
scare: {popularity: 0.3623531581}, | |
sea: {popularity: 0.6786234}, | |
seal: {popularity: 0.5311880343}, | |
sear: {popularity: 0.2651375578}, | |
search: {popularity: 0.1371922207}, | |
seen: {popularity: 0.04845264854}, | |
shalvah: {popularity: 0.3734743679}, | |
shepard: {popularity: 0.2800899325}, | |
shrink: {popularity: 0.4796703544}, | |
simple: {popularity: 0.965004213}, | |
simpleton: {popularity: 0.5028959222}, | |
sinister: {popularity: 0.3120875969}, | |
sin: {popularity: 0.5613698484}, | |
soon: {popularity: 0.3977505837}, | |
soothe: {popularity: 0.5668864284}, | |
smosh: {popularity: 0.8812294656}, | |
substitute: {popularity: 0.767571411}, | |
subtraction: {popularity: 0.4938204045}, | |
}; | |
var TrieSearch = require('trie-search'); | |
var ts = new TrieSearch(); | |
ts.addFromObject(wordList); | |
function getAutoCorrectSuggestions(input) { | |
let corrections = []; | |
const characters = [...input]; | |
let substring = ""; | |
let indexToStopAt = characters.length; // Start with whole word, minus the last letter... | |
while (corrections.length === 0 && indexToStopAt > 1) { // ...and keep going backwards if we don't find any corrections | |
indexToStopAt--; | |
substring = input.substring(0, indexToStopAt); | |
console.log("Working with substring " + substring) | |
// This will return all words starting with the substring | |
corrections = corrections.concat(ts.get(substring)); | |
} | |
return corrections; | |
} | |
getAutoCorrectSuggestions("simper"); | |
// Some things we could improve on: | |
// - Ranking by popularity | |
// - Rank by character proximity on user's keyboard layout | |
// - Provide corrections for positions other than the end | |
// - Support variable length corrections (eg payback -> playback and vice versa) | |
// - Specify max length of corrections |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment