-
Notifications
You must be signed in to change notification settings - Fork 0
/
morsetree.go
307 lines (268 loc) · 6.51 KB
/
morsetree.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
package gomorse
import (
"errors"
"strings"
"sync"
)
//Node represent a letter in Tree
type Node struct {
Dot *Node
Dash *Node
Letter string
}
//Tree represents the morse language
type Tree struct {
Groot *Node
}
//Init the morse tree
func initTree() *Tree {
tree := &Tree{Groot: &Node{Letter: "ROOT"}}
tree.insert(".", "E").
insert(".-", "A").
insert(".-.", "R").
insert(".--", "W").
insert(".--.", "P").
insert(".---", "J").
insert(".-..", "L").
insert("..", "I").
insert("..-", "U").
insert("..-.", "F").
insert("...", "S").
insert("...-", "V").
insert("....", "H").
insert("-", "T").
insert("-.", "N").
insert("-.-", "K").
insert("-.--", "Y").
insert("-.-.", "C").
insert("-..", "D").
insert("-..-", "X").
insert("-...", "B").
insert("--", "M").
insert("--.", "G").
insert("--.-", "Q").
insert("--..", "Z").
insert("---", "O")
//ADD NUMBERS
tree.insert(".----", "1")
tree.insert("..---", "2")
tree.insert("...--", "3")
tree.insert("....-", "4")
tree.insert(".....", "5")
tree.insert("-....", "6")
tree.insert("--...", "7")
tree.insert("---..", "8")
tree.insert("----.", "9")
tree.insert("-----", "0")
return tree
}
//insert a new node at the parent node depending on whether code is dot (.) or dash (-)
func (node *Node) insert(code, letter string) {
if node == nil || len(code) != 1 {
return
}
if code == "." { //DOT
if node.Dot == nil {
node.Dot = &Node{Letter: letter}
}else {
node.Dot.insert(code, letter)
}
} else {// DASH
if node.Dash == nil {
node.Dash = &Node{Letter: letter}
} else {
node.Dash.insert(code, letter)
}
}
}
// hasNext return double bool dot and dash that are true if child node is presents
func (node *Node) hasNext() (dot bool, dash bool){
return node.Dot != nil, node.Dash != nil
}
//search a node with given morse code
func (tree *Tree) search(code string) *Node {
currentNode := tree.Groot
for _, partialCode := range code {
pc := string(partialCode)
if pc == "." {
if currentNode.Dot == nil {
continue
}
currentNode = currentNode.Dot
} else {
if currentNode.Dash == nil {
continue
}
currentNode = currentNode.Dash
}
}
return currentNode
}
//addPathNodes Creates each node from the racine to the last node following the code
func (tree *Tree) addPathNodes(code string) *Node{
currentNode := tree.Groot
for _, partialCode := range code {
pc := string(partialCode)
if pc == "." {
if currentNode.Dot == nil {
currentNode.Dot = &Node{Letter: "", Dash: nil, Dot: nil}
}
currentNode = currentNode.Dot
} else {
if currentNode.Dash == nil {
currentNode.Dash = &Node{Letter: "", Dash: nil, Dot: nil}
}
currentNode = currentNode.Dash
}
}
return currentNode
}
//insert a new node at the end of the path describe by the morse code
func (tree *Tree) insert(code, letter string) *Tree {
if tree.Groot == nil {
tree.Groot = &Node{Letter: letter, Dot: nil, Dash: nil}
} else {
if len(code) == 1 {
tree.Groot.insert(code, letter)
} else {
foundNode := tree.addPathNodes(code)
//if the node has no child and no letter setted then we estimate that is the right node to put the letter in it
if dot, dash := foundNode.hasNext(); !dot && !dash && foundNode.Letter == ""{
foundNode.Letter = letter
return tree
}
foundNode.insert(code[len(code)-1:], letter)
}
}
return tree
}
//Get the path of the given letter et put dot or slash on the path string array
func (node *Node) path(path *[]string, letter string) bool {
if path == nil {
panic("path is nil")
}
if node == nil {
return false
}
if node.Dot.path(path, letter) {
*path = append(*path, ".")
return true
}else if node.Dash.path(path, letter){
*path = append(*path, "-")
return true
} else if node.Letter == letter{
return true
}else{
return false
}
}
//browse enable to find the node that contains the given letter
func (node *Node) browse(letter string) (NodeFound *Node){
if node == nil {
return nil
}
nDot, nDash := node.Dot.browse(letter), node.Dash.browse(letter)
if nDot != nil && nDot.Letter == letter{
return nDot
}else if nDash != nil && nDash.Letter == letter{
return nDash
}
return node
}
//decodeWord will be used as go routine to translate morse word to plainWord
func decodeWord(wg *sync.WaitGroup, out *string, word string){
defer wg.Done()
w := ""
for _, code := range strings.Split(word, " ") {
letter, _ := GetLetter(code)
w += letter
}
*out = w
}
/* ========================================
*
* EXPORTED ELEMENTS
*
* ========================================
*/
//MorseTree Binary tree that represent the morse language
var MorseTree = initTree()
//Encode message to morse
func Encode(message *string) (morse *string, err error){
*message = strings.ToUpper(*message)
code := ""
morse = &code
for _, letter := range *message{
l := string(letter)
if l == " " {
code += "/"
continue
}
if mCode, er := GetCode(l); er == nil {
code += mCode
code += " "
}else{
return nil, er
}
}
//Just beautify the code
code = strings.Replace(code, " /", "/", len(code))
if code[len(code)-1:] == " "{
code = code[:len(code)-1]
}
return morse, nil
}
//Decode morse to message
func Decode(morse *string) (message *string, err error) {
if morse == nil {
return nil, errors.New("morse is nil")
}
msg := ""
message = &msg
wordsStr := strings.Split(*morse, "/")
out := make([]string, len(wordsStr))
wg := new(sync.WaitGroup)
//prepare go routine to work on each word of the morse code
for i, word := range wordsStr{
wg.Add(1)
go decodeWord(wg, &out[i], word)
}
//Waiting each go routine
wg.Wait()
//Create the final message
for _, v := range out {
msg += v + " "
}
//Beautify the final message
if (*morse)[len(*morse)-1:] == " "{
*morse = (*morse)[:len(*morse)-1]
}
if msg[len(msg)-1:] == " "{
msg = msg[:len(msg)-1]
}
return message, nil
}
//GetLetter morse to message
func GetLetter(morse string) (letter string, err error){
if node := MorseTree.search(morse); node != nil {
return node.Letter, nil
}
return "", errors.New("not found")
}
// GetCode get the code of the given letter
func GetCode(letter string) (code string, err error){
var path []string
MorseTree.Groot.path(&path, letter)
//Reverse the path
return func() (string, error){
for i, j := 0, len(path)-1; i < j; i, j = i+1, j-1{
path[i], path[j] = path[j], path[i]
}
code = strings.Join(path, "")
if code == "" {
return "", errors.New("code not found for : " + "\"" + letter + "\"")
}else {
return code, nil
}
}()
}