-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
81 lines (69 loc) · 2.68 KB
/
chatbot.py
File metadata and controls
81 lines (69 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import random
import re
class SimpleChatbot:
def __init__(self, intents_path):
"""
Initializes the chatbot by loading the intents file.
"""
self.intents = self._load_intents(intents_path)
def _load_intents(self, filepath):
"""
Loads the intents from a JSON file.
"""
try:
with open(filepath, 'r') as file:
return json.load(file)
except FileNotFoundError:
print(f"Error: The file {filepath} was not found.")
return None
except json.JSONDecodeError:
print(f"Error: The file {filepath} is not a valid JSON.")
return None
def get_response(self, user_message):
"""
Finds a matching intent for the user's message and returns a response.
"""
if not self.intents:
return "I'm sorry, I am not configured correctly."
# Preprocess the user's message
user_message = user_message.lower().strip()
# Find the best matching intent
best_match_score = 0
best_intent = None
for intent in self.intents['intents']:
for pattern in intent['patterns']:
# Using simple regex for whole word matching
if re.search(r'\b' + re.escape(pattern.lower()) + r'\b', user_message):
# For this simple bot, the first direct match is good enough.
# A more complex bot might score based on match quality.
score = 1 # Simple match score
if score > best_match_score:
best_match_score = score
best_intent = intent
break # Move to next intent once a pattern matches
if best_intent and best_intent == intent:
break # Exit outer loop if a good match is found
if best_intent:
return random.choice(best_intent['responses'])
else:
return "I'm not sure how to respond to that. Can you ask me something else?"
def main():
"""
Main function to run the chatbot in the console.
"""
# Create an instance of the chatbot
chatbot = SimpleChatbot('intents.json')
print("Chatbot is ready! Type 'quit' to exit.")
print("You: ", end="")
# Interaction loop
while True:
user_input = input()
if user_input.lower() == 'quit':
print("Bot: Goodbye!")
break
response = chatbot.get_response(user_input)
print(f"Bot: {response}")
print("You: ", end="")
if __name__ == "__main__":
main()