Skip to content

Commit 9975ff2

Browse files
committed
added chatbot
1 parent 08b7bdd commit 9975ff2

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

.github/ChatBot/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Rule Based Chatbot using NLTK
2+
3+
This is a simple rule based chatbot implemented using the Natural Language Toolkit (NLTK) library in Python. The chatbot engages in conversations with users by matching their input with predefined patterns and providing appropriate responses.
4+
5+
## How it Works
6+
7+
The chatbot is built using NLTK's `Chat` class, which allows us to define patterns and corresponding responses. When the user inputs a message, the chatbot searches for a pattern that matches the input and responds with the associated response. If there's no direct match, the chatbot provides a default response to handle any unrecognized inputs.
8+
9+
The predefined patterns cover a range of greetings, questions about the chatbot, jokes, recommendations, and handling user intentions. The chatbot is designed to respond politely and engagingly to various types of user queries.
10+
11+
## Usage
12+
13+
1. Run the `chatbot.py` script, and the chatbot will greet you with a welcome message.
14+
2. Enter your message or query, and the chatbot will respond accordingly.
15+
3. To stop the conversation, simply type `quit` as your input, and the chatbot will bid you goodbye.
16+
17+
## Improving the Chatbot
18+
19+
The predefined patterns can be expanded to make the chatbot more engaging and diverse. In the `chat_patterns.py` file, you can add more patterns and responses to handle a broader range of queries. By doing so, you can reduce the likelihood of default responses and create a more dynamic and interactive chatbot.
20+
21+
## Dependencies
22+
23+
To run the chatbot, ensure you have the following installed:
24+
25+
- Python 3.x
26+
- NLTK library
27+
28+
You can install the required dependencies using pip:
29+
30+
```bash
31+
pip install nltk

.github/ChatBot/chat_patterns.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Predefined patterns and responses for the chatbot
2+
3+
patterns = [
4+
# Greetings
5+
(r'hi|hello|hey', ['Hello!', 'Hi!', 'Hey!', 'Hi there!']),
6+
(r'how are you?', ['I am good, thank you!', 'I am doing well!', 'All good!', 'I\'m fine, thanks!']),
7+
(r'what is your name?', ['You can call me Chatbot.', 'I am Chatbot!', 'My name is Chatbot.']),
8+
(r'bye|goodbye', ['Goodbye!', 'See you later!', 'Bye!', 'Have a great day!']),
9+
10+
# Jokes
11+
(r'tell me a joke', ['Why don’t scientists trust atoms? Because they make up everything!',
12+
'Parallel lines have so much in common. It’s a shame they’ll never meet.',
13+
'Why did the scarecrow win an award? Because he was outstanding in his field!']),
14+
15+
# Age
16+
(r'how old are you?', ['I am a computer program, so I don\'t have an age!',
17+
'Age is just a number, and I don\'t have one!']),
18+
19+
# Creator
20+
(r'who created you?', ['I was created by OpenAI.', 'My creators are from OpenAI.']),
21+
22+
# Compliments
23+
(r'(.*) (like|love) you', ["Aw, that's so sweet!", "Thank you! I really appreciate it."]),
24+
(r'you are (.*)(good|awesome|amazing)', ["Thank you! I'm here to assist you.", "I'm glad you think so!"]),
25+
26+
# Weather
27+
(r'(.*) (weather|temperature) today', ["I'm sorry, I am just a chatbot and don't have access to real-time data.",
28+
"You can check the weather online or through a weather app."]),
29+
30+
# Recommendations
31+
(r'(.*) (movie|book) (recommendation|recommend)', ["I recommend you watch 'The Shawshank Redemption' or read 'To Kill a Mockingbird'.",
32+
"You might enjoy 'Inception' or 'The Great Gatsby'.",
33+
"If you like action, 'The Dark Knight' is a great choice."]),
34+
35+
# How to create/build something
36+
(r'how (can|do) (I|you) (create|build) (a|an) (.*)', ["To create {4}, you can follow these steps...",
37+
"Building {4} requires some technical knowledge, but here are the basics...",
38+
"Sure! Here's a basic guide on building {4}..."]),
39+
40+
# User intentions
41+
(r'I (want|need) (.*)', ["Why do you need {1}?", "What would you do with {1}?"]),
42+
(r'I am (feeling|looking) (.*)', ["Why are you feeling {1}?", "Tell me more about why you are {1}."]),
43+
44+
# More patterns and responses to handle different queries
45+
# Add more patterns and responses here to make the chatbot more engaging and diverse
46+
47+
# Default response
48+
(r'.*', ["I'm sorry, I don't quite understand. Could you please rephrase that?",
49+
"I'm still learning, and I'm not sure how to respond to that.",
50+
"Let's talk about something else. What else would you like to know?"]),
51+
]

.github/ChatBot/chatbot.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import nltk
2+
from nltk.chat.util import Chat, reflections
3+
from chat_patterns import patterns # Import patterns from the chat_patterns.py file
4+
5+
chatbot = Chat(patterns, reflections)
6+
7+
8+
def start_chat():
9+
print("Chatbot: Hi! How can I help you today?")
10+
while True:
11+
user_input = input("You: ")
12+
if user_input.lower() == 'quit':
13+
print("Chatbot: Goodbye!")
14+
break
15+
response = chatbot.respond(user_input)
16+
print("Chatbot:", response)
17+
18+
if __name__ == "__main__":
19+
nltk.download('punkt')
20+
start_chat()

0 commit comments

Comments
 (0)