用Python进行摩尔斯码翻译器
通过输入一系列电脉冲,常表示为一个短脉冲(称为“点”)和一个长脉冲(称为“划线”),可以使用摩尔斯码方法进行文本消息的传输。Samuel F. B. Morse在19世纪40年代创造了这种代码,用于他的电报创新中,这是第一台成功使用电磁波进行长距离通信的设备。早期的电报员会按下一个键,上升和下降来传送一系列字符,接收电报员可以从纸带上读取。这个电报员经常位于连接数英里的电报杆线的铁路站点上(后来的操作员通过听取传输内容来学会阅读)。旧版使用点(或,按照电报员听到的声音,称为“di”)表示键下降,后面是字母,而破折号表示连续按下两次的键下降(称为“da”或“di-di”)。破折号、点或其他任何组合都表示字符的文本等效形式。
在19世纪末,摩尔斯码被称为国际摩尔斯电码或欧洲码,随着新的键入技术的普及,一些字母用点和划线的方式稍有不同。但即使到了20世纪60年代,美国仍在使用美国摩尔斯码。关于摩尔斯码的起源,有几个传说。据传Samuel Morse曾前往一家印刷商的公司,计算印刷商对每个字母的印刷字体数量。之后,他将这些数字解释为普通英语文本中每个字母相对频率的粗略估计。他设置了摩尔斯码,使得最短的符号与最常用的字母相对应。因此,英语中使用频率最高的字母E和T分别由一个点或一个划线代表。数字、标点符号和最少见的字母,如J和Y,都具有较长和更复杂的表示形式。
当存在干扰、噪音或衰落时,摩尔斯码提供了一种缓慢但可靠的发送和接收无线文本消息的方式。这主要是因为它的简单二进制编码(键下降或键上升)带宽非常小。人耳和大脑也非常适合作为数字接收设备。业余无线电操作员、陆地电报员和军事无线电操作员在现代仍然偶尔使用摩尔斯码。
尽管莫尔斯电码是专为电报而创建的,但人们发现了莫尔斯电码的其他用途。由于19世纪90年代发明了无线电报设备,编码通信现在可以通过无线电波传输。国际莫尔斯电码在一些纯视觉媒介中也有着悠久的历史。1867年,船只开始使用他们的船上闪光灯相互发送信号。杰里迈亚·登顿的案件也是如此。他是美国军方(后来成为参议员)在越南被俘的飞行员。在北越电视上露面时,登顿被迫宣称他的俘虏在敌后对他很好。然而,他的眼睛透露出了不同的故事。他以莫尔斯电码表示“折磨”的模式眨眼睛。该码在20世纪后期主要被淘汰。自1995年以来,美国海岸警卫队不再将其用于官方用途,现代船只更依赖卫星通信设备。然而,智能专业人员仍在接受美国海军的培训以学习这种代码。支持莫尔斯电码的另一个组织是国际莫尔斯电码保护协会,这是一个拥有数千名成员的全球业余无线电操作员联盟。因此,即使点和划的时代可能已经结束,莫尔斯电码仍在使用中。不需要紧急信号。
通过敲击必要数量的点和划,并在适当的位置进行适当的休止,可以激活它。单词之间的停顿比单词内部的停顿要长。使用莫尔斯电码发送的是什么单词或数字取决于间隔的长度以及使用的点和划的数量。学习莫尔斯电码需要熟悉这些符号以及每个点和划之间的间隔。划是类似连字符的长水平线,而点则是普通的句号。
莫尔斯电码在最简单的形式下是一种早期通信系统,用点、划或长短声响与拉丁字母表中的每个字母对应。通常,这些消息是通过光信号或电报(也称为电钥)传输的。
现在有许多不同类型的莫尔斯电码,包括用于使用拉丁字母表的语言的国际莫尔斯电码、日本版本的莫尔斯电码、和韩国莫尔斯电码SKATS等等。莫尔斯电码可以通过光或脉冲来传输消息。在萨缪尔·莫尔斯时代,电报最常用来传递脉冲消息。电报键有时被称为电报,根据使用电报键“敲出”的消息,传输电脉冲。随后,电报员会使用一系列的点击来键入消息,点击依赖于消息中的单词如何拼写。另一端的操作员会听到这些点击声,并将它们转换回语言。
莫尔斯电码使用点和划(也称为点和划)将字母表中的每个字母转换为短信号和长信号。划的脉冲长度等于三个点。当莫尔斯电码用于无线电时,点和划最终被称为“滴”和“哒”,这是根据无线电脉冲的声音来命名的。
世界各地的文明在历史上曾试图创建远程通信技术,他们使用旗帜、灯光、音乐和烟信号进行通信。这些通信技术被称为旗语系统。
代码:
# A sample python script representing how to use the morse code encoder and decoder in python, we have written separate functions for the encoding and decoding operations, along with the supporting functions for those operating functions.
class cipher_decoder_encoder:
# A constructor is written to initialize the class variables which are getting used across the various other functions which are written inside this class. All the class variables are initialized with None in the constructor.
def __init__(self):
self.morse_code_dictionary = None
self.message_to_encode = None
self.message_to_decode = None
self.encoded_message = None
self.decoded_message = None
# A function is written to initialize the morse code dictionary, this fucntions initializes the morse code dict which is getting used to encode or decode the various input strings across the rest of the functions of this class.
def initialize_morse_code_dict(self):
# Dictionary representing the morse code chart
self.morse_code_dictionary = MORSE_CODE_DICT = {'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': '-----', ', ': '--..--', '.': '.-.-.-',
'?': '..--..', '/': '-..-.', '-': '-....-',
'(': '-.--.', ')': '-.--.-'}
# This function is written to get the encoding message from the user which will be encoded using the encrypt function of this class.
def get_message_to_encode(self):
print("Enter the message that you want to encode using Morse code cipher::")
data = input("")
self.message_to_encode = data
# This is the actual function that will actually perform the encoding or encrypting operation on the input string which is taken as input in the previous function. This function reads the input string char by char and then goes on converting each char to the equivalent morse code char, this conversion of the ordinary char to the morse code char is done with the help of the morse code dictionary which was initialized at the beginning of the program, this conversion of the normal to the equivalent morse code char goes on until the chars in the input string are exhausted, once all the chars in the input string are converted to the equivalent morse code chars, then those converted morse code chars are attached together and a final morse code string is formed and in the end, this string is stored in the relevant class variable.
# Function to encrypt the string
# according to the morse code chart
def encrypt(self):
message = self.message_to_encode
cipher = ''
for letter in the message:
if letter != ' ':
# Looks up the dictionary and adds the
# corresponding morse code
# along with a space to separate
# morse codes for different characters
cipher += self.morse_code_dictionary[letter] + ' '
else:
# 1 space indicates different characters
# and 2 indicate different words
cipher += ' '
self.encoded_message = cipher
# This function is used to print the string encoded in the previous step. After the successful conversion of the input string to the morse code string this fucntion prints the converted morse code string.
def print_encoded_message(self):
print("Encoded message is: {}".format(self.encoded_message))
# This function is written to take input from the user the morse code which the user wants to decode or decrypt using the morse code dictionary. The input string is stored in the relevant class variable which is later used to get the string that needs to be decrypted or decoded.
def get_message_to_decode(self):
print("Enter the message that you want to decode using Morse code cipher::")
data = input("")
self.message_to_decode = data
# This is the actual function that will actually perform the decoding or decrypting operation on the input string which is taken as input in the previous function. This function reads the morse string char by char and then goes on converting each char to the equivalent normal char, this conversion of the morse char to the ordinary char is done with the help of the morse code dictionary which was initialized at the beginning of the program, this conversion of the morse code char to the equivalent standard char goes on until the chars in the morse string are exhausted, once all the chars in the input morse string are converted to the equivalent normal chars, then those converted chars are attached together and a final decoded string is formed and in the end, this string is stored in the relevant class variable.
# Function to decrypt the string
# from morse to English
def decrypt(self):
message = self.message_to_decode
# extra space added at the end to access the
# last morse code
message += ' '
decipher = ''
citext = ''
for letter in the message:
# checks for space
if (letter != ' '):
# counter to keep track of space
i = 0
# storing morse code of a single character
citext += letter
# in case of space
else:
# if i = 1 that indicates a new character
i += 1
# if i = 2 that indicates a new word
if i == 2:
# adding space to separate words
decipher += ' '
else:
# accessing the keys using their values (reverse of encryption)
decipher += list(self.morse_code_dictionary.keys())[list(self.morse_code_dictionary
.values()).index(citext)]
citext = ''
self.decoded_message = decipher
# This function is used to print the string decoded in the previous step. After the successful conversion of the input morse string to the normal string this fucntion prints the converted string.
def print_decoded_message(self):
print("Decoded message is: {}".format(self.decoded_message))
#Now in the main function, an object of the above-written class is created and that object is used to call the various functions which are written inside the class. The main functions provide the user with a menu that consists of various options from which the user can select, the various options that are shown to the user from which you can select one option are, to initialize the morse code dictionary, to enter the string you want to encode/cipher using the morse code technique, To encode the input string, To print the encoded string, To enter the string you want to decode/decipher using the morse code technique, To decode the input string, To print the decoded string after printing all these options the user can select from the any of it and depending upon the choice selected by the user the relevant function is called and the steps inside that function are performed if the called function required some of the input then the code asks the user for that particular input and continues the execution of the code. This displaying of the menu goes on until the user selects the last or the final option which is the exit from the code option. Another option or condition that can cause the code execution to be halted or stopped is that any exception is faced in the code execution.
def main():
morse_handler = cipher_decoder_encoder()
while(True):
# from the listed below the list of operations select any one of the operations
print("Go on selecting any one of the options")
print("0. To initialize the morse code dictionary.")
print("1. To enter the string you want to encode/cipher using the morse code technique.")
print("2. To encode the input string.")
print("3. To print the encoded string.")
print("4. To enter the string you want to decode/decipher using the morse code technique.")
print("5. To decode the input string.")
print("6. To print the decoded string.")
print("7. To exit from the code execution.")
menu_choice = input()
menu_choice = int(menu_choice)
if menu_choice == 0:
morse_handler.initialize_morse_code_dict()
elif menu_choice == 1:
morse_handler.get_message_to_encode()
elif menu_choice == 2:
morse_handler.encrypt()
print("Input string encoded successfully")
elif menu_choice == 3:
morse_handler.print_encoded_message()
elif menu_choice == 4:
morse_handler.get_message_to_decode()
elif menu_choice == 5:
morse_handler.decrypt()
print("Input string decoded successfully")
elif menu_choice == 6:
morse_handler.print_decoded_message()
elif menu_choice == 7:
sys.exit()
print(
"For printing again the choice menu enter y if want to halt enter [n]")
continue_or_exit = input()
if continue_or_exit == 'y' or continue_or_exit == 'Y':
pass
elif continue_or_exit == 'n' or continue_or_exit == 'N':
sys.exit()
if __name__ == '__main__':
main()
输出:
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
0
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
1
Enter the message that you want to encode using Morse code cipher::
HELLO MY NAME IS SAMUEL
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
2
Input string encoded successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
3
Encoded message is: .... . .-.. .-.. --- -- -.-- -. .- -- . .. ... ... .- -- ..- . .-..
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
4
Enter the message that you want to decode using Morse code cipher::
.... . .-.. .-.. --- -- -.-- -. .- -- . .. ... ... .- -- ..- . .-..
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
5
Input string decoded successfully
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
6
The decoded message is: HELLO MY NAME IS SAMUEL
For printing again the choice menu enter y if want to halt enter [n]
y
Go on selecting any one of the options
0. To initialize the morse code dictionary.
1. To enter the string you want to encode/cipher using the morse code technique.
2. To encode the input string.
3. To print the encoded string.
4. To enter the string you want to decode/decipher using the morse code technique.
5. To decode the input string.
6. To print the decoded string.
7. To exit from the code execution.
7
输出解释:
在我们写的主函数中,创建了一个上述类的对象,该对象用于调用在类中编写的各种函数。主函数为用户提供了一个菜单,其中包含各种选项,用户可以从中选择。向用户显示的各种选项包括:初始化莫尔斯码字典,输入要使用莫尔斯码技术对其进行编码/加密的字符串,对输入字符串进行编码,打印编码后的字符串,输入要使用莫尔斯码技术对其进行解码/解密的字符串,解码输入字符串,在打印所有这些选项后,用户可以从中选择一个选项,根据用户选择的选项调用相关的函数,并执行该函数内部的步骤。如果所调用的函数需要某些输入,则代码会要求用户提供该特定输入,并继续执行代码。这种菜单的显示将继续进行,直到用户选择最后一个选项或退出代码选项。可能导致代码执行停止或停止的另一个选项或条件是在代码执行中遇到任何异常。
莫尔斯码的优点
- 莫尔斯码可以通过较低的功率和带宽发送,而其他传统语音通信工具则无法做到这一点,这使它成为紧急情况下有效的通信方法。此外,莫尔斯码对于秘密对话也是一个有用的工具,因为只有精通莫尔斯码的人才能理解这些消息。
- 学习莫尔斯码可能会为您提供极高的智力丰富度,即使它不再像以前那样具有许多专业用途。通过挑战自己来解码该代码,您可以进行一种思维锻炼,从而提高学习其他有用能力的能力。
- 个人成就:学习新事物,无论其重要性如何,都可以提高您的自信心并给您一种成功的感觉。因此,了解莫尔斯码可能会提高您的整体自信心,这对您生活中的其他方面可能有所帮助。
- 鉴于现代技术的发展,莫尔斯码不再是一个可靠的解决方案,因为它很容易解密。更好的技术可以提供更好的传输信息的途径。莫尔斯码的使用在历史上已被应用于各种环境,包括业余无线电和航空业。莫尔斯码正在帮助具有听力和运动问题的人们使用辅助技术进行交流。
结论
因此,在本文中,我们了解了如何在Python中实现莫尔斯码编码器和解码器。在本文中,我们编写了一个既是莫尔斯码编码器又是解码器的python代码。该代码提供了一系列选项,我们可以从中选择适当的选项作为输入,然后代码将执行该特定操作。