import csv

alphabet = [["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", "--.."]]

dictDE_file = open("wordlist-german.txt", "r")
dictEN_file = open("wordlist-english.txt", "r")

reader = csv.reader(dictDE_file)
dictDE = [row[0].upper() for row in reader]
reader = csv.reader(dictEN_file)
dictEN = [row[0].upper() for row in reader]
dictionary = dictEN

morse_cipher = "..-..--..-......-"


def latin_to_morse(word):
    result = ""
    for letter in word:
        for latin, morse in alphabet:
            if letter == latin:
                result += morse
    return result


def guess_chars(morse_text, plain_text=""):
    if morse_text == "":
        char_guesses.append(plain_text)
    else:
        for latin, morse in alphabet:
            if morse == morse_text[0:len(morse)]:
                guess_chars(morse_text[len(morse):len(morse_text)], (plain_text + latin))


def guess_vocabs(word, phrase=""):
    if word == "":
        vocab_guesses.append(phrase)
    else:
        for vocab in dictionary:
            if vocab == word[0:len(vocab)]:
                guess_vocabs(word[len(vocab):len(word)], (phrase + vocab + " "))


char_guesses = []
guess_chars(morse_cipher)
print("Chars guessed. char_guesses complete.")

guess_list_length = len(char_guesses)
for index, word in enumerate(char_guesses):
    print("(", index, "/", guess_list_length, ") - ", word, "...", end="")
    vocab_guesses = []
    guess_vocabs(word)
    print("\r", end="")
    if vocab_guesses:
        print("Found something: ", vocab_guesses)
