javascript/ arrays/ string

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, " ");

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.

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.

If there is more than one word that has the maximum frequency, and you want to get all of these words, not just one, then return an array instead of a string:

 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]. else if (acc[word] === acc[acc.$]) acc;$;push(word), return acc. }; {}).$; console.log(word);

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Finding the most common word in Javascript Search word in string with javascript search for a character in a string with javascript How to search for special character in a string in javascript? JavaScript | Returning multiple characters that is most common in a string Using Javascript to find most common words in string? Search a string for any occurence of a word in a list of strings and higlight matched word if first character of word matches.Javascript regex Javascript Search word in String not working How to add a character to the beginning of every word in a string in javascript? How to remove a word that starts with a certain character from a string in Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM