-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathflash.go
141 lines (106 loc) · 2.87 KB
/
flash.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
package flash
import "github.com/dav009/flash/trie"
func extractKeywords(t *trie.Trie, sentence string) []string {
matches := make([]string, 0)
currentTrie := t
//sequence_end_pos := 0
idx := 0
sentenceLen := len(sentence)
for idx < sentenceLen {
char := string(sentence[idx])
//fmt.Println(string(char))
// it is a boundary char (i.e: space)
if isWordBoundarie(trie.Character(char)) {
idx2, longestSequenceFound := checkIfMatch(currentTrie, sentence, idx)
idx = idx2
if longestSequenceFound != "" {
matches = append(matches, longestSequenceFound)
}
currentTrie = t
} else if insideTrie, _ := currentTrie.Retrieve(trie.Character(char)); insideTrie != nil {
// if it is indexed in the current trie
currentTrie = insideTrie
} else {
// if it is not index in the currrent trie
currentTrie = t
idy := idx + 1
for idy < sentenceLen {
char := sentence[idy]
if isWordBoundarie(trie.Character(char)) {
break
}
idy += 1
}
idx = idy
}
if idx+1 >= sentenceLen {
if currentTrie.IsKeyword() {
matches = append(matches, currentTrie.IndexedWord)
}
}
idx += 1
}
return matches
}
type Keywords struct {
t *trie.Trie
}
func NewKeywords() Keywords {
return Keywords{trie.NewTrie()}
}
func (x Keywords) Extract(sentence string) []string {
return extractKeywords(x.t, sentence)
}
func (x Keywords) Add(w string) {
x.t.Index(trie.Keyword(w))
}
func isWordBoundarie(c trie.Character) bool {
return c == "" || c == " " || c == "\t" || c == "\n"
}
func checkIfMatch(t *trie.Trie, sentence string, idx int) (int, string) {
char := sentence[idx]
sequenceFound := ""
longestSequenceFound := ""
if t.IsKeyword() {
sequenceFound = t.IndexedWord
longestSequenceFound = t.IndexedWord
}
if t.IsCharIn(trie.Character(char)) {
// look for longest sequence from here
nextTrie, _ := t.Retrieve(trie.Character(char))
seqFound, sequenceEndpos := searchLongest(sentence, idx, nextTrie)
longestSequenceFound = seqFound
if longestSequenceFound != "" && sequenceFound != longestSequenceFound {
idx = sequenceEndpos
}
}
return idx, longestSequenceFound
}
func searchLongest(sentence string, idx int, t *trie.Trie) (string, int) {
longestSequenceFound := ""
sequenceEndpos := -1
sentenceLen := len(sentence)
idx = idx + 1
currentTrie := t
for idx < sentenceLen {
char := sentence[idx]
if (isWordBoundarie(trie.Character(char))) && currentTrie.IsKeyword() {
longestSequenceFound = currentTrie.IndexedWord
sequenceEndpos = idx
}
if trie, _ := currentTrie.Retrieve(trie.Character(char)); trie != nil {
// if it is indexed in the current trie
currentTrie = trie
} else {
break
}
idx = idx + 1
}
if idx >= sentenceLen {
if currentTrie.IsKeyword() {
longestSequenceFound = currentTrie.IndexedWord
sequenceEndpos = idx
}
}
return longestSequenceFound, sequenceEndpos
}