I wrote this app with python:
mylist = []
mydict = {}
mystr = ""
num = int(input())
for i in list(range(num)):
mylist.append(input())
for i in list(range(len(mylist))):
temp = mylist[i].split()
mydict[temp[0]] = temp[1:4]
word = input()
word_s = word.split()
for i in mydict.keys():
for x in list(range(len(word_s))):
if word_s[x] in mydict[i]:
mystr = mystr f"{i} "
print(mystr)
but I want when I give this input To the app:
4
man I je ich
kheili very très sehr
alaghemand interested intéressé interessiert
barnamenevisi programming laprogrammation Programmierung
I am very interested in programming
get This output:
man am kheili alaghemand in barnamenevisi
I Tested
else:
mystr = mystr f"{word_s[x]} "
but it is not working and output is: man am very interested in programming I am kheili interested in programming I am very alaghemand in programming I am very interested in barnamenevisi
please help me to get This output
CodePudding user response:
this should do
word = "I am very interested in programming"
for k, v in mydict.items():
for word_s in word.split():
if word_s in v:
word=word.replace(word_s, k)
print(word)
>>> man am kheili alaghemand in barnamenevisi
CodePudding user response:
When you want to translate from e.g. English to Farsi, you use an English to Farsi dictionary, not a Farsi to (English, French, German) dictionary. Since you already know the English word you want to translate, you can look up the Farsi equivalent from the dictionary easily. On the other hand, if you use a Farsi to (English, French, German) dictionary like you do, you'd need to go through every Farsi word in the dictionary, see if the English equivalent matches the word you're looking for, and then use the Farsi word for it.
Since you want to translate from English here, the keys of your dictionary should be English words. Since you want to take the input in multiple languages, you can create a list of the words in each language, and then create a dictionary using the words of the from and to languages you need.
num_words = 4 # int(input("How many words? "))
languages = ["Farsi", "English", "French", "German"]
words = []
print("Please enter words in this order, separated by a single space:")
print(" ".join(languages))
for _ in range(num_words):
words = input("> ").split()
At the end of this, you should have words
looking like so:
[['man', 'I', 'je', 'ich'],
['kheili', 'very', 'très', 'sehr'],
['alaghemand', 'interested', 'intéressé', 'interessiert'],
['barnamenevisi', 'programming', 'laprogrammation', 'Programmierung']]
Each row is the translation for a single word.
Next, we can create our translation dictionary:
print("Language options: ")
for index, lang in enumerate(languages):
print(f"{index}. {lang}")
from_lang = 1 # int(input(f"Input language [0-{len(languages)}]: "))
to_lang = 0 # int(input(f"Output language [0-{len(languages)}]: "))
translation_dict = dict()
for w in words:
from_word = w[from_lang]
to_word = w[to_lang]
translation_dict[from_word] = to_word
# Or, as a comprehension:
translation_dict = {w[from_lang]: w[to_lang] for w in words}
translation_dict
now looks like an English to Farsi dictionary:
{'I': 'man',
'very': 'kheili',
'interested': 'alaghemand',
'programming': 'barnamenevisi'}
Now, we're ready to ask for the sentence. Then, we iterate over each word, find its translation in the dictionary, and add that to our output:
sentence = input("Enter something to translate\n> ")
output_words = [] # Add translated words to an output list because it's more efficient than a string
for original_word in sentence.split():
translated_word = translation_dict.get(original_word, original_word)
# dict.get(A, B) returns the value of the key A, defaulting to B if the key is not found
output_words.append(translated_word)
# Or, as a comprehension
output_words = [translation_dict.get(w, w) for w in sentence.split()]
print("Translation:")
print(" ".join(output_words)) # Join the output list with spaces and print
Which, given the input I am very interested in programming
outputs man am kheili alaghemand in barnamenevisi