Building a class for morse code decoder in the first task


Assignment: Building a Morse Code Decoder with Python

1. Morse Code Interpreter

The Morse Code System

As we have seen in the first assignment, the current International Morse Code system encodes a range of characters, including the ISO basic Latin alphabets (A-Z), some additional Latin letter representing accents, the Arabic numerals (0-9), and a small set of punctuations and procedural signals (known as prosigns).

Each character is presented in Morse Code as an unique sequence of ‘dot' (.) and ‘dashes' (_). Figure 1 presents a subset of the characters represented in Morse Code.

907_International Morse Code representation.jpg
Figure 1: International Morse Code representation (letters and numerals)

Each character represented by the Morse Code system is separated by a single space, and each word (i.e. a combination of multiple characters) is separated by three spaces.

The following is an example that includes a punctuation character (the question mark) at the end of the Morse Code sequence. (Note that the punctuation is cosidered as a single word with a single character.)

1. S e n t e n c e : WHO AM I ?
2. Morse S e q u e n c e : . . . . . ._ . .

How Morse Code is represented in this assignment?

In this assignment, we are going to adopt the same set of representation for the Morse Code used in the first assignment based on the binary digits. The ‘dots' are represented by the digit 0 and the ‘dashes' are represented by the digit 1. As for the spaces, they are represented by the character ‘*'.

Note that our representation of Morse Code encodes the standard 26 letters (i.e. ‘A' to ‘Z) and the 10 numerals (i.e. ‘0' to ‘9'), with three additional punctuation characters (i.e. the period ‘.'; the comma ‘,'; and the question mark ‘?').

Table 1 defines the set of Morse Code representation used in this assignment.

Character

Morse Code

Character

Morse Code

Character

Morse Code

A
B
C
D
E
F
G
H
I
J
K
L
M

01
1000
1010
100
0
0010
110
0000
00
0111
101
0100
11

N
O
P
Q
R
S
T
U
V
W
X
Y
Z

10
111
0110
1101
010
000
1
001
0001
011
1001
1011
1100

0
1
2
3
4
5
6
7
8
9
.
,
?

11111
01111
00111
00011
00001
00000
10000
11000
11100
11110
010101
110011
001100

Table 1: Morse Code representation for this assignment (letters, numerals, and punctuations)

For the same example given earlier, our Morse code sequence should read as below:

1. S e n t e n c e : WHO AM I ?
2. Morse S e q u e n c e : 0 1 1 ∗ 0 0 0 0 ∗ 1 1 1 ∗ ∗ ∗ 0 1 ∗ 1 1 ∗ ∗ ∗ 0 0 ∗ ∗ ∗ 0 0 1 1 0 0

Also note that, we assume the Morse code sequences to be decoded in this assignment represent proper words and sentences in English. Each Morse code sequence should represent a sentence in English.

Task 1: Building a class for Morse Code decoder

In the first task, you are required to define a class that serves as the Morse Code decoder. This class should have one instance variable which is a dictionary structure that represents each of the Morse Code characters (presented in Table 1) as a string sequence of binary digits (0 and 1). This decoder class is used for decoding any Morse Code sequences.

The implementation of this decoder class should include the following three methods:

• init (self):

This is the constructor that is required for creating decoder instances. You should define and initialise the dictionary structure for the Morse Code representation (i.e. the instance variable) in the constructor.

• str (self):

Re-define this method to present the Morse code representation table (i.e. the dictionary structure) in a readable format. You should return a formatted string in this method.

• decode(self, morse_code_sequence):

This is the method that performs the decoding process. This method should accept a Morse Code sequence as the argument, and attempt to decode it. The decoded message should be returned as a string. (If you encountered an invalid character while decoding, return an error message instead of the partially decoded message.)

Note: The Morse Code sequence should terminate with one of the three punctuation characters, i.e. the period ‘.'; the comma ‘,'; and the question mark ‘?'.

You should name this class as "Decoder" and the Python file as StudentID_decoder.py.

Task 2: Building a class for analysing decoded characters

