July 20, 2025

Simple chatbot in python

 The most basic implementation of a chatbot works with predefined questio answer pairs stored in a Python dictionary. The human user has to enter exactly the predefined question to get an answer so the chatbot is a database lookup tool. Even if the program is less advanced than current large language models and its less mature than the Eliza software, its a good starting point to become familiar with chatbot development from scratch. The sourcecode consists of less than 50 lines of code in Python including the dataset.

import re

def run_chatbot():
    knowledge_base = {
        "hello": "Hi there! How can I help you today?",
        "how are you": "I'm a computer program, so I don't have feelings, but thanks for asking!",
        "what is your name": "I am a simple chatbot.",
        "who created you": "I was created by a programmer.",
        "what can you do": "I can answer questions based on my internal knowledge base.",
        "tell me a joke": "Why don't scientists trust atoms? Because they make up everything!",
        "what is the capital of france": "The capital of France is Paris.",
        "what is the largest ocean": "The Pacific Ocean is the largest ocean.",
        "what is the highest mountain": "Mount Everest is the highest mountain in the world.",
        "what is the square root of 9": "The square root of 9 is 3.",
        "what is the weather like today": "I'm sorry, I cannot provide real-time weather information.",
        "how old are you": "I don't have an age in the human sense.",
        "what is python": "Python is a high-level, interpreted programming language.",
        "what is AI": "AI stands for Artificial Intelligence, which is the simulation of human intelligence processes by machines.",
        "where are you from": "I exist in the digital realm!",
        "can you learn": "I don't learn in the same way humans do. My responses are pre-programmed.",
        "what is gravity": "Gravity is a fundamental force of nature that attracts any two objects with mass.",
        "what is photosynthesis": "Photosynthesis is the process used by plants, algae, and cyanobacteria to convert light energy into chemical energy.",
        "what is the speed of light": "The speed of light in a vacuum is approximately 299,792,458 meters per second.",
        "thank you": "You're welcome! Is there anything else I can assist you with?"
    }
    print("Welcome to the simple Q&A Chatbot!")
    print("Type 'quit' or 'exit' to end the conversation.")
    print("-" * 40)

    while True:
        user_input = input("You: ").strip().lower()

        if user_input in ["quit", "exit"]:
            print("Chatbot: Goodbye! Have a great day.")
            break

        found_answer = False
        for question, answer in knowledge_base.items():
            if question in user_input:
                print(f"Chatbot: {answer}")
                found_answer = True
                break
        if not found_answer:
            print("Chatbot: I'm sorry, I don't understand that question. Can you please rephrase it?")

if __name__ == "__main__":
    run_chatbot()

No comments:

Post a Comment