简体   繁体   English

接受用户输入并检查每个单词是否在字典中

[英]Taking user input and check if each word is in a dictionary

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到 go 更上一层楼,我也想把用户输入,然后检查这两个字典,看看它们是否首先匹配短语字典,然后检查单词字典,如果不匹配,然后搜索单个单词字典并提供直译

Example would be - I love you - translates to ek het jou lief例如 - 我爱你 - 翻译为 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但是如果用户输入 just love you - 我希望它与字典匹配并说 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我的代码在下面 - 拆分字会引发 hash 错误,我不知道该怎么做 - 有没有更好的方法或其他方法我应该这样做 - rest 有效

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马特

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). question变量是一个字符串,在使用split方法后,您会得到一个字符串列表(来自原始问题的单词)。 Here's more info about the split method .这是有关拆分方法的更多信息 You might also want to refresh your knowledge on iterating over lists in python .您可能还想更新有关迭代 python 中的列表的知识

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.我发现使用 function 这个实现更简洁一些。 The most important part is in the function's else block.最重要的部分在函数的else块中。 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.如果字典中没有一个单词,它将为整个内容返回 None 。 This can be easily changed by changing the else to something like这可以通过将else更改为类似的东西来轻松更改

outputPhrase += ''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM