Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
📘 Lesson  ·  Lesson 61

Simple Chatbot

हम क्या बना रहे हैं

Chatbot user के messages पढ़कर और जवाब देकर बात करता है। सबसे आसान तरह rule-based chatbot है: यह keywords ढूँढता है और मेल खाता जवाब देता है। यह सीखता नहीं, पर loops, conditions और string handling सिखाता है — और बनाने में सच में मज़ेदार है।

💡 Rule-based = message में keywords जाँचो और एक fixed जवाब दो।

Chat loop

while True:
    msg = input("You: ").lower()
    if msg == "bye":
        print("Bot: Goodbye!")
        break
    print("Bot: I heard you say", msg)
You: hi
Bot: I heard you say hi
You: bye
Bot: Goodbye!

while True loop बातचीत चलाता रहता है। हम input lowercase करते हैं ताकि matching भरोसेमंद हो, और user "bye" type करे तो break chat ख़त्म कर देता है।

Keyword rules

while True:
    msg = input("You: ").lower()
    if "hello" in msg or "hi" in msg:
        print("Bot: Hello there!")
    elif "name" in msg:
        print("Bot: I am CodeBot.")
    elif "bye" in msg:
        print("Bot: Bye!")
        break
    else:
        print("Bot: Sorry, I did not understand.")
You: what is your name
Bot: I am CodeBot.

हम in से जाँचते हैं कि "hello" या "name" जैसे keywords message में आते हैं या नहीं, और उसी अनुसार जवाब देते हैं। else उसे संभालता है जो हम नहीं पहचानते।

Replies की dictionary

जैसे rules बढ़ते हैं, dictionary उन्हें साफ़ रखती है।

replies = {
    "hello": "Hi there!",
    "name": "I am CodeBot.",
    "help": "Ask me about Python!"
}

msg = input("You: ").lower()
answer = "Sorry, I did not understand."
for keyword in replies:
    if keyword in msg:
        answer = replies[keyword]
        break
print("Bot:", answer)
You: can you help
Bot: Ask me about Python!

Dictionary हर keyword को एक reply से map करती है। हम keywords पर loop करते हैं, और अगर एक message में आए, उसका reply इस्तेमाल करते हैं। नया rule जोड़ना बस एक और dictionary entry है।

इसे smarter बनाना

  • और keywords। आम सवालों और greetings के लिए entries जोड़िए।
  • कई matches। ऐसे messages संभालिए जिनमें कई keywords हों।
  • Random replies। प्रति keyword जवाबों की list रखिए और एक random चुनिए।
  • आगे जाइए। बाद में असली समझ को NLP libraries या AI model इस्तेमाल कर सकते हैं।

आम गलतियाँ

  • Input lowercase न करना। "Hello" "hello" से match नहीं होगा — .lower() इस्तेमाल कीजिए।
  • in के बजाय ==। in जाँचता है keyword message में कहीं आता है या नहीं।
  • कोई exit condition नहीं। "bye" break बिना, loop हमेशा चलता है।
  • कोई else fallback नहीं। जो न पहचानें उन messages को हमेशा संभालिए।

सारांश

  • Rule-based chatbot message में keywords पर जवाब देता है।
  • while True loop बातचीत चलाता रहता है।
  • Input lowercase कीजिए और keywords match करने को in इस्तेमाल कीजिए।
  • keyword-से-reply की dictionary कई rules साफ़ रखती है।
  • हमेशा else fallback और exit का तरीका जोड़िए।

आगे, logic मज़बूत कीजिए Python loops से, या replies व्यवस्थित कीजिए dictionaries से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Rule-based chatbot क्या है?
Rule-based chatbot fixed नियमों के आधार पर जवाब देता है — यह user के message में keywords ढूँढता है और मेल खाता जवाब देता है। यह सीखता नहीं; आपके लिखे नियमों का पालन करता है। यह बनाने में सबसे आसान chatbot है।
सरल chatbot input कैसे समझता है?
यह सच में नहीं समझता — यह जाँचता है कि message में कुछ keywords आते हैं या नहीं। जैसे, अगर message में "hello" हो, तो greeting से जवाब देता है। पहले input lowercase करना matching भरोसेमंद बनाता है।
Chatbot को चलता कैसे रखूँ?
while True loop इस्तेमाल कीजिए जो input पढ़ता और जवाब देता रहे, जब तक user "bye" जैसा कुछ type करके exit न करे। Loop ही इसे बातचीत जैसा महसूस कराता है।
User input lowercase क्यों करूँ?
Users mixed case में type करते हैं ("Hello", "HELLO", "hello")। .lower() से lowercase में बदलना मतलब आपकी keyword जाँच चलती है चाहे वे कैसे भी type करें।
अपना chatbot smarter कैसे बनाऊँ?
और keyword rules जोड़िए, keywords को replies से map करने को dictionary इस्तेमाल कीजिए, प्रति message कई keywords संभालिए, या library या AI model पर जाइए। Rule-based वो बुनियाद है जिस पर आप बनाते हैं।
← Back to Python Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicEmail Spam Detection आपकी प्रगति: 61 / 66 (92%) · Lesson 62 / 66 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें