Blazingly fast cleaning swear words (and their leetspeak) in strings
Currently, the library has set up default filtering for profanity in both English and Vietnamese languages
Inspired by a package better_profanity of Son Thanh Nguyen, This library is a version designed for Node.js.
It supports modified spellings (such as p0rn
, h4NDjob
, handj0b
and b*tCh
).
npm i better_profanity_ts
Only Unicode characters from categories Ll
, Lu
, Mc
and Mn
are added. More on Unicode categories can be found here.
Not all languages are supported yet, such as Chinese.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const text = "You p1ec3 of sHit. Ban nhu con c4c"
console.log(profanity.censor(text))
# You p1ec3 of ****. Ban nhu con ****
}
All modified spellings of words in profanity_wordlist.txt will be generated. For example, the word handjob
would be loaded into:
'handjob', 'handj*b', 'handj0b', 'handj@b', 'h@ndjob', 'h@ndj*b', 'h@ndj0b', 'h@ndj@b',
'h*ndjob', 'h*ndj*b', 'h*ndj0b', 'h*ndj@b', 'h4ndjob', 'h4ndj*b', 'h4ndj0b', 'h4ndj@b'
The full mapping of the library can be found in better_profanity.ts.
By default, profanity
replaces each swear words with 4 asterisks ****
.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const text = "You p1ec3 of sHit. Ban nhu con c4c"
console.log(profanity.censor(text))
# You p1ec3 of ****. Ban nhu con ****
}
The function .censor()
also hides words separated not just by an empty space
but also other dividers, such as _
, ,
and .
. Except for @, $, *, ", '
.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const text = "...sh1t...hello_cat_fuck,,,,123"
console.log(profanity.censor(text))
# ...****...hello_cat_****,,,,123
}
4 instances of the character in the second parameter in .censor()
will be used to replace the swear words.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const text = "...sh1t...hello_cat_fuck,,,,123"
console.log(profanity.censor(text, '-'))
# ...----...hello_cat_----,,,,123
}
Function .containsProfanity()
return True
if any words in the given string has a word existing in the wordlist.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const text = "...sh1t...hello_cat_fuck,,,,123"
console.log(profanity.containsProfanity(text))
# true
}
Function loadCensorWords
takes a List
of strings as censored words.
The provided list will replace the default wordlist.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const customBadwords: string[] = ['happy', 'jolly', 'merry'];
profanity.loadCensorWords(customBadwords);
console.log(profanity.censor("Have a merry day!"));
# **** a **** day
}
Function `loadCensorWordsFromFile takes a filename, which is a text file and each word is separated by lines.
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
profanity.loadCensorWordsFromFile('/path/to/my/project/my_wordlist.txt');
console.log(profanity.censor("Have a merry day!"));
# **** a **** day
}
Functions loadCensorWords
and loadCensorWordsFromFile
take a keyword argument whitelistWords
to ignore words in a wordlist.
It is best used when there are only a few words that you would like to ignore in the wordlist.
# Use the default wordlist
const options = { whitelistWords: ['happy', 'merry'] };
profanity.loadCensorWords([], options)
# or with your custom words as a List
const customBadWords: string[] = ['happy', 'jolly', 'merry']
profanity.loadCensorWords(custom_badwords, options)
# or with your custom words as a text file
profanity.loadCensorWordsFromFile('/path/to/my/project/my_wordlist.txt', options)
import { Profanity } from 'better_profanity_ts/lib/better_profanity'
async function main() {
const profanity = new Profanity()
const customBadWords: string[] = ['happy', 'jolly', 'merry']
profanity.addCensorWords(customBadWords);
console.log(profanity.censor("Happy you, fuck!"));
# **** you, ****!
}
Please read for details on our code, and the process for submitting pull requests to us.
This project is licensed under the MIT License - see the LICENSE file for details