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.

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

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