Home › AI + Python: Build a Chai Chatbot › Lesson 5
Understand whole sentences: the in word
AI + Python: Build a Chai Chatbot, lesson 5 of 8. simple English notes and code you can run, free.
Open the interactive lesson with Run buttonWatch on YouTube
The idea
A customer types "masala chai ka menu dikhao". The Part 4 bot replies "Samjha nahi!", because it looks up the whole sentence in its diary, and the diary only has "menu".
A real chai bhaiya just catches the one useful word. Python can do the same with in: is this word inside the sentence?
Go through every key in the diary with for word in replies:. If word in msg, print its reply.
"menu" in "masala chai ka menu dikhao"isTrue.- "menu aur price" has two key words, so the bot gives both replies.
- If no key word matches, the bot stays quiet. Part 10 fixes this.
The code
replies = {"menu": "Masala, Adrak!", "price": "Rs 20"}
while True:
msg = input("Aap: ").lower().strip()
for word in replies:
if word in msg:
print(replies[word])
Output:
Aap: Masala chai ka MENU dikhao Masala, Adrak! Aap: Price kya hai bhaiya? Rs 20 Aap: menu aur price Masala, Adrak! Rs 20
Try it yourself
- Press Run: the Input box already has the three sentences. Then try "bhaiya menu please".
- Add "timing": "Subah 7 - raat 10" and ask "shop ki timing kya hai?"
- Type "kaise ho?". Why is the bot quiet?
Remember
Small word on the left, big sentence on the right. Next: Part 6, a new reply every time.
← "MENU" or "menu"? .lower() + .strip() · A new reply every time: random.choice →
All lessons: AI + Python: Build a Chai Chatbot