My pre-programmed translator works almost perfectly, with one minor but rather ugly issue. The translation text prints like this.
[joey@manjarobox Languages]$ python universal.dict
Enter text to translate:
he should have given up .
eh dloulhs evah nevig pu. [joey@manjarobox Languages]$
That last block has no typos. It's how I pasted it.
eng_to_lang = {
".":"\b.",
"?":"\b?",
"!":"\b!",
",":"\b,",
"he":"eh", "He":"Eh",
"should":"dloulhs", "Should":"Dlouhs",
"have":"evah", "Have":"Evah",
"given":"nevig", "Given":"Nevig",
"up":"pu", "Up":"Pu",
}
# UNLESS YOU KNOW WHAT YOU'RE DOING,
# DO NOT TOUCH ANYTHING FROM THIS POINT ON!
value = input("Enter text to translate: \n")
# string = value.lower()
string = value.split()
for element in range(0, len(string)):
print(eng_to_lang.get(string[element]), end=" ")
I would also like for my code to recognize punctuation and treat it accordingly, that way I don't have to correct everything once I am able to echo translated text in a file. When I tried it, "he," is treated like a single word.
Anyone got a solution?
My pre-programmed translator works almost perfectly, with one minor but rather ugly issue. The translation text prints like this.
[joey@manjarobox Languages]$ python universal.dict
Enter text to translate:
he should have given up .
eh dloulhs evah nevig pu. [joey@manjarobox Languages]$
That last block has no typos. It's how I pasted it.
eng_to_lang = {
".":"\b.",
"?":"\b?",
"!":"\b!",
",":"\b,",
"he":"eh", "He":"Eh",
"should":"dloulhs", "Should":"Dlouhs",
"have":"evah", "Have":"Evah",
"given":"nevig", "Given":"Nevig",
"up":"pu", "Up":"Pu",
}
# UNLESS YOU KNOW WHAT YOU'RE DOING,
# DO NOT TOUCH ANYTHING FROM THIS POINT ON!
value = input("Enter text to translate: \n")
# string = value.lower()
string = value.split()
for element in range(0, len(string)):
print(eng_to_lang.get(string[element]), end=" ")
I would also like for my code to recognize punctuation and treat it accordingly, that way I don't have to correct everything once I am able to echo translated text in a file. When I tried it, "he," is treated like a single word.
Anyone got a solution?
The behaviour is caused by the use of end=" "
in the print
statement.
Normally, a newline is printed at the end of a print
statement, but this parameter is telling it to simply print a space instead of a newline. Thus, when the program ends, the command line prints on the same line.
You can 'fix' it by simply adding this at the end of your program:
print()
That will force a newline and will start the command line on the next line.
Instead of printing each word within a loop, an alternate approach would be to construct a string and then print the string at the end:
result = ''
for element in string:
result += eng_to_lang[element] + ' '
print(result)
This approach stores each word in a list, then joins it together in the end:
word_list = []
for element in string:
word_list.append(eng_to_lang[element])
print(" ".join(word_list))
or even simply:
word_list = [eng_to_lang[word] for word in value.split()]
print(" ".join(word_list))