Created
July 28, 2018 12:10
-
-
Save tankala/d1929ebd0a1e9aedef65fdd46b9c4ffc to your computer and use it in GitHub Desktop.
Word Wizard for calculating words count with business logic flaw
This file contains hidden or 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 stopWords = ["a", "about", "above", "after", "again", "against", "all", "am", "an", "and", "any", "are", "as", "at", "be", "because", "been", "before", "being", "below", "between", "both", "but", "by", "could", "did", "do", "does", "doing", "down", "during", "each", "few", "for", "from", "further", "had", "has", "have", "having", "he", "he'd", "he'll", "he's", "her", "here", "here's", "hers", "herself", "him", "himself", "his", "how", "how's", "i", "i'd", "i'll", "i'm", "i've", "if", "in", "into", "is", "it", "it's", "its", "itself", "let's", "me", "more", "most", "my", "myself", "nor", "of", "on", "once", "only", "or", "other", "ought", "our", "ours", "ourselves", "out", "over", "own", "same", "she", "she'd", "she'll", "she's", "should", "so", "some", "such", "than", "that", "that's", "the", "their", "theirs", "them", "themselves", "then", "there", "there's", "these", "they", "they'd", "they'll", "they're", "they've", "this", "those", "through", "to", "too", "under", "until", "up", "very", "was", "we", "we'd", "we'll", "we're", "we've", "were", "what", "what's", "when", "when's", "where", "where's", "which", "while", "who", "who's", "whom", "why", "why's", "with", "would", "you", "you'd", "you'll", "you're", "you've", "your", "yours", "yourself", "yourselves"]; | |
exports.getWordsCountInDesc = function (sentence, callBack) { | |
let words = getWordsFromSentence(sentence); | |
let wordsWithCount = getWordsWithCount(words); | |
let wordsWithCountInDesc = getWordsWithCountInDescendingOrder(wordsWithCount); | |
callBack(null, wordsWithCountInDesc); | |
} | |
//Sort words in descending order based on counts | |
var getWordsWithCountInDescendingOrder = function (wordsWithCount) { | |
return Object.keys(wordsWithCount) | |
.sort((word, anotherWord) => wordsWithCount[anotherWord] - wordsWithCount[word]) | |
.reduce((wordsWithCountInDesc, word) => { | |
wordsWithCountInDesc[word] = wordsWithCount[word]; | |
return wordsWithCountInDesc; | |
}, {}); | |
} | |
// Gives words array after convering to lower case replacing characters like comma, full stop & etc | |
var getWordsFromSentence = function (sentence) { | |
sentence = sentence.toLowerCase(); | |
sentence = sentence.replace('/./g', ' '); | |
sentence = sentence.replace('/,/g', ' '); | |
sentence = sentence.replace('/ /g', ' '); | |
sentence = sentence.trim(); | |
let words = sentence.split(' '); | |
return words; | |
} | |
// Returns words with count after removing stop words | |
var getWordsWithCount = function (words) { | |
let wordsWithCount = {}; | |
words.forEach(word => { | |
if (!isItStopWord(word)) { | |
if (wordsWithCount[word]) { | |
wordsWithCount[word] += 1; | |
} else { | |
wordsWithCount[word] = 1; | |
} | |
} | |
}); | |
return wordsWithCount; | |
} | |
//Returns wether word is a stop word or not | |
var isItStopWord = function (word) { | |
return stopWords.includes(word); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment