Created
January 11, 2022 14:05
-
-
Save farleyta/34a2478dfa6bf3329b880d35316fa9a3 to your computer and use it in GitHub Desktop.
Analyze the most common letters in five-letter words vs all words. Satisfies some curiosity that arose while playing Wordle.
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
// Webster's Unabridged English Dictionary from: | |
// https://github.com/adambom/dictionary | |
const dictionary = require("./dictionary.json"); | |
const alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
const fiveLetters = Object.keys(dictionary).filter(word => word.length === 5); | |
// console.log(alphabet.length); | |
// console.log(fiveLetters.length); | |
const alphabetDict = alphabet.split('').reduce((dict, letter) => { | |
dict[letter] = 0; | |
return dict; | |
}, {}); | |
// console.log(alphabetDict); | |
// counts the number of letters in each word and adds to the overall alphabet count | |
const getDistribution = words => { | |
return words.reduce((dict, word) => { | |
word.toLowerCase().split('').forEach(letter => { | |
if (dict[letter] !== undefined) { | |
dict[letter] = dict[letter] + 1; | |
} | |
return dict; | |
}); | |
return dict; | |
}, alphabetDict); | |
} | |
// console.log(distribution); | |
// Sorts by most common and limits to 10 letters | |
const getDistributionSorted = words => { | |
const distribution = getDistribution(words); | |
return Object.keys(distribution).sort((a, b) => { | |
return distribution[b] - distribution[a]; | |
}).reduce((acc, letter) => { | |
if (Object.getOwnPropertyNames(acc).length < 10) { | |
acc[letter] = distribution[letter]; | |
} | |
return acc; | |
}, {}); | |
} | |
console.log('Five Letters: '); | |
console.log(getDistributionSorted(fiveLetters)); | |
console.log('All Letters: '); | |
console.log(getDistributionSorted(Object.keys(dictionary))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment