Created
July 14, 2016 14:29
-
-
Save athaeryn/c0d94d5dbbd51d828b27a525dcfaa81c 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 fs = require('fs') | |
const words = fs.readFileSync('/usr/share/dict/words') | |
.toString() | |
.toLowerCase() | |
.split('\n') | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('') | |
let lookup = | |
alphabet.reduce((obj, letter) => { | |
obj[letter] = alphabet.reduce((obj, letter) => { | |
obj[letter] = {} | |
return obj | |
}, {}) | |
return obj | |
}, {}) | |
words.forEach(function insert (word) { | |
if (word.length < 2) return | |
const first = word[0] | |
const number = word.length - 2 | |
const last = word[word.length - 1] | |
if (lookup[first][last][number]) { | |
lookup[first][last][number].push(word) | |
} else { | |
lookup[first][last][number] = [ word ] | |
} | |
}) | |
function sample (coll) { | |
return coll[~~(Math.random() * coll.length)] | |
} | |
function splitToParts (word) { | |
const lastIndex = word.length - 1 | |
const first = word[0] | |
const middle = word.substring(1, lastIndex) | |
const last = word[lastIndex] | |
return [first, middle, last] | |
} | |
function numeronymize (word) { | |
return splitToParts(word).join('') | |
} | |
function denumeronymize (numeronym) { | |
const [ first, middle, last ] = splitToParts(numeronym) | |
const number = middle.length | |
return lookup[first][last][number] | |
} | |
function telephone (word) { | |
return sample(denumeronymize(numeronymize(word))) | |
} | |
console.log(telephone('iphone')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment