-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathwordFlags.go
87 lines (68 loc) · 1.23 KB
/
wordFlags.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
package lingo
import (
"fmt"
"strings"
"unicode"
)
// WordFlags represent the types a word may be. A word may have multiple flags
type WordFlag uint32
const (
NoFlag WordFlag = iota
IsLetter
IsAscii
IsDigit
IsLower
IsPunct
IsSpace
IsTitle
IsUpper
LikeURL
LikeNum
LikeEmail
IsStopWord
IsOOV // for ner
MAXFLAG
)
func (f WordFlag) String() string {
return fmt.Sprintf("%014b", f)
}
func (l Lexeme) Flags() WordFlag {
var wf WordFlag
s := l.Value
if StringIs(s, unicode.IsLetter) {
wf |= (1 << IsLetter)
}
if StringIs(s, unicode.IsDigit) {
wf |= (1 << IsDigit)
}
if StringIs(s, isAscii) {
wf |= (1 << IsAscii)
}
if StringIs(s, unicode.IsLower) {
wf |= (1 << IsLower)
}
if StringIs(s, unicode.IsPunct) {
wf |= (1 << IsPunct)
}
if StringIs(s, unicode.IsSpace) {
wf |= (1 << IsSpace)
}
if StringIs(s, unicode.IsUpper) {
wf |= (1 << IsUpper)
}
if l.LexemeType == URI {
wf |= (1 << LikeURL)
}
if _, ok := NumberWords[strings.ToLower(s)]; ok {
wf |= (1 << LikeNum)
}
if _, ok := stopwords[s]; ok {
wf |= (1 << IsStopWord)
}
if len(s) > 0 {
if (unicode.IsUpper(rune(s[0])) || unicode.IsTitle(rune(s[0]))) && StringIs(s[1:], unicode.IsLower) {
wf |= (1 << IsTitle)
}
}
return wf
}