forked from kerszl/demorse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemorse.py
133 lines (115 loc) · 2.96 KB
/
demorse.py
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
#!/usr/bin/python3
# v0.0.1 25.09.2021
# Kerszi (Szikers)
# git - https://github.com/kerszl/demorse
import argparse
import sys
import re
MORSE_CODE = {
".-": "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",
".-.-.-": ".",
"---...": ":",
"--..--": ",",
"-.-.-.": ";",
"..--..": "?",
"-...-": "=",
".----.": "'",
"-..-.": "/",
"-.-.--": "!",
"-....-": "-",
"..--.-": "_",
".-..-.": "\"",
"-.--.": "(",
"-.--.-": ")",
"...-..-": "$",
".-...": "&",
".--.-.": "@"
}
def get_param():
# exception for "--" in head of string
if len(sys.argv) == 3:
if sys.argv[2] == '--':
return '--'
description = """The program converts the morse code to plain text.
You can throw in some text like this "\\..../.\\.-.. .-..|---".
The program will cut unnecessary characters and convert
it into plain text like: ".... . .-.. .-.. ---" and
convert it to "hello".
Example: demorse -t ".... . .-.. .-.. ---"
"""
parser = argparse.ArgumentParser(description=description)
parser.add_argument('-t', '--text', type=str, help="Text to decode", default=None, nargs=argparse.REMAINDER)
args = parser.parse_args()
if args.text:
return str(args.text)
else:
parser.print_help()
return None
def decode_morse_code(pure_morse_code_2_decode):
decode_string = ""
morse_string = " ".join(pure_morse_code_2_decode)
for i in pure_morse_code_2_decode:
decode_string += MORSE_CODE.get(i, ' ')
if len(decode_string) > 0:
return morse_string, decode_string
else:
return None
def leave_plain_text(morse_string_code):
# Clean from unnecessary signs
chars = re.findall('[.]|-| ', morse_string_code, re.DOTALL)
if not chars:
return None
decode_morse_string = ''.join(chars)
# return string2list
return decode_morse_string.split(' ')
# ----
def just_decode(text_to_decode):
if text_to_decode:
pure_text = leave_plain_text(text_to_decode)
if pure_text:
morse_string, decode_morse_string = decode_morse_code(pure_text)
if decode_morse_string:
print("Morse :", morse_string)
print("Demorse :", decode_morse_string)
else:
print("No real morse code")
else:
print("Nothing to decode")
if __name__ == '__main__':
text2decode = get_param()
just_decode(text2decode)