0% found this document useful (0 votes)
10 views2 pages

Chat Bot

The document outlines the implementation of a school chatbot using Python, which utilizes NLTK for text processing and sklearn for vectorization and similarity measurement. It includes a graphical user interface (GUI) built with Tkinter, allowing users to input questions and receive answers based on a predefined FAQ dataset. The chatbot processes user input, matches it to the closest FAQ question, and returns the corresponding answer or an error message if no match is found.

Uploaded by

Arjit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Chat Bot

The document outlines the implementation of a school chatbot using Python, which utilizes NLTK for text processing and sklearn for vectorization and similarity measurement. It includes a graphical user interface (GUI) built with Tkinter, allowing users to input questions and receive answers based on a predefined FAQ dataset. The chatbot processes user input, matches it to the closest FAQ question, and returns the corresponding answer or an error message if no match is found.

Uploaded by

Arjit
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SCHOOL CHATBOT

import json
import string
import tkinter as tk
from tkinter import scrolledtext
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
import nltk
from nltk.stem.porter import PorterStemmer
from nltk.tokenize import word_tokenize

# ------------------ NLTK Downloads ------------------


nltk.download('punkt')
nltk.download('punkt_tab')

# ------------------ Stemmer Setup ------------------


stemmer = PorterStemmer()

# ------------------ Text Cleaning Function ------------------


def clean_text(text):
tokens = word_tokenize(text)
return [stemmer.stem(w.lower()) for w in tokens if w not in string.punctuation]

# ------------------ Load FAQ Data ------------------


with open('C:\\Users\\Dell\\Desktop\\faq_data.json', 'r') as f:
faq_data = json.load(f)

questions = [item['question'] for item in faq_data]


answers = [item['answer'] for item in faq_data]

# ------------------ Preprocess and Vectorize Questions ------------------


cleaned_questions = [' '.join(clean_text(q)) for q in questions]
vectorizer = TfidfVectorizer()
question_vectors = vectorizer.fit_transform(cleaned_questions)

# ------------------ Chatbot Response Function ------------------


def get_response(user_input):
cleaned_input = ' '.join(clean_text(user_input))
user_vector = vectorizer.transform([cleaned_input])
similarities = cosine_similarity(user_vector, question_vectors)
best_match_idx = similarities.argmax()
best_score = similarities[0, best_match_idx]

print(f"DEBUG: User input cleaned: {cleaned_input}")


print(f"DEBUG: Best score: {best_score}")
print(f"DEBUG: Best match question: {questions[best_match_idx]}")

if best_score > 0.1: # Lowered threshold for better matching


return answers[best_match_idx]
else:
return "Sorry, I don't understand your question."
# ------------------ GUI using Tkinter ------------------
def send_message():
user_input = entry.get()
if user_input.strip() == "":
return
chat_area.insert(tk.END, "You: " + user_input + "\n")
response = get_response(user_input)
chat_area.insert(tk.END, "Bot: " + response + "\n\n")
entry.delete(0, tk.END)

# Create GUI window


root = tk.Tk()
root.title("School Chatbot")

# Chat area
chat_area = scrolledtext.ScrolledText(root, wrap=tk.WORD, width=60, height=20, font=("Arial", 12))
chat_area.pack(padx=10, pady=10)

# Input field
entry = tk.Entry(root, width=50, font=("Arial", 12))
entry.pack(side=tk.LEFT, padx=(10, 0), pady=(0, 10))

# Send button
send_button = tk.Button(root, text="Send", width=10, command=send_message)
send_button.pack(side=tk.LEFT, padx=(5, 10), pady=(0, 10))

# Start GUI loop


root.mainloop()

You might also like