def translate() -> None:
sentence = input("enter a sentence to translate: ")
translated_list = []
words = sentence.split()
for word in words:
translated_list.append(translate_word(word))
translated_sentence = " ".join(translated_list)
print(f"translation: {translated_sentence}\n")
def translate_word(word: str) -> str:
with open("dictionary.txt", "r") as file:
content = file.read()
translations = content.split("\n")[:-1]
for translation in translations:
origin_word, translated_word = translation.split(" -> ")
if origin_word == word:
return translated_word
def add_word() -> None:
word = input("enter a word: ")
translation = input("enter translation: ")
with open("dictionary.txt", "a") as file:
sentence = f"{word} -> {translation}"
file.write(f"{sentence}\n")
print(f"added translation {sentence} to dictionary!\n")
def list_all() -> None:
with open("dictionary.txt", "r") as file:
print("dictionary:\n"
f"{file.read()}\n")
actions = {
"1": translate,
"2": add_word,
"3": list_all
}
valid_actions = actions.keys()