I am building a language translator for an assignment.
There are two dictionaries, one for words and one for phrases
It takes a word in English and provides the equivalent word in Aafrikaans It can also take a small phrase in English and provide the equivalent phrase in Aafrikaans
To go to the next level, I also want to take the users input and then check against these two dictionaries to see if they first match the phrase dictionary, second check the word dictionary, and if none match, then search for the individual words in the word dictionary and provide a literal translation
Example would be - I love you - translates to ek het jou lief
But if the user typed in just love you - I would want it to match the word dictionary and say liefde jy
I thought the way to do it would be to take the input and split the input into a list and then take that list and search for each word sequentially in each dictionary - but for the life of me i cant see how i can do that in a loop. My code is below - the splitword throws a hash error and i cant see how i would do this - is there a better way or some other way i should do this - the rest works
i2w = {'hello': 'hallo', 'love': 'liefde', 'you': 'jy', 'I': 'ek', 'are': 'is', 'a': 'a', 'man': 'man', 'woman': 'vrou', 'how': 'hoe', 'the': 'die', 'best': 'beste', 'smell': 'reuk', 'nice': 'lekker'}
i2p = {'i love you': 'ek het jou lief', 'you are welcome': 'jy is welkom', 'life is good': 'die lewe is goed'}
question = input("Please enter the word or phrase you would like translated from Engish to Aafrikaans?: ").lower()
splitquestion = question.split ()
while question != "finished":
if question in i2p:
print("The Aafrikaans translation is ",i2p.get(question))
break
elif question in i2w:
print("The Aafrikaans translation is ",i2w.get(question))
break
elif splitquestion in i2w:
print("The Aafrikaans translation is ",i2w.get(splitquestion))
break
else:
question = input("Sorry this specific word or phrase is not in the dictionary, be mindful of word order or grammar, Please enter what word or phrase you would like translated from Engish to Aafrikaans?: ").lower()
print ("\nThanks for using the translator tool!")
Thanks
Matt
CodePudding user response:
Taking user input and check if each word is in a dictionary can be done as follows:
question = input()
splitquestion = question.split()
for word in splitquestion:
if word in iw2:
# do something
The question
variable is a string and after using the split
method you get a list of strings (the words from the original question). Here's more info about the split method.
You might also want to refresh your knowledge on iterating over lists in python.
CodePudding user response:
words = {'hello': 'hallo', 'love': 'liefde', 'you': 'jy', 'I': 'ek', 'are': 'is', 'a': 'a', 'man': 'man', 'woman': 'vrou', 'how': 'hoe', 'the': 'die', 'best': 'beste', 'smell': 'reuk', 'nice': 'lekker'}
phrases = {'i love you': 'ek het jou lief', 'you are welcome': 'jy is welkom', 'life is good': 'die lewe is goed'}
inputPhrase = input('Please enter the word or phrase you would like translated from Engish to Aafrikaans?: ').lower()
# Take a string, input, return the phrase or None if it doesn't exist.
def translate(input):
outputPhrase = ''
if inputPhrase in phrases:
outputPhrase = phrases.get(inputPhrase)
return outputPhrase
else:
splitInput = inputPhrase.split()
for word in splitInput:
if word in words:
outputPhrase = f' {words.get(word)}'
# This would return None if not all words are in the dictionary.
else:
return None
translated = translate(inputPhrase)
while not translated:
question = input('Sorry this specific word or phrase is not in the dictionary, be mindful of word order or grammar, '
'Please enter what word or phrase you would like translated from Engish to Aafrikaans?: ').lower()
print(f'The Aafrikaans translation is {translated}.')
print ('\nThanks for using the translator tool!')
I find this implementation a little cleaner with the use of a function. The most important part is in the function's else
block. It will split the phrase at whitespace, then iterate through every word. If one word isn't in the dictionary, it will return None for the whole thing. This can be easily changed by changing the else
to something like
outputPhrase = ''