Skip to content

Morse Code Converter Program. #85

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Morse-Code-Converter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Morse code converter
### What is Morse Code?
Morse Code is a character encoding scheme that allows operators to send messages using a series of electrical pulses represented as short or long pulses, dots, and dashes in other words.

## About
Using this program one can convert the english text or sentence to morse code and vice versa.
This code can be run in terminal or in tkinter window.

# Running this code
One can run this code in terminal as well as in tkinter window.
### Running in terminal.
```bash
python3 main.py
```
### Running through tkinter app window.
```bash
python3 converter_app.py
```
41 changes: 41 additions & 0 deletions Morse-Code-Converter/converter_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from tkinter import *
from morse import MorseCode

# translate object
translate = MorseCode()

# --------------------------------BUTTON FUNCTIONS------------------------------
# Button function morse
def code():
text = e.get().strip()
print(text)
e.delete(0, END)
text = translate.to_morse(text)
e.insert(0, text)

# Button function english
def decode():
text = e.get().strip()
print(text)
e.delete(0, END)
e.insert(0, translate.to_english(text))

# Button function clear
def on_clear():
e.delete(0, END)

# -----------------------------------UI SETUP-----------------------------------
root = Tk()
root.title("Morse Code Converter")

# Entry field
e = Entry(root, width=35, borderwidth=5)
e.grid(row=0, column=0, columnspan=3, pady=10, padx=10)
e.focus()

code = Button(root, text="Code", command=code).grid(row=1, column=0)
clear = Button(root, text="Clear", command=on_clear).grid(row=1, column=1)
decode = Button(root, text="Decode", command=decode).grid(row=1, column=2)
exit = Button(root, text="EXIT", command=root.quit).grid(row=2, column=0, columnspan=3)

root.mainloop()
57 changes: 57 additions & 0 deletions Morse-Code-Converter/data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
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":"--..",
"1":".----",
"2":"..---",
"3":"...--",
"4":"....-",
"5":".....",
"6":"-....",
"7":"--...",
"8":"---..",
"9":"----.",
"0":"-----",
".":".-.-.-",
",":"--..--",
"?":"..--..",
"'":".----.",
"!":"-.-.--",
"/":"-..-.",
"(":"-.--.",
"&":".-...",
":":"---...",
";":"-.-.-.",
"=":"-...-",
"+":".-.-.",
"-":"-....-",
"_":"..--.-",
"\"":".-..-.",
"$":"...-..-",
"@":".--.-.",
"¿":"..-.-",
"¡":"--...-"
}
14 changes: 14 additions & 0 deletions Morse-Code-Converter/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from morse import MorseCode

# Creating a traslate object using MorseCode class
translate = MorseCode()

# Converting English to morse (encode)
text = input("enter a message to convert to morse code: ")
morse_code = translate.to_morse(text)
print(morse_code)

#Converting Morse to english (decode)
text = input("enter morse code to convert to actual message: ")
english_text = translate.to_english(text)
print(english_text)
63 changes: 63 additions & 0 deletions Morse-Code-Converter/morse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from data import morse_code

class MorseCode:
def __init__(self):
self.in_morse = ""
self.in_english = ""

def to_morse(self, sentence):
'''
Takes one required parameter sentence of type string and
converts it into the equivalent morse code.
'''
self.in_morse = ""
sentence = sentence.split(" ")
# print(sentence)

morse_translation = []
for word in sentence:
# Converting each word to a list of characters
word_ = list(word)

# Matching every character with morse code in data.py
for letter in word_:
morse_translation.append(morse_code[letter.lower()])

# Adding a forwars slash at end of each word except the last word
if sentence.index(word) != len(sentence)-1:
morse_translation.append("/")

# Joining the final list to make a string of morse code characters
self.in_morse = " ".join(morse_translation)

return self.in_morse

def to_english(self, code_in_morse):
'''
Converts morse code to english takes one required parameter
code_in_morse as a string
'''
self.in_english = ""
# Checking if the entered code has "/" as seperator or not?
if "/" in code_in_morse:
code_list = code_in_morse.split(" / ")
else:
code_list = code_in_morse.split(" ")

# Creating a list for morse code to convert it later to english
morse_list = []
for code in code_list:
code = code.split(" ")
morse_list.append(code)

# Looping through the dictionary of morse code and replacing morse to letter
for word in morse_list:
for letter in word:
for key, value in morse_code.items():
if letter == value:
self.in_english += key

# After each word concatinating the white space
self.in_english += " "

return self.in_english
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,4 @@ The contribution guidelines are as per the guide [HERE](https://github.com/larym
| 44 | [Sudoku Solver](https://github.com/Mannuel25/Python-project-Scripts/tree/main/SudokuSolver) | [Ruben Grande Muñoz](https://github.com/RgrMz) |
| 45 | [Duplicate File Remover](https://github.com/mas-designs/Python-project-Scripts/tree/main/Remove%20Duplicate%20Files%20in%20Folder) | [Michael Stadler](https://github.com/mas-designs) |
| 46 | [Image Divider](https://github.com/larymak/Python-project-Scripts/tree/main/ImageDivider) | [Rajarshi Banerjee](https://github.com/GSAUC3) |)
| 47 | [Morse Code Converter](https://github.com/HarshitRV/Python-project-Scripts/tree/main/Morse-Code-Converter) | [HarshitRV](https://github.com/HarshitRV) |)