swati-jagtap / javascript-practice

0 stars 0 forks source link

12.3.1-- Write a function which cleans text. Clean the following text. #148

Open swati-jagtap opened 3 years ago

swati-jagtap commented 3 years ago

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])
}

image image image