新しいpythonとプログラミングの世界です。ポイントに達する。私はこのコードを実行し、入力を入れて鶏を言うと、それは2つの脚の動物として返信されます。しかし、私は間違いなく宇宙猿のような間にスペースを持っている2つの単語のもののための返事を得る(それは私の辞書に表示されますが)どのように私はそれを解決するのですか?辞書の2ワードのキーを検索する
私の辞書:example.py
dictionary2 = {
"chicken":"chicken two leg animal",
"fish":"fish is animal that live under water",
"cow":"cow is big vegetarian animal",
"space monkey":"monkey live in space",
私のコード:test.py
from example import *
print "how can i help you?"
print
user_input = raw_input()
print
print "You asked: " + user_input + "."
response = "I will get back to you. "
input_ls = user_input.split(" ")
processor = {
"dictionary2":False,
"dictionary_lookup":[]
}
for w in input_ls:
if w in dictionary2:
processor["dictionary2"] = True
processor["dictionary_lookup"].append(w)
if processor["dictionary2"] is True:
dictionary_lookup = processor["dictionary_lookup"][0]
translation = dictionary2[dictionary_lookup]
response = "what you were looking for is: " + translation
print
print "Response: " + response
なぜユーザー入力を分割していますか?入力全体を辞書のキーとして使用するだけです。 – Barmar
通常、ユーザの入力は "what is chicken"のようになりますので、私のコードは辞書のチキンを検出し、それに基づいて答えを返します。 – saitama
"what is"のような雑音を取り除く方法を知っているパーサーを設計する必要があります。 – Barmar