forked from awsong/go-darts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dawg.go
289 lines (260 loc) · 7.81 KB
/
dawg.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
package darts
import (
"fmt"
"sort"
)
type dawgNode struct {
parents, children map[rune] /*Key_type*/ *dawgNode
lastChar rune /*Key_type*/
acceptable, merged, printed bool
index int
freq int
}
func addSubTree(node *dawgNode, key []rune /*Key_type*/) {
current := node
for _, char := range key {
current.lastChar = char
if current.children == nil {
current.children = make(map[rune] /*Key_type*/ *dawgNode)
}
newNode := new(dawgNode)
current.children[char] = newNode
newNode.parents = make(map[rune] /*Key_type*/ *dawgNode)
if current.acceptable {
newNode.parents[-char] = current
} else {
newNode.parents[char] = current
}
current = newNode
}
current.acceptable = true
}
func merge(current, start, end *dawgNode) {
// go to the tail of the not-yet-merged path
c := current
for c.children != nil {
c.merged = true
c = c.children[c.lastChar]
}
if c == end {
return
}
r := end
var char rune /*Key_type*/
var pc *dawgNode
// each node on the not-yet-merged path has only one parent
for char, pc = range c.parents {
}
pr, found := r.parents[char]
for found == true && pr.merged == true && pc != current && pc.merged == false {
c, r = pc, pr
for char, pc = range c.parents {
}
pr, found = r.parents[char]
if r == current {
fmt.Println("Oooooops, hoho")
}
}
r.parents[char] = pc
if char < 0 { //means pc is acceptable
if pc.acceptable == false {
fmt.Println("wrong condition")
}
char = -char
}
pc.children[char] = r
/*
if r == start{
fmt.Printf("start:%p, current:%p, end:%p\n", start, current, end)
m := start
fmt.Printf("%p\n", m)
for m.children != nil{
fmt.Printf("%d ", m.lastChar)
m = m.children[m.lastChar]
fmt.Printf("%p\n", m)
}
fmt.Printf("%v\n", end.parents)
}
*/
//tag merged flag
m := current.children[current.lastChar]
for m != nil {
m.merged = true
m = m.children[m.lastChar]
}
}
func buildDAWG(keys [][]rune /*Key_type*/, freq []int) *dawgNode {
first := true
start := new(dawgNode)
var end *dawgNode
f0:
for _, key := range keys {
current := start
for j, alphabet := range key {
if current.children == nil {
// if we are here, means key is a super string of the previous one
// like previous: abcd, key: abcdef
addSubTree(current, key[j:])
continue f0
}
if alphabet > current.lastChar {
if first {
first = false
end = current
for end.children != nil {
end = end.children[end.lastChar]
}
}
// order is important, merge() must be called before addSubTree()
merge(current, start, end)
addSubTree(current, key[j:])
continue f0
}
current = current.children[current.lastChar]
}
}
if first {
first = false
end = start
for end.children != nil {
end = end.children[end.lastChar]
}
}
merge(start, start, end)
return start
}
func printDAWG(d *dawgNode) {
if d.printed {
return
}
d.printed = true
fmt.Printf("This: %p, acceptable:%t\n", d, d.acceptable)
for i, p := range d.parents {
fmt.Printf("Parent %d: %p\n", i, p)
}
for i, p := range d.children {
fmt.Printf("Child %d: %p\n", i, p)
}
for _, p := range d.children {
printDAWG(p)
}
}
func BuildFromDAWG(keys [][]rune /*Key_type*/, freq []int) Darts {
dawg := buildDAWG(keys, freq)
//fmt.Println(dawg.children[19968].children[21051])
//printDAWG(dawg)
var d = new(dartsBuild)
d.key = keys
d.freq = freq
d.resize(512)
d.darts.Base[0] = 1
d.nextCheckPos = 0
siblings := d.fetchDAWG(dawg)
d.insertDAWG(siblings)
if d.err < 0 {
panic("Build error")
}
//fmt.Println(dawg.children[19968].children[25163].children[36974].children[22825])
return d.darts
}
type Pair struct {
Char rune /*Key_type*/
node *dawgNode
}
// A slice of Pairs that implements sort.Interface to sort by Value.
type PairList []Pair
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PairList) Len() int { return len(p) }
func (p PairList) Less(i, j int) bool { return p[i].Char < p[j].Char }
// A function to turn a map into a PairList, then sort and return it.
func sortMapByValue(m map[rune] /*Key_type*/ *dawgNode) PairList {
p := make(PairList, len(m))
i := 0
for k, v := range m {
p[i] = Pair{k + 1, v}
i++
}
sort.Sort(p)
return p
}
func (d *dartsBuild) fetchDAWG(parent *dawgNode) PairList {
if parent.acceptable {
newNode := new(dawgNode)
if nil == parent.children {
parent.children = make(map[rune] /*Key_type*/ *dawgNode)
}
//tricky, to make -1(or 255 in byte version) 0 in func sortMapByValue (k+1)
var t rune = /*Key_type*/ 0
parent.children[t-1] = newNode
}
return sortMapByValue(parent.children)
}
func (d *dartsBuild) insertDAWG(siblings PairList) int {
if d.err < 0 {
panic("insertDAWG error")
return 0
}
var begin int = 0
var pos int = max(int(siblings[0].Char)+1, d.nextCheckPos) - 1
var nonZeroNum int = 0
first := false
if len(d.darts.Base) <= pos {
d.resize(pos + 1)
}
for {
next:
pos++
if len(d.darts.Base) <= pos {
d.resize(pos + 1)
}
if d.darts.Check[pos] > 0 {
nonZeroNum++
continue
} else if !first {
d.nextCheckPos = pos
first = true
}
begin = pos - int(siblings[0].Char)
if len(d.darts.Base) <= (begin + int(siblings[len(siblings)-1].Char)) {
d.resize(begin + int(siblings[len(siblings)-1].Char) + 400)
}
if d.used[begin] {
continue
}
for i := 1; i < len(siblings); i++ {
if begin+int(siblings[i].Char) >= len(d.darts.Base) {
fmt.Println(len(d.darts.Base), begin+int(siblings[i].Char), begin+int(siblings[len(siblings)-1].Char))
}
if 0 != d.darts.Check[begin+int(siblings[i].Char)] {
goto next
}
}
break
}
if float32(nonZeroNum)/float32(pos-d.nextCheckPos+1) >= 0.95 {
d.nextCheckPos = pos
}
d.used[begin] = true
d.size = max(d.size, begin+int(siblings[len(siblings)-1].Char)+1)
for i := 0; i < len(siblings); i++ {
d.darts.Check[begin+int(siblings[i].Char)] = begin
}
for i := 0; i < len(siblings); i++ {
if siblings[i].node.index > 0 { // siblings[i] is is already visited
d.darts.Base[begin+int(siblings[i].Char)] = siblings[i].node.index
} else { // siblings[i] is a new node
newSiblings := d.fetchDAWG(siblings[i].node)
if len(newSiblings) == 0 {
var value Value
value.Freq = 1
d.darts.Base[begin+int(siblings[i].Char)] = -len(d.darts.ValuePool) - 1
d.darts.ValuePool = append(d.darts.ValuePool, value)
} else {
h := d.insertDAWG(newSiblings)
siblings[i].node.index = h
d.darts.Base[begin+int(siblings[i].Char)] = h
}
}
}
return begin
}