-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner.go
95 lines (87 loc) · 1.5 KB
/
cleaner.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
package nlp
import (
"unicode"
"github.com/koykov/byteseq"
)
const (
CleanControl = 1 << iota
CleanMark
CleanPunct
CleanSpace
CleanDigit
CleanNumber
CleanSymbol
CleanLetter
CleanPrint
CleanGraphic
DefaultCleanMask = CleanControl | CleanMark | CleanSymbol | CleanNumber | CleanPunct
)
type UnicodeCleaner[T byteseq.Byteseq] struct {
Mask uint32
}
func NewUnicodeCleaner[T byteseq.Byteseq](mask uint32) UnicodeCleaner[T] {
return UnicodeCleaner[T]{Mask: mask}
}
func (c UnicodeCleaner[T]) Clean(x T) []rune {
return c.AppendClean(nil, x)
}
func (c UnicodeCleaner[T]) AppendClean(dst []rune, x T) []rune {
s := byteseq.Q2S(x)
m := c.Mask
for _, r := range s {
if m > 0 {
if m&CleanControl > 0 {
if unicode.IsControl(r) {
continue
}
}
if m&CleanPrint > 0 {
if unicode.IsPrint(r) {
continue
}
}
if m&CleanGraphic > 0 {
if unicode.IsGraphic(r) {
continue
}
}
if m&CleanMark > 0 {
if unicode.IsMark(r) {
continue
}
}
if m&CleanPunct > 0 {
if unicode.IsPunct(r) {
continue
}
}
if m&CleanSpace > 0 {
if unicode.IsSpace(r) {
continue
}
}
if m&CleanDigit > 0 {
if unicode.IsDigit(r) {
continue
}
}
if m&CleanNumber > 0 {
if unicode.IsNumber(r) {
continue
}
}
if m&CleanSymbol > 0 {
if unicode.IsSymbol(r) {
continue
}
}
if m&CleanLetter > 0 {
if unicode.IsLetter(r) {
continue
}
}
}
dst = append(dst, r)
}
return dst
}