Documentation
¶
Overview ¶
Package morsetrie implements trie-based decoding for Morse code.
Package morsetrie implements trie-based decoding for Morse code.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrUnexpectedChar = errors.New("unexpected character in morse input")
ErrUnexpectedChar is returned when the Morse code string contains unsupported characters.
var StaticTrie = &Trie{ Nodes: []Node{ {Val: 0, Child: [2]int16{1, 3}}, {Val: 'E', Child: [2]int16{9, 2}}, {Val: 'A', Child: [2]int16{19, 17}}, {Val: 'T', Child: [2]int16{4, 13}}, {Val: 'N', Child: [2]int16{5, 7}}, {Val: 'D', Child: [2]int16{6, 25}}, {Val: 'B', Child: [2]int16{36, 56}}, {Val: 'K', Child: [2]int16{8, 26}}, {Val: 'C', Child: [2]int16{-1, -1}}, {Val: 'I', Child: [2]int16{15, 10}}, {Val: 'U', Child: [2]int16{11, 31}}, {Val: 'F', Child: [2]int16{12, -1}}, {Val: 'É', Child: [2]int16{-1, -1}}, {Val: 'M', Child: [2]int16{14, 21}}, {Val: 'G', Child: [2]int16{27, 23}}, {Val: 'S', Child: [2]int16{16, 24}}, {Val: 'H', Child: [2]int16{35, 34}}, {Val: 'W', Child: [2]int16{22, 18}}, {Val: 'J', Child: [2]int16{-1, 30}}, {Val: 'R', Child: [2]int16{20, 41}}, {Val: 'L', Child: [2]int16{-1, 54}}, {Val: 'O', Child: [2]int16{38, 28}}, {Val: 'P', Child: [2]int16{-1, 57}}, {Val: 'Q', Child: [2]int16{-1, -1}}, {Val: 'V', Child: [2]int16{-1, 33}}, {Val: 'X', Child: [2]int16{51, -1}}, {Val: 'Y', Child: [2]int16{52, -1}}, {Val: 'Z', Child: [2]int16{37, 44}}, {Val: 0, Child: [2]int16{40, 29}}, {Val: '0', Child: [2]int16{-1, -1}}, {Val: '1', Child: [2]int16{49, -1}}, {Val: 0, Child: [2]int16{47, 32}}, {Val: '2', Child: [2]int16{-1, -1}}, {Val: '3', Child: [2]int16{-1, -1}}, {Val: '4', Child: [2]int16{-1, -1}}, {Val: '5', Child: [2]int16{-1, -1}}, {Val: '6', Child: [2]int16{-1, 50}}, {Val: '7', Child: [2]int16{-1, -1}}, {Val: 0, Child: [2]int16{39, -1}}, {Val: '8', Child: [2]int16{46, -1}}, {Val: '9', Child: [2]int16{-1, -1}}, {Val: 0, Child: [2]int16{42, -1}}, {Val: '+', Child: [2]int16{-1, 43}}, {Val: '.', Child: [2]int16{-1, -1}}, {Val: 0, Child: [2]int16{-1, 45}}, {Val: ',', Child: [2]int16{-1, -1}}, {Val: ':', Child: [2]int16{-1, -1}}, {Val: 0, Child: [2]int16{48, -1}}, {Val: '?', Child: [2]int16{-1, -1}}, {Val: '’', Child: [2]int16{-1, -1}}, {Val: '–', Child: [2]int16{-1, -1}}, {Val: '/', Child: [2]int16{-1, -1}}, {Val: '(', Child: [2]int16{-1, 53}}, {Val: ')', Child: [2]int16{-1, -1}}, {Val: 0, Child: [2]int16{55, -1}}, {Val: '"', Child: [2]int16{-1, -1}}, {Val: '=', Child: [2]int16{-1, -1}}, {Val: 0, Child: [2]int16{58, -1}}, {Val: '@', Child: [2]int16{-1, -1}}, }, }
StaticTrie is a hardcoded version of the ITU M.1677 trie.
Functions ¶
func Decode ¶
Decode provides a package-level decode function that uses the static trie to decode a string.
Example ¶
The Decode function decodes a string of Morse code.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
morseCode := "- .... .. ... / .. ... / -- --- .-. ... . - .-. .. ."
text, err := morsetrie.Decode(morseCode)
if err != nil {
panic(err)
}
fmt.Println(text)
}
Output: THIS IS MORSETRIE
Example (Extended) ¶
The default static trie supports standard alphanumeric characters, punctuation symbols, and the accented 'E'.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
morseCode := `..-.. .-.-.- --..-- ---... ..--.. .----. ` +
`-....- -..-. -.--. -.--.- .-..-. -...- .-.-. .--.-.`
text, err := morsetrie.Decode(morseCode)
if err != nil {
panic(err)
}
fmt.Println(text)
}
Output: É.,:?’–/()"=+@
Example (InvalidInput) ¶
Decode returns ErrUnexpectedChar for invalid input characters.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
text, err := morsetrie.Decode("... --- ...!")
fmt.Println(text)
fmt.Println(err)
}
Output: unexpected character in morse input: !
Example (UnknownSequence) ¶
Unknown Morse sequences are represented by '?' in the output.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
text, err := morsetrie.Decode(".......") // 7 dots — not a valid sequence
fmt.Println(text)
fmt.Println(err)
}
Output: ? <nil>
Example (WordSeparator) ¶
The forward slash '/' is treated as a word separator.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
text, _ := morsetrie.Decode("... --- ... / ... --- ...")
fmt.Println(text)
}
Output: SOS SOS
Types ¶
type Node ¶
type Node struct {
// Val is the decoded rune. If 0, this node is not a valid symbol end.
Val rune
// Child stores indices for the next node.
// Child[0] is the '.' edge; Child[1] is the '-' edge.
// We use int16 to reduce memory footprint.
Child [2]int16
}
Node is a node in the decoding Trie.
type Trie ¶
type Trie struct {
Nodes []Node
}
Trie is a compact, array-backed Morse decode Trie.
func (*Trie) Decode ¶
Decode converts a string of Morse code into its corresponding text representation. It interprets '.' and '-' as Morse signals and uses whitespace (space, tab, newline, carriage return) to delimit encoded characters. The forward slash ('/') is treated as a word separator and is converted to a space in the output.
If the input contains characters other than '.', '-', '/', or whitespace, Decode returns an empty string and ErrUnexpectedChar. Unknown Morse sequences are represented by '?' in the output.
Example ¶
The Decode method can be called on a custom Trie instance, allowing alternative Morse code mappings.
package main
import (
"fmt"
"github.com/pierow2k/morsetrie"
)
func main() {
myTrie := &morsetrie.Trie{
Nodes: []morsetrie.Node{
{Val: 0, Child: [2]int16{1, 2}}, // root
{Val: 'E', Child: [2]int16{-1, -1}}, // just "." -> E
{Val: 'T', Child: [2]int16{-1, -1}}, // just "-" -> T
},
}
result, err := myTrie.Decode(". -")
if err != nil {
panic(err)
}
fmt.Println(result)
}
Output: ET
