sentence = `%I $am@% a %tea@cher%, &and& I lo%#ve %tea@ching%;. There $is nothing; &as& mo@re rewarding as educa@ting &and& @emp%o@wering peo@ple. ;I found tea@ching m%o@re interesting tha@n any other %jo@bs. %Do@es thi%s mo@tivate yo@u to be a tea@cher!?`
console.log(sentence.replace(/\W/g,"")); //replace all non-word by ""
console.log(sentence.replace(/%$@&#;/g,"")); //not wor
const cleanTxt=(sentence.toLowerCase().replace(/[%$@&#;?!,.]/g,""));
const word=cleanTxt.split(" ");
console.log(word);
let Word;
let Count;
let frequency={};
let Frequency=[]
for(let w of word){
if(frequency[w])
frequency[w]++;
else
frequency[w]=1;
}
console.log(frequency)
for(let f in frequency){
Word=f;
Count=frequency[f];
Frequency.push({Word,Count})
}
console.log(Frequency)
let sortedwords=Frequency.sort((a,b)=>{
if(a.Count>b.Count)return -1;
if(a.Count<b.Count)return 1;
return 0;
})
console.log(sortedwords)
console.log("3 most frequent words:")
for(let i=0;i<3;i++){
console.log(sortedwords[i])
}