Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getTokens(rawString) {
- // NB: `.filter(Boolean)` removes any falsy items from an array
- return rawString.toLowerCase().split(/[ ,!.";:-]+/).filter(Boolean).sort();
- }
- function mostFrequentWord(text) {
- let words = getTokens(text);
- let wordFrequencies = {};
- for (let i = 0; i <= words.length; i++) // setting variable of words to defined and word frequencies to a for loop. Increase variable i by 1 if words array element is > words.length
- {
- if (words[i] in wordFrequencies) {
- wordFrequencies[words[i]]++;
- // if a word in the text of wordFrequencies array is used more than once catalog the count else..
- } else {
- wordFrequencies[words[i]] = 1;
- }
- // count a single use as 1
- }
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- //to show with an array the words that occur most frequently
- let currentMaxCount = wordFrequencies[currentMaxKey];
- //to show with an array the count of the currentMaxkey words
- for (let word in wordFrequencies) {
- if (wordFrequencies[word] > currentMaxCount) {
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- // if a word is greater than the currentmaxcount the current max key will be the new word and the new count will be the count of the new word?
- }
- }
- return currentMaxKey;
- //this will return the array of the current max words with name and count
- }
Add Comment
Please, Sign In to add comment