In this task, you are required to define a class for analysing the number of occurrences for each of the letters (i.e. ‘A' to ‘Z) and numerals (i.e. ‘0' to ‘9') from the decoded sequences. This class should have one instance variable which is a dictionary structure that is used for keeping track of the number of occurrences for each of the letters and numerals decoded by the Morse Code decoder in Task 1.

The implementation of this character analyser class should include the following three methods:

• init (self):

This is the constructor that is required for creating instances of this class. You should define and initialise the dictionary structure for characters (i.e. the instance variable) in the constructor.

• str (self):

Re-define this method to present the number of occurrences for each of the letters and numerals in a readable format. You should return a formatted string in this method.

• analyse_characters(self, decoded_sequence):

This is the method that performs the analysis on the decoded sequences at the character level. This method should accept a decoded sequence as the argument, and attempt to count the occurrences for each of the letters and numerals encountered in the given decoded sequence. The counts should be updated in the dictionary structure defined in the constructor.

Note: You should not consider the punctuation characters in this task as they will be handled in Task 4 below.

You should name this class as "CharacterAnalyser" and the corresponding Python file as StudentID_character.py.

Task 3: Building a class for analysing decoded words

In this task, you are required to define a class for analysing the number of occurrences for each of the English words from the decoded sequences. This class should have one instance variable which is a dictionary structure that is used for keeping track of the number of occurrences for each word decoded by the Morse Code decoder in Task 1.

The implementation of this word analyser class should include the following three methods:

• init (self):

This is the constructor that is required for creating instances of this class. You should define and initialise the dictionary structure for words (i.e. the instance variable) in the constructor.

• str (self):

Re-define this method to present the number of occurrences for each word in a readable format. You should return a formatted string in this method.

• analyse_words(self, decoded_sequence):

This is the method that performs the analysis on the decoded sequences at the word level. This method should accept a decoded sequence as the argument, and attempt to count the occurrences for each word encountered in the given decoded sequence. The counts should be updated in the dictionary structure defined in the constructor.

Note: Again, you should not consider the punctuation characters in this task as they will be handled in Task 4 below.

You should name this class as "WordAnalyser" and the Python file as StudentID_word.py.

Task 4: Building a class for analysing decoded sentences

Following Task 2 and Task 3, you are required to define a class in this task for analysing the number of occurrences for each type of English sentences from the decoded sequences. This class should have one instance variable which is a dictionary structure that is used for keeping track of the number of occurrences for each type of sentences decoded by the Morse Code decoder in Task 1.

The types of sentence that we are handling in this assignment include: clauses (indicated by the comma), complete sentences (indicated by the period), and questions (indicated by the question mark).

The implementation of this sentence analyser class should include the following three methods:

• init (self):

This is the constructor that is required for creating instances of this class. You should define and initialise the dictionary structure for sentence types (i.e. the instance variable) in the constructor.

• str (self):

Re-define this method to present the number of occurrences for each sentence type in a readable format. You should return a formatted string in this method.

• analyse_sentences(self, decoded_sequence):

This is the method that performs the analysis on the decoded sequences at the sentence level. This method should accept a decoded sequence as the argument, and attempt to count the occurrences for each sentence type (which could be a number of clauses and/or a full sentence or a question) encountered in the given decoded sequence. The counts should be updated in the dictionary structure defined in the constructor.

You should name this class as "SentenceAnalyser" and the corresponding Python file as StudentID_sentence.py.

Task 5: Putting all the classes together

In this final task, we will test all the classes defined above. You should define a function called main() that will drive the flow of execution of the program. Below is a sequence of tasks that should execute within the main() function.

• Create an instance for each of the four classes. You should have a decoder, a character analyser, a word analyser, and a sentence analyser.

• Prompt the user to input any random sequences of Morse Code. The Morse Code sequences can be of any length but with the minimum of one set of three consecutive ‘*'. (Note that the user should be allowed to input multiple Morse Code sequences not until the user indicates that he/she would like to terminate.)

• Invoke the corresponding method in the decoder to perform the decoding on each of the Morse Code sequences provided by the user. (All the decoded sequences should be displayed on the console.)

• Invoke the corresponding method in the character analyser to determine the total number of occurrences for each of the letters and numerals appeared in all the decoded sequences.

• Repeat the same task for the total number of occurrences for each word and each sentence type encountered in all the decoded sequences.

• Display all the analysis results on the console in a readable format.

Note: Some of the tasks above may be implemented as functions. You should make your own decision with respect to this. You should also consider designing a menu with options allowing the user to select which level of analysis is intended (character, word or sentence).

You should name the Python file of this task as StudentID_main.py.

Attachment:- Python-Assigignment.rar

Request for Solution File

Ask an Expert for Answer!!
Python Programming: Building a class for morse code decoder in the first task
Reference No:- TGS02741253

Expected delivery within 24 Hours