def search (search_code): for letter, code in morse.items(): #if the searched for string (code) shows up in the tuples anywhere if search_code == code: #create a new string of the keys associated with this value in tuples return letter return "" #for debugging, to catch anything that isn't in dict
def morse_d(phrase): morse_d = dict([(val[1], val[0]) for val in morse.items()]) phrase = [word.split() for word in phrase.split('/')] words = [] for word in phrase: translated = '' for letter in word: translated += morse_d[letter] words.append(translated) return ' '.join(words)
def read_code(file_name): """Take Morse Code message and turn it into English """ # Get the morse code from a preexisting file load_file = open(file_name, "r") # Get the morse sentence out of the file morse_message = load_file.read() load_file.close() # Initialize the english sentence complete_sentence = "" # Split the morse into words depending # on how many spaces each value has between the next value (7 is the key) regex = "\s{7}" # Split the sentence into a list of words list_of_words = re.split(regex, morse_message) # Loop through each word for each_word in list_of_words: #print("This is a word:", each_word) # Split the morse words into lists of morse letters. regex = "\s{3}" list_of_letters = re.split(regex, each_word) # Loop through the morse letters for each_letter in list_of_letters: #print("This is the letters in the word:", each_letter) # Once all values have been grouped appropriately # Translate the values into their english counterparts for character, code in morse.items(): if code == each_letter: complete_sentence += character #print("This is the letter in english:", character) # Add a space between the words complete_sentence += " " # Return the translated message return complete_sentence
def morse_to_message(message): translation = "" # Break the message into words words = re.split("\s{7}", message) # For every word in the message for word in words: # Break the word into letters letters = re.split("\s{3}", word) # For every letter in a word for char in letters: # Find the key for the morse letter for letter,code in morse.items(): if code == char: translation += letter # Add a space between each word translation += " " return translation
def read_code(morse_file): """ Accepts content in a string from specified morse file and returns English translation in a string""" # This creates a function that accepts a filename and # returns the English translation of the contents of the file. untranslated_code = open_message(morse_file) translated_code = "" # Split at 7 space block for list of words word_list = re.split(" ", untranslated_code) # 10 spaces rather # than 7 spaces to account for the three spaces between symbols # for each word in list of words for word in word_list: # split each word at 3 space block to create list of symbols symbol_list = re.split(" ", word) # convert each symbol into equivalent letter for symbol in symbol_list: for letter, char in morse.items(): if symbol == char: translated_code += letter translated_code += " " return translated_code