Skip to content

Commit bd9a87f

Browse files
committed
implemented options params with object initialization
1 parent 7d11ca0 commit bd9a87f

File tree

3 files changed

+137
-50
lines changed

3 files changed

+137
-50
lines changed

README.md

100644100755
+51-16
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,67 @@
11
[![Create Release](https://github.com/pcpratheesh/go-censorword/actions/workflows/release.yml/badge.svg)](https://github.com/pcpratheesh/go-censorword/actions/workflows/release.yml)
22

33
# go-censorword
4-
5-
go-censorword is a lightweight library for detecting profanities in Go string.
6-
4+
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.
75

86
## Installation
9-
```
7+
```sh
108
go get -u github.com/pcpratheesh/go-censorword
119
```
10+
11+
for previous version
12+
13+
```sh
14+
go get -u github.com/pcpratheesh/[email protected]
15+
```
16+
1217
## Usage
1318
```go
1419
import (
1520
"github.com/pcpratheesh/go-censorword"
1621
)
1722
```
1823

24+
1925
## In working
20-
go-censorword package uses a censorWord [here](censor/censor.go) list to check the profanities.
21-
Als we have provided an option to override this list of contents by using
26+
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.
27+
2228
```go
23-
CustomCensorList()
29+
CustomCensorList([]string{})
2430
```
25-
You can provide your own list to search and replace the profanities
2631

27-
## Example
32+
## How to use
2833
```go
29-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
30-
detector.CustomCensorList([]string{
31-
"bad", "word","one",
32-
})
34+
// this would initialize the detector object.
35+
var detector = gocensorword.NewDetector(
36+
gocensorword.WithCensorReplaceChar("*"),
37+
38+
// override the existing list of bad words with your own
39+
gocensorword.WithCustomCensorList([]string{
40+
"bad", "word","one",
41+
}),
42+
)
43+
44+
// censor the word
45+
actualString := "with having some bad words"
46+
filterString, err := detector.CensorWord(actualString)
47+
if err != nil {
48+
panic(err)
49+
}
50+
3351
```
52+
53+
## Option Methods
54+
- *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 (@).
55+
- *WithCustomCensorList([]string)* : The list of your own profanity words
56+
- *WithSanitizeSpecialCharacters(bool)*: To sanitize the special characters in the word
57+
- *WithKeepPrefixChar(bool)*: If you want to Kept the prefix Character (eg : F****)
58+
- *WithKeepSuffixChar(bool)*: If you want to Kept the suffix Character (eg : ****K)
59+
3460
## Example
3561
```go
36-
detector := NewDetector().SetCensorReplaceChar("*")
62+
detector := NewDetector(
63+
gocensorword.WithCensorReplaceChar("*"),
64+
)
3765

3866
resultString, err := detector.CensorWord(inputString)
3967

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

4674
In the future, we should implement the following points
4775
- Support for other language profanities
48-
- All words having repeated characters more than twice (eg : fuck -> fuuuuuck)
49-
- Should remove word match conditions (eg :-> fucker, fucking..etc)
76+
- All words having repeated characters more than twice
77+
78+
79+
## Contributing
80+
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.
81+
82+
83+
## License
84+
The Profanity Filter package is licensed under the MIT License. See the LICENSE file for more information.

gocensorword.go

100644100755
+51-17
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var (
1919
Transformer transform.Transformer
2020
)
2121

22+
type Options func(*CensorWordDetection)
2223
type CensorWordDetection struct {
2324
CensorList []string
2425
CensorReplaceChar string
@@ -30,8 +31,8 @@ type CensorWordDetection struct {
3031
}
3132

3233
// this will create a new CensorWordDetection object
33-
func NewDetector() *CensorWordDetection {
34-
return &CensorWordDetection{
34+
func NewDetector(options ...Options) *CensorWordDetection {
35+
detector := &CensorWordDetection{
3536
CensorList: censor.CensorWordsList,
3637
CensorReplaceChar: censor.CensorChar,
3738
KeepPrefixChar: false,
@@ -40,31 +41,64 @@ func NewDetector() *CensorWordDetection {
4041
TextNormalization: true,
4142
ReplaceCheckPattern: "(?i)%s",
4243
}
44+
45+
// add / update new options
46+
for _, opt := range options {
47+
opt(detector)
48+
}
49+
50+
return detector
4351
}
4452

53+
// WithCustomCensorList
4554
// change the default censor list
4655
// can provide own censor words list
47-
func (censor *CensorWordDetection) CustomCensorList(list []string) *CensorWordDetection {
48-
censor.CensorList = list
49-
return censor
56+
func WithCustomCensorList(list []string) Options {
57+
return func(detector *CensorWordDetection) {
58+
detector.CensorList = list
59+
}
5060
}
5161

52-
// change the censorReplaceCharacter
53-
func (censor *CensorWordDetection) SetCensorReplaceChar(char string) *CensorWordDetection {
54-
censor.CensorReplaceChar = char
55-
return censor
62+
// WithCensorReplaceChar
63+
func WithCensorReplaceChar(char string) Options {
64+
return func(detector *CensorWordDetection) {
65+
detector.CensorReplaceChar = char
66+
}
5667
}
5768

58-
// sanitize special characters
59-
func (censor *CensorWordDetection) WithSanitizeSpecialCharacters(status bool) *CensorWordDetection {
60-
censor.SanitizeSpecialCharacters = status
61-
return censor
69+
// WithSanitizeSpecialCharacters
70+
func WithSanitizeSpecialCharacters(status bool) Options {
71+
return func(detector *CensorWordDetection) {
72+
detector.SanitizeSpecialCharacters = status
73+
}
6274
}
6375

64-
// sanitize text normalization
65-
func (censor *CensorWordDetection) WithTextNormalization(status bool) *CensorWordDetection {
66-
censor.TextNormalization = status
67-
return censor
76+
// WithTextNormalization
77+
func WithTextNormalization(status bool) Options {
78+
return func(detector *CensorWordDetection) {
79+
detector.TextNormalization = status
80+
}
81+
}
82+
83+
// WithKeepPrefixChar
84+
func WithKeepPrefixChar() Options {
85+
return func(detector *CensorWordDetection) {
86+
detector.KeepPrefixChar = true
87+
}
88+
}
89+
90+
// WithKeepPrefixChar
91+
func WithKeepSuffixChar() Options {
92+
return func(detector *CensorWordDetection) {
93+
detector.KeepSuffixChar = true
94+
}
95+
}
96+
97+
// WithReplaceCheckPattern
98+
func WithReplaceCheckPattern(pattern string) Options {
99+
return func(detector *CensorWordDetection) {
100+
detector.ReplaceCheckPattern = pattern
101+
}
68102
}
69103

70104
// sanitize text normalization

gocensorword_test.go

100644100755
+35-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import (
99
)
1010

1111
func TestBadWord(t *testing.T) {
12-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
12+
var detector = gocensorword.NewDetector(
13+
gocensorword.WithCensorReplaceChar("*"),
14+
)
1315

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

2325
func TestWithCustomList(t *testing.T) {
24-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
26+
var detector = gocensorword.NewDetector(
27+
gocensorword.WithCensorReplaceChar("*"),
28+
gocensorword.WithCustomCensorList([]string{
29+
"ass", "bitch",
30+
}),
31+
)
2532

2633
word := "bad ass"
27-
detector.CustomCensorList([]string{
28-
"ass", "bitch",
29-
})
34+
3035
resultString, err := detector.CensorWord(word)
3136
if err != nil {
3237
panic(err)
@@ -36,7 +41,9 @@ func TestWithCustomList(t *testing.T) {
3641
}
3742

3843
func TestBadWordFirstLetterKept(t *testing.T) {
39-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
44+
var detector = gocensorword.NewDetector(
45+
gocensorword.WithKeepPrefixChar(),
46+
)
4047

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

5158
func TestBadWordFirstAndLastLetterKept(t *testing.T) {
52-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
59+
var detector = gocensorword.NewDetector(
60+
gocensorword.WithCensorReplaceChar("*"),
61+
gocensorword.WithKeepPrefixChar(),
62+
gocensorword.WithKeepSuffixChar(),
63+
)
5364

5465
word := "bitch"
55-
detector.KeepPrefixChar = true
56-
detector.KeepSuffixChar = true
5766
resultString, err := detector.CensorWord(word)
5867
if err != nil {
5968
panic(err)
@@ -63,26 +72,35 @@ func TestBadWordFirstAndLastLetterKept(t *testing.T) {
6372
}
6473

6574
func TestBadWordEmptyList(t *testing.T) {
66-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
67-
detector.CustomCensorList([]string{})
75+
var detector = gocensorword.NewDetector(
76+
gocensorword.WithCustomCensorList(nil),
77+
)
78+
6879
word := "bitch"
6980
_, err := detector.CensorWord(word)
7081
require.NotNil(t, err)
7182
}
7283

7384
func TestBadFullLength(t *testing.T) {
74-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
75-
detector.KeepPrefixChar = true
76-
detector.KeepSuffixChar = true
85+
var detector = gocensorword.NewDetector(
86+
gocensorword.WithCensorReplaceChar("*"),
87+
gocensorword.WithKeepPrefixChar(),
88+
gocensorword.WithKeepSuffixChar(),
89+
)
90+
7791
word := "fuck post content asshole suck sucker"
7892
resultString, _ := detector.CensorWord(word)
7993
require.Equal(t, resultString, "f**k post content a*****e s**k s****r")
8094
}
8195

8296
func TestBadWithCustomReplacePattern(t *testing.T) {
83-
var detector = gocensorword.NewDetector().SetCensorReplaceChar("*")
84-
detector.KeepPrefixChar = true
85-
detector.KeepSuffixChar = true
97+
var detector = gocensorword.NewDetector(
98+
gocensorword.WithCensorReplaceChar("*"),
99+
gocensorword.WithKeepPrefixChar(),
100+
gocensorword.WithKeepSuffixChar(),
101+
gocensorword.WithReplaceCheckPattern(`\b%s\b`),
102+
)
103+
86104
detector.ReplaceCheckPattern = `\b%s\b`
87105
word := "pass ass fucker sucker"
88106
resultString, _ := detector.CensorWord(word)

0 commit comments

Comments
 (0)