Advertisement
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)
- //this line returns the rawString array and greats a boolean
- //for the symboles in the text.
- .sort();
- }
- function mostFrequentWord(text) {
- let words = getTokens(text);
- let wordFrequencies = {};
- for (let i = 0; i <= words.length; i++) {
- if (words[i] in wordFrequencies) {
- wordFrequencies[words[i]]++;
- } else {
- wordFrequencies[words[i]] = 1;
- }
- }
- //a variable for words was created in line 12 and is given the value of getTokens, in line 14 if the frequent words of (i) exceeds 0 then the words doesn't get a token. unless the wordFrequent is greater than 1 then words get a token.
- let currentMaxKey = Object.keys(wordFrequencies)[0];
- let currentMaxCount = wordFrequencies[currentMaxKey];
- //the variable currentMaxKey and currentMaxCount is given functions.
- for (let word in wordFrequencies) {
- if (wordFrequencies[word] > currentMaxCount) {
- currentMaxKey = word;
- currentMaxCount = wordFrequencies[word];
- }
- }
- return currentMaxKey;
- }
- //if the wordFrequencies is greater than the currentMaxCount then the currentMaxKey gets a token.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement