Skip to content

Commit

Permalink
implemented options params with object initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
pcpratheesh committed Mar 30, 2023
1 parent 7d11ca0 commit bd9a87f
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 50 deletions.
67 changes: 51 additions & 16 deletions README.md
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,39 +1,67 @@
[![Create Release](https://github.com/pcpratheesh/go-censorword/actions/workflows/release.yml/badge.svg)](https://github.com/pcpratheesh/go-censorword/actions/workflows/release.yml)

# go-censorword

go-censorword is a lightweight library for detecting profanities in Go string.

go-censorword is a lightweight and easy-to-use tool that allows you to detect and filter out profanity words from your text-based content. Whether you're building a social media platform, a chat app, or just want to keep your comments section clean, this package can help.

## Installation
```
```sh
go get -u github.com/pcpratheesh/go-censorword
```

for previous version

```sh
go get -u github.com/pcpratheesh/[email protected]
```

## Usage
```go
import (
"github.com/pcpratheesh/go-censorword"
)
```


## In working
go-censorword package uses a censorWord [here](censor/censor.go) list to check the profanities.
Als we have provided an option to override this list of contents by using
The go-censorword package uses a censorWord [here](censor/censor.go) list to check profanities, but also provides an option for you to override this list with your own contents. You can create a list of bad words that are not included in the original blacklist by using the **customCensorList** method.

```go
CustomCensorList()
CustomCensorList([]string{})
```
You can provide your own list to search and replace the profanities

## Example
## How to use
```go
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
detector.CustomCensorList([]string{
"bad", "word","one",
})
// this would initialize the detector object.
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),

// override the existing list of bad words with your own
gocensorword.WithCustomCensorList([]string{
"bad", "word","one",
}),
)

// censor the word
actualString := "with having some bad words"
filterString, err := detector.CensorWord(actualString)
if err != nil {
panic(err)
}

```

## Option Methods
- *WithCensorReplaceChar(string)* : This method can be used to replace the filtered word characters with asterisks (*), dashes (-) or custom characters, like the pound sign (#) or at sign (@).
- *WithCustomCensorList([]string)* : The list of your own profanity words
- *WithSanitizeSpecialCharacters(bool)*: To sanitize the special characters in the word
- *WithKeepPrefixChar(bool)*: If you want to Kept the prefix Character (eg : F****)
- *WithKeepSuffixChar(bool)*: If you want to Kept the suffix Character (eg : ****K)

## Example
```go
detector := NewDetector().SetCensorReplaceChar("*")
detector := NewDetector(
gocensorword.WithCensorReplaceChar("*"),
)

resultString, err := detector.CensorWord(inputString)

