+ 1
Please help me with an assignment, I have no clue on how to check for the most frequent word in a sentence.
The assignment requires that I use ".replace() method and regular expression" to do the assessment using Javascript. I would also appreciate if you can provide alternative solution to solve it without using ". replace () method." Thank you so much as you continue to help me https://code.sololearn.com/WaihOmy11L4O/?ref=app
5 Answers
+ 2
See if this does what you want.
I used replace to remove all punctuation characters. You asked to find most frequent word but multiple words could match the same maximum frequency so the following prints an Array of words repeated that maximum frequency.
The following uses some ES6 features so it won't work in a browser older than around 2016 or 2017.
const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; &as& mo@re rewarding as educa@ting &and& @emp%o@weri@ng peo@ple. ;I found tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching'
/*pls i need help on how i can check for the most frequent word using ".replace() and regular expression"*/
let cleanSentence = sentence.replace(/[^\w\s]|_/g, ""); // punctuation removed
let words = cleanSentence.split(/\s/); // Array of words. Any whitespace is a delimiter.
let wordFrequencies = new Map();
words.forEach(function(word) {
if (!wordFrequencies.has(word))
wordFrequencies.set(word, 1);
else
wordFrequencies.set(word, wordFrequencies.get(word)+1);
});
// Now wordFrequencies maps each unique word to a frequency count.
let maxFrequency = Math.max(...wordFrequencies.values()); // Find max frequency of any word.
let wordsMatchingMaxFrequency = Array.from(wordFrequencies.keys()).filter((word) => {
return wordFrequencies.get(word) === maxFrequency;
});
console.log(wordsMatchingMaxFrequency);
+ 1
Okafor wrote, "Pls I will like you to explain some parts of your code. I will send the specific parts to you if I still fail to understand them."
Response:
That code has fairly descriptive variable names and comments so I'm not sure what specific parts could be unclear. It might make sense more if you try to rewrite each step and console.log the corresponding variables to closely examine what each part does.
For example, here I print out the values of a couple variables:
let cleanSentence = sentence.replace(/[^\w\s]|_/g, ""); // punctuation removed
console.log('cleanSentence = ' + cleanSentence);
let words = cleanSentence.split(/\s/); // Array of words. Any whitespace is a delimiter.
console.log('words = ' + JSON.stringify(words));
You could do similar for every other step in the code.
0
Josh Greig, sir I so much appreciate your contributions in seeing that I enjoy learning Javascript.
Pls I will like you to explain some parts of your code. I will send the specific parts to you if I still fail to understand them. For now, let me study your code like my textbook ( you are really advance in Javascript).
once again thank you so much.
0
You are absolutely right. Like I told you before, please sir don't worry about whether i understand your solutions at the moment or not, I will definitely understand it later. Today I learnt about Map() and Set(), I checked your code, is clearly understandable. I failed to understand it then because I lacked the prerequisite knowledge for me to understand it. I am a million times grateful for your gentility and kindness.
0
const sentence =
'%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; &as& mo@re rewarding as educa@ting &and& @emp%o@weri@ng peo@ple. ;I found tea@ching m%o@re interesting tha@n any ot#her %jo@bs. %Do@es thi%s mo@tiv#ate yo@u to be a tea@cher!? %Th#is 30#Days&OfJavaScript &is al@so $the $resu@lt of &love& of tea&ching';
let reg = /\W+/g;
let replaceChar = " ";
let result = sentence.replace(reg,replaceChar)
console.log(result)