How can i show top 5 most frequently encountered words from the array of "bad words" in user input string
I have an array with bad words and function, with find it:
let words = textWords.split(' ');
console.log('words', words)
let badWords = listOfBadWords.join('|');
let regex = new RegExp(badWords, 'gi');
let matche开发者_运维技巧s = words.reduce((acc, word) => {
if (word.match(regex)) {
let match = word.toLowerCase();
acc[match] = (acc[match] || 0) + 1;
}
return acc;
}, {});
let sortedMatches = Object.entries(matches).sort((a, b) => b[1] - a[1]);
let top5 = sortedMatches.slice(0, 5).map(m => m[0]);
console.log(sortedMatches, top5); // in format [['word', numberOfUsing], ....]
How can i make the output like top 5 words most useable in format: word - numberOfUsing
I personally wouldn't use regular expression here. I'd create an object to count the words with from the badWords array and simply iterate the user's words to increase the counter. Something like that:
const badWords = ['cunt', 'shit', 'idiot', 'motherfucker', 'asshole', 'dickhead', 'slut', 'prick', 'whore', 'wanker'];
const userText = "I met this idiot today, he did behave like an asshole. This idiot treated me like a slut. Can't believe this idiot made me feeling like a cunt. Such an asshole, really. I tried to speak with this asshole, but he was just a dickhead.";
const counterObject = badWords.reduce((acc, curr) => {
acc[curr] = 0;
return acc;
}, {});
userText.split(/[\s,\.]/).forEach(
token => {
if (token in counterObject) counterObject[token]++;
}
);
const topWords = Object.entries(counterObject).sort((a, b) => b[1] - a[1]).slice(0, 5);
console.log(topWords);
I would do it like this
const listOfBadWords = ['is', 'or'];
const badWordsFound = [];
const userString = 'This is a test, works or not, is to be seen';
for(i = 0; i < listOfBadWords.length; i++){
const matches = (userString.match(new RegExp(listOfBadWords[i], 'g')) || []).length;
if(matches)
badWordsFound.push([listOfBadWords[i], matches]);
}
const sorted = badWordsFound.sort((a, b) => b[1] - a[1]);
console.log(sorted);
精彩评论