A collection of Python chatbots ranging from simple rule-based systems to more sophisticated conversational agents.
This project contains multiple chatbot implementations:
- Simple Chatbot (
simple_chatbot.py) - Basic rule-based chatbot using pattern matching - Enhanced Chatbot (
enhanced_chatbot.py) - Advanced chatbot with memory and conversation context - NLP Chatbot (
nlp_chatbot.py) - Natural language processing chatbot with sentiment analysis
- Python 3.7 or higher
- Terminal or command prompt
- Clone or download this project to your local machine
- Navigate to the project directory:
cd python-chatbot
python3 simple_chatbot.pyFeatures:
- Basic conversation patterns (greetings, questions, etc.)
- Random response selection
- Simple pattern matching
- Exit commands
python3 enhanced_chatbot.pyFeatures:
- Conversation memory and context
- User name recognition and storage
- Session statistics
- Conversation history saving
- Context-aware responses
- Special commands (
stats)
python3 nlp_chatbot.pyFeatures:
- All enhanced chatbot features
- Sentiment analysis with mood indicators
- Emotion detection
- Intent recognition
- Advanced conversation analytics
For the NLP version and additional features, install dependencies:
# Install optional dependencies
pip3 install -r requirements.txt
# For NLTK (if using NLP features)
python3 -c "import nltk; nltk.download('punkt'); nltk.download('vader_lexicon')"π€ ChatBot: Hello! I'm a simple chatbot. Type 'quit' or 'bye' to exit.
==================================================
π€ You: Hello there!
π€ ChatBot: Hi there! What's on your mind?
π€ You: What's your name?
π€ ChatBot: My name is ChatBot. What's yours?
π€ You: How are you?
π€ ChatBot: I'm doing great, thank you for asking!
π€ You: bye
π€ ChatBot: Goodbye! It was nice chatting with you!
π€ Enhanced ChatBot: Hello! I'm an enhanced chatbot with memory.
I can remember our conversation and get to know you better!
Type 'quit', 'bye', or 'stats' for statistics, or just start chatting!
======================================================================
π€ You: Hi, I'm John
π€ Enhanced ChatBot: Nice to meet you, John! I'll remember that.
π€ You: What's the weather like?
π€ Enhanced ChatBot: I don't have access to real weather data, but I hope it's nice where you are! How's the weather on your end?
π€ You: stats
π Session Stats:
Duration: 0:01:23
Messages: 4
Your name: John
π€ You: Do you remember what I said earlier?
π€ Enhanced ChatBot: Yes, I remember we talked about that! You said: 'What's the weather like?'
Edit the responses dictionary in either chatbot file:
self.responses = {
'new_category': [
"Response 1",
"Response 2",
"Response 3"
]
}Add regex patterns to detect new intents:
self.patterns = {
'new_pattern': re.compile(r'\b(keyword1|keyword2)\b', re.IGNORECASE)
}python-chatbot/
βββ simple_chatbot.py # Basic rule-based chatbot
βββ enhanced_chatbot.py # Chatbot with memory and context
βββ nlp_chatbot.py # NLP-powered chatbot with sentiment analysis
βββ run_chatbot.py # Easy launcher for all chatbots
βββ requirements.txt # Python dependencies
βββ README.md # This file
βββ LICENSE # MIT License
βββ conversation_*.json # Saved conversation logs (generated)
| Feature | Simple | Enhanced | NLP |
|---|---|---|---|
| Basic Conversation | β | β | β |
| Pattern Matching | β | β | β |
| Conversation Memory | β | β | β |
| User Recognition | β | β | β |
| Context Awareness | β | β | β |
| Session Statistics | β | β | β |
| Conversation Saving | β | β | β |
| Sentiment Analysis | β | β | β |
| Intent Recognition | β | β | β |
| Entity Extraction | β | β | β |
You can set these environment variables for customization:
export CHATBOT_NAME="YourBotName"
export CHATBOT_SAVE_CONVERSATIONS="true"
export CHATBOT_LOG_LEVEL="INFO"Enhanced chatbot saves conversations as JSON files:
- Location:
conversation_YYYYMMDD_HHMMSS.json - Format: Structured JSON with timestamps and metadata
-
Permission Error: Make sure Python files are executable
chmod +x simple_chatbot.py chmod +x enhanced_chatbot.py
-
Module Not Found: Ensure you're using Python 3.7+
python3 --version
-
Encoding Issues: The chatbots use UTF-8 encoding for emoji support
To understand and extend these chatbots:
- Python Regex: Learn about pattern matching with regular expressions
- JSON Handling: Understanding data serialization for conversation storage
- Object-Oriented Programming: Classes and methods in Python
- Natural Language Processing: For advanced chatbot features
Planned features:
- Web interface using Flask
- Voice input/output capability
- Integration with external APIs
- Machine learning-based responses
- Multi-language support
- Plugin system for extensions
Feel free to fork this project and submit improvements! Some ideas:
- Add new conversation patterns
- Improve response quality
- Add new features
- Fix bugs or optimize performance
This project is open source and available under the MIT License.
Happy Chatting! π
For questions or suggestions, feel free to reach out or create an issue in the repository.