Home > Software design >  How to search a string for the most common character or word in JavaScript
How to search a string for the most common character or word in JavaScript

Time:03-26

I have a question that asked me to:

Clean the following text and find the most frequent word: "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'"

I used regEx to clean the string like this:

const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find 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';
const sentReg = /\W(?<!1)/g;
let sent = sentence.replace(/ /g, "1");

let finalSent = sent.replace(sentReg, ""), finalfinalSent = finalSent.replace(/1/g, " ");

then I realized I don't know how to (or there might not be a way to) use the match() function to search a string by word, so I tried splitting it up into an array like:

let senArr = finalfinalSent.split(" "), wordOccur = [];

for (const x of senArr) {
    var re = new RegExp(x, "g");
    var y = finalfinalSent.match(re);

    wordOccur = wordOccur.concat([y.length]);
};

...and now I'm stuck because I don't know how to search an array in JavaScript, only in Python, and I feel the way to search through a string would be much easier than this. I would appreciate some pointers.

CodePudding user response:

I wouldn't change the spaces to "1". Instead use a regex that will not remove spaces while cleaning.

Then you can call match on the cleaned string, and use reduce to start counting words and maintain a reference to the most frequent one:

const sentence = '%I $am@% a %tea@cher%, &and& I lo%#ve %te@a@ching%;. The@re $is no@th@ing; && mo@re rewarding than educa@ting &and& @emp%o@weri@ng peo@ple. ;I find 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 word = sentence.replace(/[^\w\s]/g, "")
            .match(/\w /g)
            .reduce((acc, word) => {
                acc[word] = (acc[word] || 0)   1;
                if (!(acc[word] < acc[acc.$])) acc.$ = word;
                return acc;
            }, {}).$;
            
console.log(word);

Observe that acc will be a "dictionary" of words, where the corresponding values are the counts. A special $ entry is created in that same dictionary, which will hold the most frequent word.

  • Related