Expand All @@ -45,5 +73,12 @@ if err != nil {

In the future, we should implement the following points
- Support for other language profanities
- All words having repeated characters more than twice (eg : fuck -> fuuuuuck)
- Should remove word match conditions (eg :-> fucker, fucking..etc)
- All words having repeated characters more than twice


## Contributing
Contributions to the Profanity Filter package are welcome and encouraged! If you find a bug or have a feature request, please open an issue on GitHub. If you'd like to contribute code, please fork the repository, make your changes, and submit a pull request.


## License
The Profanity Filter package is licensed under the MIT License. See the LICENSE file for more information.
68 changes: 51 additions & 17 deletions gocensorword.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var (
Transformer transform.Transformer
)

type Options func(*CensorWordDetection)
type CensorWordDetection struct {
CensorList []string
CensorReplaceChar string
Expand All @@ -30,8 +31,8 @@ type CensorWordDetection struct {
}

// this will create a new CensorWordDetection object
func NewDetector() *CensorWordDetection {
return &CensorWordDetection{
func NewDetector(options ...Options) *CensorWordDetection {
detector := &CensorWordDetection{
CensorList: censor.CensorWordsList,
CensorReplaceChar: censor.CensorChar,
KeepPrefixChar: false,
Expand All @@ -40,31 +41,64 @@ func NewDetector() *CensorWordDetection {
TextNormalization: true,
ReplaceCheckPattern: "(?i)%s",
}

// add / update new options
for _, opt := range options {
opt(detector)
}

return detector
}

// WithCustomCensorList
// change the default censor list
// can provide own censor words list
func (censor *CensorWordDetection) CustomCensorList(list []string) *CensorWordDetection {
censor.CensorList = list
return censor
func WithCustomCensorList(list []string) Options {
return func(detector *CensorWordDetection) {
detector.CensorList = list
}
}

// change the censorReplaceCharacter
func (censor *CensorWordDetection) SetCensorReplaceChar(char string) *CensorWordDetection {
censor.CensorReplaceChar = char
return censor
// WithCensorReplaceChar
func WithCensorReplaceChar(char string) Options {
return func(detector *CensorWordDetection) {
detector.CensorReplaceChar = char
}
}

// sanitize special characters
func (censor *CensorWordDetection) WithSanitizeSpecialCharacters(status bool) *CensorWordDetection {
censor.SanitizeSpecialCharacters = status
return censor
// WithSanitizeSpecialCharacters
func WithSanitizeSpecialCharacters(status bool) Options {
return func(detector *CensorWordDetection) {
detector.SanitizeSpecialCharacters = status
}
}

// sanitize text normalization
func (censor *CensorWordDetection) WithTextNormalization(status bool) *CensorWordDetection {
censor.TextNormalization = status
return censor
// WithTextNormalization
func WithTextNormalization(status bool) Options {
return func(detector *CensorWordDetection) {
detector.TextNormalization = status
}
}

// WithKeepPrefixChar
func WithKeepPrefixChar() Options {
return func(detector *CensorWordDetection) {
detector.KeepPrefixChar = true
}
}

// WithKeepPrefixChar
func WithKeepSuffixChar() Options {
return func(detector *CensorWordDetection) {
detector.KeepSuffixChar = true
}
}

// WithReplaceCheckPattern
func WithReplaceCheckPattern(pattern string) Options {
return func(detector *CensorWordDetection) {
detector.ReplaceCheckPattern = pattern
}
}

// sanitize text normalization
Expand Down
52 changes: 35 additions & 17 deletions gocensorword_test.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import (
)

func TestBadWord(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
)

word := "bitch"
resultString, err := detector.CensorWord(word)
Expand All @@ -21,12 +23,15 @@ func TestBadWord(t *testing.T) {
}

func TestWithCustomList(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithCustomCensorList([]string{
"ass", "bitch",
}),
)

word := "bad ass"
detector.CustomCensorList([]string{
"ass", "bitch",
})

resultString, err := detector.CensorWord(word)
if err != nil {
panic(err)
Expand All @@ -36,7 +41,9 @@ func TestWithCustomList(t *testing.T) {
}

func TestBadWordFirstLetterKept(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
var detector = gocensorword.NewDetector(
gocensorword.WithKeepPrefixChar(),
)

word := "bitch"
detector.KeepPrefixChar = true
Expand All @@ -49,11 +56,13 @@ func TestBadWordFirstLetterKept(t *testing.T) {
}

func TestBadWordFirstAndLastLetterKept(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
)

word := "bitch"
detector.KeepPrefixChar = true
detector.KeepSuffixChar = true
resultString, err := detector.CensorWord(word)
if err != nil {
panic(err)
Expand All @@ -63,26 +72,35 @@ func TestBadWordFirstAndLastLetterKept(t *testing.T) {
}

func TestBadWordEmptyList(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
detector.CustomCensorList([]string{})
var detector = gocensorword.NewDetector(
gocensorword.WithCustomCensorList(nil),
)

word := "bitch"
_, err := detector.CensorWord(word)
require.NotNil(t, err)
}

func TestBadFullLength(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
detector.KeepPrefixChar = true
detector.KeepSuffixChar = true
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
)

word := "fuck post content asshole suck sucker"
resultString, _ := detector.CensorWord(word)
require.Equal(t, resultString, "f**k post content a*****e s**k s****r")
}

func TestBadWithCustomReplacePattern(t *testing.T) {
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
detector.KeepPrefixChar = true
detector.KeepSuffixChar = true
var detector = gocensorword.NewDetector(
gocensorword.WithCensorReplaceChar("*"),
gocensorword.WithKeepPrefixChar(),
gocensorword.WithKeepSuffixChar(),
gocensorword.WithReplaceCheckPattern(`\b%s\b`),
)

detector.ReplaceCheckPattern = `\b%s\b`
word := "pass ass fucker sucker"
resultString, _ := detector.CensorWord(word)
Expand Down

0 comments on commit bd9a87f

Please sign in to comment.