Python AI Chatbot for Beginners Source Code

Learn to build an AI chatbot in Python using NLTK! Easy steps, kid-style examples, jokes & memes. Perfect for beginners.

Ever wanted your own AI buddy that replies to “hi”? Meet ChatPy! We’ll build this simple chatbot in Python using NLTK—explained in a kid-friendly way (plus jokes, real-life examples, and memes!). 🐍

Why use NLTK Chatbot?

Python AI Chatbot for Beginners Source Code

NLTK (Natural Language Toolkit) is like your language toolbox. The chat.util module helps us build a fun chatbot with minimal code. Think of it as Lego: you snap together pieces (conversation patterns) to build something cool—like ChatPy!


Step 1: Setup NLTK (Keyword: nltk.chat util)

import nltk  
from nltk.chat.util import Chat, reflections  
nltk.download('punkt')  # only the first time

Real-life example: It’s like downloading new stickers for your sticker album—done once, then reuse forever!

Step 2: Conversation Pairs

This is the heart of ChatPy—when you say “hi,” ChatPy responds!

pairs = [
  [r"hi|hello|hey", ["Hello there!", "Hey!", "Hi!"]],
  ...
]

Analogy: Imagine teaching your plush toy responses to your commands—if you say “banana,” it says “yum!”

Step 3: Reflections!

reflections handles swaps like "I am" ➝ "you are". It’s ghostwriting magic under the hood!

Step 4: Build & Converse

chatbot = Chat(pairs, reflections)
print("Hi! I'm your AI chatbot. Type 'bye' to exit.")
chatbot.converse()

Real-life example: This is like opening a lemonade stand sign: “Come talk to me!” And ChatPy listens/responds.


Meme Break!

When you type "how are you?"  
ChatPy: "Feeling fantastic today!"  
You: *mic drop* 🎤

(Insert meme of an excited kid giving a thumbs-up.)


Step 5: Handling Unknown Questions

[  
  [r"(.*)", ["I'm not sure about that.", "Could you rephrase?"]]  
]

Analogy: When your pet tilts their head because they didn't understand you—cute and helpful!


Full Code

# ----- Simple AI Chatbot in Python -----
# Author: Azeem Teli (PyZilla)
# Lines: ~100
# Requirements: pip install nltk

import nltk
from nltk.chat.util import Chat, reflections

# Download NLTK data (only first time)
nltk.download('punkt')

# ----- Chatbot Conversation Pairs -----
pairs = [
    [
        r"hi|hello|hey",
        ["Hello there!", "Hey! How can I help you today?", "Hi!"]
    ],
    [
        r"how are you ?",
        ["I'm doing great! Thanks for asking.", "Feeling fantastic today!"]
    ],
    [
        r"what is your name ?",
        ["I'm ChatPy, your Python chatbot.", "I go by ChatPy — nice to meet you!"]
    ],
    [
        r"who created you ?",
        ["I was created by Azeem Teli for PyZilla!", "Azeem Teli is my creator."]
    ],
    [
        r"what can you do ?",
        ["I can chat with you, answer simple questions, and keep you company.", 
         "Main bas baat kar sakta hoon (I can only talk for now)."]
    ],
    [
        r"python",
        ["Python is my favorite language! It's powerful yet simple."]
    ],
    [
        r"bye|goodbye",
        ["Goodbye! Have a great day!", "See you later!"]
    ],
    [
        r"(.*)",
        ["I'm not sure about that. Could you rephrase?", 
         "Interesting... tell me more!", 
         "Hmm, I’ll have to Google that."]
    ]
]

# ----- Create Chatbot Instance -----
chatbot = Chat(pairs, reflections)

# ----- Start Chat -----
print("Hi! I'm your AI chatbot. Type 'bye' to exit.")
chatbot.converse()

Now you can copy-paste it and try chatting with ChatPy!


Why This Matters

  • Beginner-friendly: Perfect for someone who just learned loops and conditions.
  • Creates confidence: You built something real with ~100 lines of code!
  • Fun and shareable: Show classmates your chatbot friend.

Add-On Learning Resources

Fun Real-Life Example

Imagine teaching ChatPy during your road trip:

  • You: “hi”
  • ChatPy: “Hey! How can I help you today?”
  • You: “play music”
  • ChatPy: “I’m not sure about that. Could you rephrase?” It’s like a digital friend riding shotgun—just a little clueless, but trying!

Tips

  1. Change responses: Edit the pairs list—want ChatPy to say “Cool beans!” when you say “awesome”? Just add it.
  2. Add math helper: Add a pattern r"what is (\d+) \+ (\d+)" and compute responses with int(...) + int(...). Boom—makes ChatPy a mini math tutor!
  3. Try reflections swap: Say “I am happy”—ChatPy can reply “you are happy”? ChatPy learns to mirror feelings!

Final Thoughts

That’s it! You’ve made a Python AI chatbot with NLTK. It’s like building a magic talking toy. Encourage students to tweak responses, add jokes, or turn ChatPy into a quiz-bot about animals. The possibilities are endless!

Got Questions?

  • Ask me how to add emojis!
  • Want ChatPy to tell riddles?
  • Or—just share your most hilarious ChatPy response 😄

Happy coding!

Post a Comment