-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathword.go
165 lines (136 loc) · 3.12 KB
/
word.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package tsplitter
import (
"strings"
"sync"
)
const (
knownType = iota
unknownType
)
//Words after splited
type Words struct {
words []string
wordTypes []int
size int
deDup []map[string]struct{}
lockDedup sync.RWMutex
}
func newWords() *Words {
return &Words{
deDup: []map[string]struct{}{
make(map[string]struct{}),
make(map[string]struct{}),
},
}
}
func (w *Words) add(word string, wordType int) {
w.words = append(w.words, word)
w.wordTypes = append(w.wordTypes, wordType)
w.addDedup(word, wordType)
w.size++
}
func (w *Words) addDedup(word string, wordType int) {
w.deDup[wordType][word] = struct{}{}
}
func (w *Words) removeDedup(word string, wordType int) {
w.lockDedup.Lock()
delete(w.deDup[wordType], word)
w.lockDedup.Unlock()
}
func (w *Words) addKnown(word string) {
w.add(word, knownType)
}
func (w *Words) addUnKnown(word string) {
w.add(word, unknownType)
}
func (w *Words) concatLast(word string, newWordType int) {
last := w.size - 1
w.removeDedup(w.words[last], w.wordTypes[last])
w.words[last] += word
w.wordTypes[last] = newWordType
w.addDedup(w.words[last], w.wordTypes[last])
}
func (w *Words) isLastType(wordType int) bool {
return w.wordTypes[w.size-1] == wordType
}
//All return all words
func (w *Words) All() []string {
return w.words
}
//AllDedup return all deduplicate words
func (w *Words) AllDedup() []string {
allLen := 0
for _, v := range w.deDup {
allLen += len(v)
}
result := make([]string, allLen)
i := 0
for _, v := range w.deDup {
for k := range v {
result[i] = k
i++
}
}
return result
}
//AllDedupDelim return all deduplicate words with delimiter
func (w *Words) AllDedupDelim(delim string) string {
allLen := 0
for _, v := range w.deDup {
allLen += len(v)
}
result := make([]string, allLen)
i := 0
for _, v := range w.deDup {
for k := range v {
result[i] = k
i++
}
}
return strings.Join(result, delim)
}
//AllDedupInterface return all deduplicate words in terface type
func (w *Words) AllDedupInterface() []interface{} {
allLen := 0
for _, v := range w.deDup {
allLen += len(v)
}
result := make([]interface{}, allLen)
i := 0
for _, v := range w.deDup {
for k := range v {
result[i] = k
i++
}
}
return result
}
func (w *Words) getDedup(wordType int) []string {
result := make([]string, len(w.deDup[wordType]))
i := 0
for k := range w.deDup[wordType] {
result[i] = k
i++
}
return result
}
//Unknown return deduplicate words which not found in dictionary
func (w *Words) Unknown() []string {
return w.getDedup(unknownType)
}
//Unknown return deduplicate words which not found in dictionary with delim
func (w *Words) UnknownDelim(delim string) string {
return strings.Join(w.getDedup(unknownType), delim)
}
//Known return deduplicate and ambiguous words which found in dictionary
func (w *Words) Known() []string {
return w.getDedup(knownType)
}
//Known return deduplicate and ambiguous words which found in dictionary with delim
func (w *Words) KnownDelim(delim string) string {
return strings.Join(w.getDedup(knownType), delim)
}
//Size return size of words
func (w *Words) Size() int {
return w.size
}