Created
November 25, 2020 07:31
-
-
Save py563/9ddaec28173aa6130eb059500e65f2b6 to your computer and use it in GitHub Desktop.
Text to Morse Code Conversion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MorseCodeTranslator: | |
# Code copied from https://pythonise.com/categories/python/python-morse-code-translator | |
# Author Julian Nash --> https://github.com/Julian-Nash/python-morse-code-translator | |
# International morse code (sample) | |
morse = { | |
# Letters | |
"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": "--..", | |
# Numbers | |
"0": "-----", | |
"1": ".----", | |
"2": "..---", | |
"3": "...--", | |
"4": "....-", | |
"5": ".....", | |
"6": "-....", | |
"7": "--...", | |
"8": "---..", | |
"9": "----.", | |
# Punctuation | |
"&": ".-...", | |
"'": ".----.", | |
"@": ".--.-.", | |
")": "-.--.-", | |
"(": "-.--.", | |
":": "---...", | |
",": "--..--", | |
"=": "-...-", | |
"!": "-.-.--", | |
".": ".-.-.-", | |
"-": "-....-", | |
"+": ".-.-.", | |
'"': ".-..-.", | |
"?": "..--..", | |
"/": "-..-.", | |
} | |
def translate_morse(self, morse, strict=True): | |
""" | |
Translates morse code to text using a small set of Internation Morse code. | |
Accepts: | |
morse (str): A string of morse code to translate | |
strict (bool): If True, parse and return morse code containing 4 spaces | |
Returns: | |
str: A translated string of text | |
""" | |
if morse == "": | |
return "You must provide a string of text to translate" | |
if " " in morse: | |
if strict: | |
return "Unable to translate morse code. Found 4 spaces in morse code string" | |
else: | |
morse.replace(" ", " ") | |
translation = "" | |
words = morse.split(" ") | |
for morse_word in words: | |
chars = morse_word.split(" ") | |
for char in chars: | |
for k, v in self.morse.items(): | |
if char == v: | |
translation += k | |
translation += " " | |
return translation.rstrip() | |
def translate_text(self, text): | |
""" | |
Translates text to morse code using a small set of Internation Morse code. | |
Accepts: | |
text (str): A string of text to translate | |
Returns: | |
str: A string translated to Morse code | |
""" | |
if text == "": | |
return "You must provide a morse code string to translate" | |
translation = "" | |
words = text.split(" ") | |
for word in words: | |
w = list() | |
for char in word: | |
if char.lower() in self.morse: | |
w.append(self.morse[char.lower()]) | |
translation += " ".join(w) | |
translation += " " | |
return translation.rstrip() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from morse import MorseCodeTranslator | |
translator = MorseCodeTranslator() | |
text = "This string has been translated to morse code and back again" | |
# Translate text to morse code | |
morse = translator.translate_text(text) | |
# Translate morse code to text | |
translated_text = translator.translate_morse(morse) | |
print(morse) | |
print(translated_text) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment