-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
54 lines (44 loc) · 1.32 KB
/
Copy pathchatbot.py
File metadata and controls
54 lines (44 loc) · 1.32 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
import json
import random
import string
# Load intents from JSON file
with open("intents.json") as file:
data = json.load(file)
# Text preprocessing function
def preprocess(text):
text = text.lower()
text = text.translate(str.maketrans("", "", string.punctuation))
tokens = text.split()
return tokens
# Find best matching intent
def get_response(user_input):
tokens = preprocess(user_input)
best_intent = None
max_score = 0
# Loop through intents
for intent in data["intents"]:
for pattern in intent["patterns"]:
pattern_tokens = preprocess(pattern)
score = 0
# Count matching words
for word in tokens:
if word in pattern_tokens:
score += 1
# Select intent with highest match score
if score > max_score:
max_score = score
best_intent = intent
# Return response
if best_intent:
return random.choice(best_intent["responses"])
else:
return "Sorry, I didn't understand that."
# Chat loop
print("Chatbot is running! Type 'exit' to stop.")
while True:
user_input = input("You: ")
if user_input.lower() == "exit":
print("Bot: Goodbye!")
break
response = get_response(user_input)
print("Bot:", response)