Skip to content

Instantly share code, notes, and snippets.

@rob137
Created November 30, 2017 09:07
Show Gist options
  • Save rob137/cb1b7a9bdc5767313ec7ec7a045bcdd6 to your computer and use it in GitHub Desktop.
Save rob137/cb1b7a9bdc5767313ec7ec7a045bcdd6 to your computer and use it in GitHub Desktop.
/*
Example lyrics used throughout:
jude = "Hey jude, don't make it bad. Take a sad song and make it better."
jude += " Remember to let her into your heart, Then you can start to make it better."
*/
function getTokens(rawString) {
/*
This function takes the original lyrics and:
1. Changes them to lower case:
"hey, jude, don't make it bad take a sad song and make it better
remember to let her into your heart, then you can start to make it better."
2. Splits lyrics into an array of strings, with an element for each word. It identfies
each word by looking for whitespace, commas, exclamation points (etc - see regex).
["hey", "jude", "don't", "make", "it", "bad", "take", "a", "sad", "song", "and",
"make", "it", "better", "remember", "to", "let", "her", "into", "your", "heart",
"then", "you", "can", "start", "to", "make", "it", "better", ""]
3. Removes any falsy array elements (removes final array element, which was empty quotes).
4. Alphabetically sorts the array.
["a", "and", "bad", "better", "better", "can", "don't", "heart", "her", "hey",
"into", "it", "it", "it", "jude", "let", "make", "make", "make", "remember",
"sad", "song", "start", "take", "then", "to", "to", "you", "your"]
*/
return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
}
function mostFrequentWord(text) {
// See above!
let words = getTokens(text);
/*
The wordFrequencies object is defined in local scope. It will eventually
record each word with the number of times that word appears in the lyrics.
So in our example:
wordFrequencies.bad will be created and paired with 1
wordFrequencies.better will be created and paired with 2.
*/
let wordFrequencies = {};
/*
In plain english: "If a word from the lyrics array isn't already a key
in the wordFrequencies object, add the word as a key with the value 1.
If the word is already a key in wordFrequencies, increase its value by 1."
In our example, we end up with wordFrequencies looking like this:
{
a: 1
and: 1
bad: 1
better: 2
can: 1
don 't: 1
heart: 1
her: 1
hey: 1
into: 1
it: 3
jude: 1
let: 1
make: 3
remember: 1
sad: 1
song: 1
start: 1
take: 1
then: 1
to: 2
undefined: 1
you: 1
your: 1
}
*/
for (let i = 0; i <= words.length; i++) {
if (words[i] in wordFrequencies) {
wordFrequencies[words[i]]++;
} else {
wordFrequencies[words[i]] = 1;
}
}
/*
Makes an array of keys from the wordFrequencies object and assigns the first
element to the local variable currentMaxKey for the time being. So currentMaxKey
now refers to the name of the (alphabetical) first key in wordFrequencies.
So in our example, currentMaxKey === "a";
*/
let currentMaxKey = Object.keys(wordFrequencies)[0];
/*
currentMaxCount is created locally and assigned the value of the first (alphabetical)
key in wordFrequencies.
So in our example currentMaxCount is initially set to 1.
*/
let currentMaxCount = wordFrequencies[currentMaxKey];
/*
Finally we loop through the function until we find the largest number, and then return
the associated key.
Note that in the example, both 'it' and 'make' occur 3 times. The function returns
'it' because this word appears first in Object.keys (which sorts alphabetically).
*/
for (let word in wordFrequencies) {
if (wordFrequencies[word] > currentMaxCount) {
currentMaxKey = word;
currentMaxCount = wordFrequencies[word];
}
}
return currentMaxKey;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment