Golang package for filtering out Profanity words
Taking Out the Censorship with go-censorword!
Introduction
Go Censorword is an open source, lightweight library for censoring words and phrases in text. It’s purpose is to allow you to quickly and easily censor any text without having to manually replace each word or phrase. With Go Censorword, you can define a list of censored words or phrases that will be automatically replaced with whatever substitute word or phrase you choose.
Getting Started with go-censorword
Installation
Once you have downloaded the Go Censorword library, installation is straightforward and easy. You can simply use the following command to install the package to your application.
go get -u github.com/pcpratheesh/go-censorword
And then import this package in your code
import (
"github.com/pcpratheesh/go-censorword"
)
Configure detector object
You have to create a new gocensorword object in order to do profanity filtering.
// this would initialize the detector object.
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
)
Go censor word is comes with other configuration options such as custom list of bad words, customized replace character , With keeping prefix character, with keeping suffix character..etc. And it can be done by following.
// this would initialize the detector object.
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("#"),
)
gocensorword.WithCensorReplaceChar(“#”) : this will replace the filtered profanity word as #
Example : somebadword — this will be shown as some##word
// this would initialize the detector object.
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("#"),
gocensorword.WithCustomCensorList([]string{
"this", "is","my","own","list","of","bad","words"
})
)
gocensorword.WithCustomCensorList([]string{}) : this would be a list of bad words
// this would initialize the detector object.
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("#"),
gocensorword.WithCustomCensorList([]string{
"this", "is","my","own","list","of","bad","words"
}),
gocensorword.WithKeepPrefixChar(),
)
gocensorword.WithKeepPrefixChar() : If you just wanted to keep the Prefix letter of your bad word,
example : F***
// this would initialize the detector object.
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("#"),
gocensorword.WithCustomCensorList([]string{
"this", "is","my","own","list","of","bad","words"
}),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
)
gocensorword.WithKeepSuffixChar() : If you just wanted to keep the Suffix letter of your bad word,
example : ***K
When you configured WithKeepPrefixChar and WithKeepSuffixChar, the output will be
example : B****D
How filter out bad words
You can use CensorWord method to filter the string
// censor the word
actualString := "with having some bad words"
filterString, err := detector.CensorWord(actualString)
if err != nil {
panic(err)
}
This will output something like as follows
with having some *** words
Furthermore, since Go-Censorword is open source anyone who wishes to contribute additions or modifications can do so by submitting pull requests on Git Hub! This allows anyone who believes they have an idea for improving the library’s functionality or usability to make their contribution known and potentially have it included in future releases — further enhancing its versatility even further!
Checkout Github code for more