Created
July 7, 2020 13:25
-
-
Save jacobgarcia/57fab8baf8bf207118c5b841ced48f78 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
//This function counts the occurences | |
function findMostUsedWords(text) { | |
const matches = text.reduce((a, b) => { | |
a[b] = (a[b] + 1 || 1) | |
return a | |
}, {}) | |
const max = Math.max(...Object.values(matches)) | |
return Object.keys(matches).filter((word) => matches[word] === max) | |
} | |
// This function removes the excluded words | |
function removeExcludedWords(text, wordsToExclude) { | |
return text.split(' ').filter((word) => !wordsToExclude.find(wr => wr === word)).map(word => word.toLowerCase()) | |
} | |
function retrieveMostFrequentlyUsedWords(literatureText, wordsToExclude) { | |
const parsedText = literatureText.replace(/[\W_]+/g, ' ') | |
const text = removeExcludedWords(parsedText, wordsToExclude) | |
return findMostUsedWords(text) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment