Computer Science Canada Translating an english sentence to spanish using a given database |
Author: | G-nerd [ Wed Nov 24, 2010 6:46 am ] | ||||
Post subject: | Translating an english sentence to spanish using a given database | ||||
Hi I need help trying to translate an English sentence to Spanish using a database given and also making the user continue to ask an English sentence until asked to be terminated using a given character. Here is my code
When I run the code on python it first asks me to type in a sentence, which I do, but using strings so that in can be converted to a list. However, once the english sentence has been implemented, nothing comes out. Can someone show me what the error is. My friend has a different code but I can't understand it. He says it works but not properly. I don't understand how he came up with the x.split().
when he types : " the fat man fat" it just produces el gordo hombre it should say el gordo hombre gordo Please any help would be gladly appreciated. |
Author: | Tony [ Thu Nov 25, 2010 12:59 am ] |
Post subject: | RE:Translating an english sentence to spanish using a given database |
> Can someone show me what the error is. If the Python interpreter does not give you warnings/errors, then there isn't one and the code is doing exactly what you told it to do. That might be different from what you thought about though ![]() > I don't understand how he came up with the x.split(). x is an argument which receives a string object. String objects have a number of methods defined, including #split() http://docs.python.org/library/stdtypes.html#str.split |
Author: | kctan [ Tue Nov 30, 2010 12:39 am ] | ||
Post subject: | Re: Translating an english sentence to spanish using a given database | ||
Make some small change to your friend's code. If you need more explanation, let me know.
|
Author: | wtd [ Tue Nov 30, 2010 1:44 am ] | ||||||||
Post subject: | Re: Translating an english sentence to spanish using a given database | ||||||||
First off, let's convert your db to a dictionary.
Now, let's get a list of the words in an input sentence:
Now, let's get a new list of translated words.
Now, let's use a generator expression passed to join to get the new sentence.
Isn't that much nicer than this explicit looping and list of lists malarky? |
Author: | kctan [ Tue Nov 30, 2010 3:32 am ] |
Post subject: | Re: Translating an english sentence to spanish using a given database |
wtd @ Tue Nov 30, 2010 2:44 pm wrote: Isn't that much nicer than this explicit looping and list of lists malarky? I think it depends. For someone who is not familiar with list comprehension, your solution may not be easy to understand. |
Author: | wtd [ Tue Nov 30, 2010 11:00 am ] |
Post subject: | RE:Translating an english sentence to spanish using a given database |
Then that person would be well-advised to learn about list comprehensions and generators. Python is not Tring with different syntax, after all. |