JavaScript simple profanity filter - Stack Overflow

Hi I want to create very basic profanity filter in JavaScript.I've an array called badWords and al

Hi I want to create very basic profanity filter in JavaScript.

I've an array called badWords and also I've constant called description. I won't to check whether there is any bad word contains in that description.

This is what I've done upto now.

const badWords = ["Donald Trump","Mr.Burns","Sathan"];

const description = "Mr.Burns entered to the hall."
let isInclude = false;
badWords.forEach(word=>{
  if(description.includes(word)){
  isInclude = true
  }
})

console.log(`Is include`,isInclude)

Hi I want to create very basic profanity filter in JavaScript.

I've an array called badWords and also I've constant called description. I won't to check whether there is any bad word contains in that description.

This is what I've done upto now.

const badWords = ["Donald Trump","Mr.Burns","Sathan"];

const description = "Mr.Burns entered to the hall."
let isInclude = false;
badWords.forEach(word=>{
  if(description.includes(word)){
  isInclude = true
  }
})

console.log(`Is include`,isInclude)

Only problem is I've to loop through badWords array. Is there a way to get this done without looping through the array?

Share Improve this question asked Jun 5, 2019 at 7:42 margherita pizzamargherita pizza 7,18529 gold badges102 silver badges178 bronze badges 9
  • 4 Why would looping over several values of a dataset be a “problem”? – 04FS Commented Jun 5, 2019 at 7:44
  • 4 And …? Are you just theoretically concerned about performance (hint: don’t), or do you have any actual indication that this makes things measurably or noticeable slow? – 04FS Commented Jun 5, 2019 at 7:50
  • 4 You can shortcut this iteration as soon as you have found one match. Use .some instead of .forEach – deceze Commented Jun 5, 2019 at 7:51
  • 1 It looks like you're really considering the performance of a large number of bad words (strings). In which case, I'd suggest looking at regex (regular expressions) since 1 regular expression could do the same task of more than one (hopefully many more) of those bad words. I use regex101. for much of my own regex work, and its a valuable learning tool. – John Dunne Commented Jun 5, 2019 at 7:58
  • 2 @JohnDunne To add emphasis to JohnDunne said, regex can also help catch variation of the "bad" words better, so they won't have to be multiple items on the list. – aviya.developer Commented Jun 5, 2019 at 8:00
 |  Show 4 more ments

5 Answers 5

Reset to default 6

Use some() - it exits from the loop as soon as a match to the condition is found, as such it's more performant than forEach.

let isInclude = badWords.some(word => description.includes(word));

Here's what the regexp solution looks like:

// https://stackoverflow./a/3561711/240443
const reEscape = s => s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');

// needs to be done only once
const badWords = ["Donald Trump","Mr.Burns","Sathan"];
const badWordsRE = new RegExp(badWords.map(reEscape).join('|'));

// testing is quick and easy
console.log("Mr.Burns entered to the hall.".match(badWordsRE)); // "Mr.Burns"
console.log("Nothing objectionable".match(badWordsRE));         // null

(If your bad words are actual regexps, like "Mr\.Burns", then leave out the .map(reEscape))

this function i made turns the bad word into **** and if the bad word is 5 letters it will put 5 *'s

(this was made for my chat i coded)

function filter(chat) {
    var cusswords = ["badword1", "badword2"]
    var chat; // define the chat var which is the function parameter
    for(i=0; i < cusswords.length; i++) {
        var length = String(cusswords[i]).length;
        var characters = "";
        for(u=0; u < length; u++) {
            characters = String(characters) + String("*");
        }
        chat = chat.replace(String(cusswords[i]), String(characters));
    }
    console.log(chat)
    return chat;
} 

then you just use a var equal to the text you want to filter in the filter() function.

var cleanChat = filter(chat);

// send(cleanChat) then send it with another function. 

this function first splits sentence with space and makes an array of words now with the first for loop it checks if the badwords array includes any of this words if yes it pushing it to the result array if no it gose to secound loop and it check for bination words also (that might be inside your bad words array like ['bad joe']) and if it found it also pushing it to the result array. then if result array length not be 0 so can say you have some bad words and you can print resualt

const badW = ["Donald Trump", "Mr.Burns", "Sathan"];
const badWordCheker = function (sentence) {
  const words = sentence.trim().split(" ");
  const result = [];

  for (let i = 0; i < words.length; i++) {
    const currentWord = words[i];
    if (badW.includes(currentWord)) {
      result.push(currentWord);
    } else {
      for (let j = i + 1; j < words.length; j++) {
        const nextWords = words.slice(i, j + 1);

        const phrase = nextWords.join(" ");

        if (badW.includes(phrase)) {
          result.push(phrase);
        }
      }
    }
  }

  if (result.length !== 0) {
    return `the bad word you used  : (${result}) `;
  } else {
    return "what you wrote is ok";
  }
};

console.log(badWordCheker("Donald Trump is here and so Sathan ! "));

use try catch

const badWords = ['Donald Trump', 'Mr.Burns', 'Sathan']

const description = 'Mr.Burns entered to the hall.'
let isInclude = false
try {
  badWords.forEach(word => {
    if (description.includes(word)) {
      isInclude = true
      throw new Error(word)
    }
  })
} catch (e) {
  console.log(e)
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742296911a4417310.html

相关推荐

  • JavaScript simple profanity filter - Stack Overflow

    Hi I want to create very basic profanity filter in JavaScript.I've an array called badWords and al

    18小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信