-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
111 lines (73 loc) · 3.38 KB
/
Copy pathchatbot.py
File metadata and controls
111 lines (73 loc) · 3.38 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import json
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
class FAQChatbot:
def __init__(self, file_name):
with open(file_name, "r", encoding="utf-8") as file:
self.data = json.load(file)
self.questions = []
for item in self.data:
self.questions.append(item["question"])
self.vectorizer = TfidfVectorizer()
self.vectors = self.vectorizer.fit_transform(self.questions)
self.replies = {
"hi": "Hello! How can I help you today?",
"hello": "Hi! Ask me anything about StudyBuddy.",
"hey": "Hey! What would you like to know?",
"good morning": "Good Morning! 😊",
"good afternoon": "Good Afternoon! 😊",
"good evening": "Good Evening! 😊",
"how are you": "I'm doing great. Thanks for asking!",
"who are you": "I'm StudyBuddy FAQ Chatbot.",
"who made you": "I was developed by Harsh Garg during the CodeAlpha AI Internship.",
"what can you do": "I can answer questions related to StudyBuddy.",
"thanks": "You're welcome! 😊",
"thank you": "Happy to help!",
"bye": "Goodbye! Have a great day!",
"goodbye": "See you soon!"
}
def get_response(self, question):
question = question.lower().strip()
keywords = [
"studybuddy", "study", "task", "planner", "progress",
"python", "java", "c++", " c ", "html", "css",
"javascript", "flutter", "git", "github",
"ai", "artificial intelligence", "machine learning",
"deep learning", "nlp", "streamlit",
"algorithm", "algorithms", "dsa",
"data structure", "coding", "programming",
"interview", "exam", "sql", "database",
"password", "profile", "reminder"
]
if not any(word in f" {question} " for word in keywords):
return (
"Sorry, I couldn't understand that question. 😊\n\n"
"I can answer questions related to:\n"
"• StudyBuddy\n"
"• Programming\n"
"• Artificial Intelligence\n"
"• Machine Learning\n\n"
"Example questions:\n"
"• What is Python?\n"
"• What is Java?\n"
"• What is StudyBuddy?\n"
"• What is Machine Learning?"
)
for word, reply in self.replies.items():
if word in question:
return reply
user_vector = self.vectorizer.transform([question])
score = cosine_similarity(user_vector, self.vectors)
index = score.argmax()
confidence = score[0][index]
print("Best Match:", self.data[index]["question"])
print("Confidence:", confidence)
if confidence < 0.50:
text = "Sorry, I don't know the answer to that.\n\n"
text += "You can ask questions like:\n\n"
for item in self.data[:5]:
text += f"• {item['question']}\n"
return text
return self.data[index]["answer"]
def get_questions(self):
return self.questions