This repository has been archived by the owner on Jun 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
traversal.go
171 lines (134 loc) · 3.73 KB
/
traversal.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
package spellio
import (
"strings"
)
// CountWords returns the number of words in the dictionary.
func (e *Engine) CountWords() int {
cnt := 0
stack := []*letterNode{&e.root}
for len(stack) > 0 {
curr := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if curr.Word != nil {
cnt++
}
for _, child := range curr.children {
stack = append(stack, child)
}
}
return cnt
}
func (e *Engine) findNode(word string) (*letterNode, bool) {
curr := &e.root
for _, char := range word {
next, ok := curr.findChild(char)
if !ok {
return nil, false
}
curr = next
}
return curr, true
}
// GetWordsByPrefix returns all words in the dictionary
// that start with the given prefix.
func (e *Engine) GetWordsByPrefix(prefix string) []*Word {
prefix = strings.ToLower(prefix)
start, ok := e.findNode(prefix)
if !ok {
return nil
}
var words []*Word
stack := []*letterNode{start}
for len(stack) > 0 {
curr := stack[len(stack)-1]
stack = stack[:len(stack)-1]
if curr.Word != nil {
words = append(words, curr.Word)
}
for _, child := range curr.children {
stack = append(stack, child)
}
}
return words
}
// A NearbyWord contains the word and the number of changes
// required to transform it into the original word.
type NearbyWord struct {
*Word
Changes int
}
// A KeyboardLayout is a map of characters to
// characters that are nearby on the keyboard
// and are considered as possible replacements.
type KeyboardLayout map[rune][]rune
// GetNearbyWords returns all words in the dictionary
// that are near the given word and can be transformed
// into it with the given number of changes.
func (e *Engine) GetNearbyWords(rawWord string, maxChanges int, layout KeyboardLayout) []NearbyWord {
rawWord = strings.ToLower(rawWord)
rawWordChars := []rune(rawWord)
possibleWords := make([]NearbyWord, 0)
type nearbyWordState struct {
node *letterNode
chars []rune
changes int
index int
}
queue := []nearbyWordState{{&e.root, []rune{}, 0, 0}}
for len(queue) > 0 {
currState := queue[0]
queue = queue[1:]
if currState.index == len(rawWordChars) {
if currState.node.Word != nil {
wordInfo := NearbyWord{currState.node.Word, currState.changes}
possibleWords = append(possibleWords, wordInfo)
}
continue
}
nextChar := rawWordChars[currState.index]
nextNode, ok := currState.node.findChild(nextChar)
if ok {
nextState := nearbyWordState{
nextNode,
make([]rune, len(currState.chars)+1),
currState.changes + 1, currState.index + 1,
}
copy(nextState.chars, currState.chars)
nextState.chars[len(currState.chars)] = nextChar
queue = append(queue, nextState)
}
if currState.changes < maxChanges {
alternativeChars := layout[nextChar]
for _, alternativeChar := range alternativeChars {
alternativeNextNode, ok := currState.node.findChild(alternativeChar)
if !ok {
continue
}
nextStateChars := make([]rune, len(currState.chars)+1)
copy(nextStateChars, currState.chars)
nextStateChars[len(currState.chars)] = alternativeChar
alternativeCharState := nearbyWordState{
alternativeNextNode,
//append(currState.chars, alternativeChar),
nextStateChars,
currState.changes + 1, currState.index + 1,
}
queue = append(queue, alternativeCharState)
}
var checkCharRedundancy bool = true
// temporally every char will be checked for redundancy
if checkCharRedundancy {
nextStateChars := make([]rune, len(currState.chars))
copy(nextStateChars, currState.chars)
redundantCharState := nearbyWordState{
currState.node,
//currState.chars,
nextStateChars,
currState.changes + 1, currState.index + 1,
}
queue = append(queue, redundantCharState)
}
}
}
return possibleWords
}