summaryrefslogtreecommitdiff
path: root/morse
blob: bd65ecde8610db1ab4c57c23634eca9ef1e53737 (plain)
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
#!/usr/bin/env python3
#
# Morse.py - A morse code interpreter.
#
# A friend of mine likes to send me messages in Morse code
# sometimes. He's a little weird. He thinks I'm really good at Morse
# code. Actually, I can just do some Python.
#
# Author: Ben Sima <bensima@gmail.com>
# Date: 2015-07-06
# License: MIT
#


import re
import argparse
from typing import Dict


eng = {
    "a" : ".-",
    "b" : "-...",
    "c" : "-.-.",
    "d" : "-..",
    "e" : ".",
    "f" : "..-.",
    "g" : "--.",
    "h" : "....",
    "i" : "..",
    "j" : ".---",
    "k" : "-.-",
    "l" : ".-..",
    "m" : "--",
    "n" : "-.",
    "o" : "---",
    "p" : ".--.",
    "q" : "--.-",
    "r" : ".-.",
    "s" : "...",
    "t" : "-",
    "u" : "..-",
    "v" : "...-",
    "w" : ".--",
    "x" : "-..-",
    "y" : "-.--",
    "z" : "--..",
    " " : "/",
    "0" : "-----",
    "1" : ".----",
    "2" : "..---",
    "3" : "...--",
    "4" : "....-",
    "5" : ".....",
    "6" : "-....",
    "7" : "--...",
    "8" : "---..",
    "9" : "----.",
    "," : ",",
    "!" : "!",
    "?" : "?",
} # type: Dict[str, str]


morse = {v: k for k, v in eng.items()} # type: Dict[str, str]


class InvalidMorseLetter(Exception):
    "Handle errors when processing Morse Code."
    pass


class InvalidEnglishLetter(Exception):
    "Handle errors when processing English."
    pass


def morsep(msg: str) -> bool:
    "Predicate for detecting Morse Code."
    return msg[0][0] in ['.', '-', '/']


def morse_to_eng(msg: str) -> str:
    """Translate Morse code to English.

    Will raise an InvalidMorseLetter if an error during
    the translation occurs."""
    split_msg = re.split(" ", msg) # type: list
    filtered_msg = [e for e in split_msg if e] # type: list
    try:
        return ''.join("{}".format(morse[char]) for char in filtered_msg)
    except KeyError:
        raise InvalidMorseLetter

def eng_to_morse(msg: str) -> str:
    """Translate English into Morse Code.

    Will rais an InvalidEnglishLetter if an error during
    the translation occurs."""
    try:
        return ''.join("{} ".format(eng[char]) for char in msg)
    except KeyError:
        raise InvalidEnglishLetter


def decode(msg: str) -> str:
    "Main control flow."
    lower_msg = msg.lower()
    if morsep(lower_msg) == True:
        return morse_to_eng(lower_msg)
    else:
        return eng_to_morse(lower_msg)


if __name__ == '__main__':
    parser = argparse.ArgumentParser(description='Translate English to Morse code, and vice versa.')
    parser.add_argument('-v', '--version', help='Show the version.', action='version',
            version='Morse 0.1')
    parser.add_argument('msg', type=str, nargs=1,
                        help="""The message to be decoded.
                        Input type will be automatically detected and converted.
                        Must be wrapped in quotes.""")

    args = parser.parse_args()
    out = decode(args.msg[0]) # type: str
    print("{}".format(out))