Skip to content

Instantly share code, notes, and snippets.

@jacobgarcia
Created July 7, 2020 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobgarcia/57fab8baf8bf207118c5b841ced48f78 to your computer and use it in GitHub Desktop.
Save jacobgarcia/57fab8baf8bf207118c5b841ced48f78 to your computer and use it in GitHub Desktop.
//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