-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreeng.go
454 lines (420 loc) · 9.33 KB
/
streeng.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package streeng
import (
"io/ioutil"
"net/http"
"path/filepath"
"regexp"
"sync"
)
// Node is a struct of Streeng node
type Node struct {
value rune
words []int
numberWords int
characters map[rune]*Node
}
// Streeng is a struct of Streeng
type Streeng struct {
root *Node
reverseRoot *Node
words []string
nodeCount int
reverseCount int
depth int
terms map[string]int
tokens []int
rate float64
}
/*
StringFromFile function reads bytes from the file
and returns file's data as string
*/
func StringFromFile(fileName string) (string, error) {
abs, err := filepath.Abs(fileName)
if err != nil {
return "", err
}
bytes, err2 := ioutil.ReadFile(abs)
if err2 != nil {
return "", err2
}
return string(bytes), nil
}
/*
StringFromURL function reads bytes from the URL content
and returns content's data as string
*/
func StringFromURL(url string) (string, bool) {
res, err := http.Get(url)
if err != nil {
return "", false
}
defer res.Body.Close()
if res.StatusCode == http.StatusOK {
data, err2 := ioutil.ReadAll(res.Body)
if err2 != nil {
panic("reading failed from body bytes")
}
return string(data), true
}
return "", false
}
/*
MakeStreeng makes a streeng struct with given string array
and it builds new tree. But reverse tree will not build
*/
func MakeStreeng(words []string) *Streeng {
root := new(Node)
root.characters = make(map[rune]*Node)
root.words = nil
s := new(Streeng)
s.root = root
s.nodeCount = 1
s.depth = 0
for k, v := range words {
addString(s, k, v)
}
s.reverseCount = -1
s.words = words
s.rate = float64(len(words)) / float64(s.nodeCount)
s.terms = nil
s.tokens = nil
s.reverseRoot = nil
return s
}
// ReverseStreeng makes reverse tree and attach streeng
func (s *Streeng) ReverseStreeng() *Node {
reverseRoot := new(Node)
reverseRoot.characters = make(map[rune]*Node)
reverseRoot.words = nil
tempNode := reverseRoot
count := 0
for k, v := range s.words {
tempNode = reverseRoot
runic := []rune(v)
lenOfValue := len(runic)
for i := (lenOfValue - 1); i >= 0; i-- {
isLast := i == 0
if val, ok := tempNode.characters[runic[i]]; ok {
tempNode = val
if isLast {
tempNode.words = append(tempNode.words, k)
}
} else {
count++
n := new(Node)
n.characters = make(map[rune]*Node)
n.value = runic[i]
if isLast {
n.words = append(n.words, k)
}
tempNode.characters[runic[i]] = n
tempNode = tempNode.characters[runic[i]]
}
}
}
s.reverseRoot = reverseRoot
s.reverseCount = count
return reverseRoot
}
// Traverse function traverses nodes on given tree
func (s *Streeng) Traverse(sc func(*Node)) {
if s != nil && s.root != nil {
traverseChild(s.root, sc)
}
}
// GoTraverse function traverses nodes on given tree with goroutines
func (s *Streeng) GoTraverse(sc func(*Node)) {
if s != nil && s.root != nil {
var wg sync.WaitGroup
wg.Add(len(s.root.characters))
for _, v := range s.root.characters {
go goTraverseChild(&wg, v, sc)
}
wg.Wait()
}
}
// Clean function cleans the tree
func (s *Streeng) Clean() {
if s != nil && s.root != nil {
cleanChild(s.root)
cleanChild(s.reverseRoot)
s.root.words = nil
s.root.characters = nil
s.words = nil
s.nodeCount = 1
s.reverseCount = 1
s.terms = nil
s.tokens = nil
}
}
// Search function searches given word in the tree
func (s *Streeng) Search(word string) []int {
runic := []rune(word)
lenOfWord := len(runic)
if s != nil && s.root != nil && lenOfWord > 0 {
tempNode := s.root
for i := 0; i < lenOfWord; i++ {
if tempNode.characters[runic[i]] != nil {
tempNode = tempNode.characters[runic[i]]
} else {
return nil
}
}
return tempNode.words
}
return nil
}
// Match function matches words with given regular expression
func (s *Streeng) Match(regex string) ([]int, error) {
re, err := regexp.Compile(regex)
if err != nil {
return nil, err
}
var mutex sync.Mutex
results := []int{}
s.GoTraverse(func(node *Node) {
if re.MatchString(s.words[node.words[0]]) {
for _, index := range node.words {
mutex.Lock()
results = append(results, index)
mutex.Unlock()
}
}
})
return results, nil
}
// StartWith function searches words which start with given string
func (s *Streeng) StartWith(word string) []int {
runic := []rune(word)
lenOfWord := len(runic)
if s != nil && s.root != nil && lenOfWord > 0 {
tempNode := s.root
for i := 0; i < lenOfWord; i++ {
if tempNode.characters[runic[i]] != nil {
tempNode = tempNode.characters[runic[i]]
} else {
return nil
}
}
words := []int{}
for _, v := range tempNode.words {
words = append(words, v)
}
getSubstring(&words, tempNode)
return words
}
return nil
}
// EndWith function searches words which end with given string
func (s *Streeng) EndWith(word string) []int {
runic := []rune(word)
lenOfWord := len(runic)
if s != nil && s.reverseRoot != nil && lenOfWord > 0 {
tempNode := s.reverseRoot
for i := (lenOfWord - 1); i >= 0; i-- {
if tempNode.characters[runic[i]] != nil {
tempNode = tempNode.characters[runic[i]]
} else {
return nil
}
}
words := []int{}
for _, v := range tempNode.words {
words = append(words, v)
}
getSubstring(&words, tempNode)
return words
}
return nil
}
// Terms function calculates term of tree with frequency as map
func (s *Streeng) Terms() map[string]int {
if s != nil {
s.terms = make(map[string]int)
s.tokens = make([]int, len(s.words))
for j := 0; j < len(s.words); j++ {
s.tokens[j] = -1
}
i := 1
collectTerm(s, s.root, &i)
}
return s.terms
}
// FindFreqTerms reports frequent of terms bigger than minimum value
func (s *Streeng) FindFreqTerms(min int) map[string]int {
if s != nil && s.terms != nil && min >= 0 {
if min > 0 {
terms := make(map[string]int)
for k, v := range s.terms {
if v >= min {
terms[k] = v
}
}
return terms
}
return s.terms
}
return nil
}
// Contains returns whether or not the word exists
func (s *Streeng) Contains(word string) bool {
runic := []rune(word)
lenOfWord := len(runic)
if s != nil && s.root != nil && lenOfWord > 0 {
tempNode := s.root
for i := 0; i < lenOfWord; i++ {
if tempNode.characters[runic[i]] != nil {
tempNode = tempNode.characters[runic[i]]
} else {
return false
}
}
return len(tempNode.words) > 0
}
return false
}
// Depth returns depth of streeng
func (s *Streeng) Depth() int {
return s.depth
}
// Words returns element of words
func (s *Streeng) Words(index int) string {
if index >= 0 {
return s.words[index]
}
return ""
}
// NodeCount returns count of streeng's tree
func (s *Streeng) NodeCount() int {
return s.nodeCount
}
// ReverseNodeCount returns count of streeng's reverse tree
func (s *Streeng) ReverseNodeCount() int {
return s.reverseCount
}
// Rate returns rate streeng
func (s *Streeng) Rate() float64 {
return s.rate
}
// TermList returns list of terms
func (s *Streeng) TermList() map[string]int {
return s.terms
}
// TokenList returns list of tokens
func (s *Streeng) TokenList() []int {
return s.tokens
}
// Value returns rune value of node
func (n *Node) Value() rune {
return n.value
}
/*
Words returns word of index,
if index is smaller than 0 or
bigger than number of words, it returns -1
*/
func (n *Node) Words(index int) int {
if index >= 0 && index < n.numberWords {
return n.words[index]
}
return -1
}
/*
Character returns node's rune child.
If there is not, it returns nil
*/
func (n *Node) Character(char rune) *Node {
if val, ok := n.characters[char]; ok {
return val
}
return nil
}
// NumberWords returns number of node's words
func (n *Node) NumberWords() int {
return n.numberWords
}
func getSubstring(words *[]int, node *Node) {
if node != nil {
for _, v := range node.words {
*words = append(*words, v)
}
for _, v := range node.characters {
getSubstring(words, v)
}
}
}
func addString(s *Streeng, index int, value string) {
tempNode := s.root
runic := []rune(value)
lenOfValue := len(runic)
if lenOfValue > s.depth {
s.depth = lenOfValue
}
for i := 0; i < lenOfValue; i++ {
isLast := i+1 == lenOfValue
if val, ok := tempNode.characters[runic[i]]; ok {
tempNode = val
if isLast {
tempNode.words = append(tempNode.words, index)
tempNode.numberWords++
}
} else {
n := new(Node)
n.characters = make(map[rune]*Node)
n.value = runic[i]
n.numberWords = 0
s.nodeCount++
if isLast {
n.words = append(n.words, index)
n.numberWords++
}
tempNode.characters[runic[i]] = n
tempNode = tempNode.characters[runic[i]]
}
}
}
func collectTerm(s *Streeng, node *Node, i *int) {
if node != nil {
if len(node.words) > 0 {
s.terms[s.words[node.words[0]]] = len(node.words)
for _, v := range node.words {
s.tokens[v] = *i
}
*i++
}
for _, v := range node.characters {
collectTerm(s, v, i)
}
}
}
func traverseChild(node *Node, sc func(*Node)) {
if node != nil {
if len(node.words) > 0 {
sc(node)
}
for _, v := range node.characters {
traverseChild(v, sc)
}
}
}
func goTraverseChild(wg *sync.WaitGroup, node *Node, sc func(*Node)) {
if node != nil {
if len(node.words) > 0 {
sc(node)
}
for _, v := range node.characters {
traverseChild(v, sc)
}
}
wg.Done()
}
func cleanChild(node *Node) {
if node != nil {
for _, v := range node.characters {
cleanChild(v)
}
node.words = nil
node.characters = nil
}
}