-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
PersianSwear.go
102 lines (88 loc) · 2.2 KB
/
PersianSwear.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bufio"
"os"
"regexp"
"strings"
)
type PersianSwear struct {
swearWords []string
}
func NewPersianSwear() *PersianSwear {
ps := &PersianSwear{}
file, err := os.Open("data.txt")
if err != nil {
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
splitedWords := strings.Split(line, ", ")
ps.swearWords = append(ps.swearWords, splitedWords...)
}
if err := scanner.Err(); err != nil {
panic(err)
}
return ps
}
func (ps *PersianSwear) clearWord(word string) string {
pattern := regexp.MustCompile(`[\p{P}\p{Mn}\p{S}\p{N}]`)
clearedWord := pattern.ReplaceAllString(word, "")
clearedWord = strings.TrimSpace(clearedWord)
clearedWord = strings.ReplaceAll(clearedWord, "\u200c", " ")
clearedWord = strings.ReplaceAll(clearedWord, "ي", "ی")
clearedWord = strings.ReplaceAll(clearedWord, "ك", "ک")
clearedWord = strings.ReplaceAll(clearedWord, "ـ", "")
return clearedWord
}
func (ps *PersianSwear) AddWord(word string) {
ps.swearWords = append(ps.swearWords, strings.TrimSpace(word))
}
func (ps *PersianSwear) AddWords(words ...string) {
ps.swearWords = append(ps.swearWords, words...)
}
func (ps *PersianSwear) RemoveWord(word string) {
trimmedWord := strings.TrimSpace(word)
for i, w := range ps.swearWords {
if w == trimmedWord {
ps.swearWords = append(ps.swearWords[:i], ps.swearWords[i+1:]...)
break
}
}
}
func (ps *PersianSwear) RemoveWords(words ...string) {
for _, word := range words {
ps.RemoveWord(word)
}
}
func (ps *PersianSwear) IsBad(word string) bool {
clearedWord := ps.clearWord(word)
for _, w := range ps.swearWords {
if w == clearedWord {
return true
}
}
return false
}
func (ps *PersianSwear) HasSwear(text string) bool {
words := strings.Split(text, " ")
for _, word := range words {
if ps.IsBad(word) {
return true
}
}
return false
}
func (ps *PersianSwear) FilterWords(text, symbol string) string {
words := strings.Split(text, " ")
for i, word := range words {
if ps.IsBad(word) {
words[i] = symbol
}
}
return strings.Join(words, " ")
}
func (ps *PersianSwear) FilterWordsWithDefaultSymbol(text string) string {
return ps.FilterWords(text, "*")
}