Introduction
Detecting and filtering bad words in applications is crucial for maintaining a safe and respectful environment. In this blog post, we introduce various APIs to handle bad word detection and filtering in JavaScript. We’ll go through dozens of useful API examples with code snippets to help you understand how to use these APIs effectively.
Bad Words Library
The bad-words
library is a popular solution for detecting and filtering offensive language in strings. Below are some examples of how to use this library.
Installation
npm install bad-words
Basic Usage
const Filter = require('bad-words');
const filter = new Filter();
const cleanString = filter.clean('This is a damn bad word!');
console.log(cleanString); // This is a **** bad word!
Adding Custom Bad Words
const filter = new Filter();
filter.addWords('badword1', 'badword2');
const text = 'This is a badword1';
console.log(filter.clean(text)); // This is a *****
Removing words from the filter
const filter = new Filter();
filter.removeWords('hells', 'sadist');
const text = 'This is hells';
console.log(filter.clean(text)); // This is hells
Profanity Filter
The profanity-filter
library is another robust solution for filtering bad language. Here are some examples:
Installation
npm install profanity-filter
Basic Usage
const Profanity = require('profanity-js');
const profanity = new Profanity();
const cleanText = profanity.clean('This is a damn bad word');
console.log(cleanText); // This is a **** bad word
Checking for Profanity
const profanity = new Profanity();
const hasProfanity = profanity.isProfane('You are a bad person');
console.log(hasProfanity); // true or false based on the string
Application Example
Now, let’s create a simple chat application that filters out bad words using the bad-words library.
Setting up the Project
mkdir chat-filter-app
cd chat-filter-app
npm init -y
npm install express bad-words
Creating the Server
const express = require('express');
const Filter = require('bad-words');
const app = express();
const port = 3000;
const filter = new Filter();
app.use(express.json());
app.post('/send-message', (req, res) => {
const message = req.body.message;
const cleanMessage = filter.clean(message);
res.send({ cleanMessage });
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
Testing the Application
Use the following curl command to test the application:
curl -X POST http://localhost:3000/send-message -H "Content-Type: application/json" -d '{"message": "This is some damn example text!"}'
The server will respond with the filtered text.
Conclusion
Implementing bad words filtering in your application can significantly improve user experience and maintain a respectful environment. Both bad-words
and profanity-filter
libraries offer robust solutions for detecting and filtering offensive language. By following the examples provided, you can easily integrate these libraries into your project.
Hash: b54f5671aa01d1c2714770c4138eaa4dc1af9020f7015af94e0fe34ba4e7ebea