import numpy as np
import nltk
import string
import random
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# Load and preprocess the document
f = open(r'zain.txt', 'r', errors='ignore')
raw_doc = f.read().lower()
# Download necessary resources
nltk.download('punkt')
nltk.download('wordnet')
# Tokenization
sent_tokens = nltk.sent_tokenize(raw_doc)
word_tokens = nltk.word_tokenize(raw_doc)
# Lemmatization
lemmer = nltk.stem.WordNetLemmatizer()
def LemTokens(tokens):
return [lemmer.lemmatize(token) for token in tokens]
# Remove punctuation
remove_punct_dict = dict((ord(punct), None) for punct in string.punctuation)
def LemNormalize(text):
return LemTokens(nltk.word_tokenize(text.lower().translate(remove_punct_dict)))
# Greetings setup
GREET_INPUTS = ("hello", "hi", "greetings", "sup", "what's up", "hey")
GREET_RESPONSES = ["hi", "hey", "*nods*", "hi there", "hello", "I am glad! You are
talking to me"]
def greet(sentence):
for word in sentence.split():
if word.lower() in GREET_INPUTS:
return random.choice(GREET_RESPONSES)
# Function to generate a detailed response
def response(user_response):
# Handle specific question about the developer's name
if "developer" in user_response and "name" in user_response:
return "My developer's name is Rao Zain."
robo_response = ''
TfidfVec = TfidfVectorizer(tokenizer=LemNormalize, stop_words='english')
tfidf = TfidfVec.fit_transform(sent_tokens)
# Calculate similarity between user input and document sentences
vals = cosine_similarity(tfidf[-1], tfidf)
idx = vals.argsort()[0][-2]
flat = vals.flatten()
flat.sort()
req_tfidf = flat[-2]
# Check if the similarity score is sufficient
if req_tfidf == 0:
robo_response = "I am sorry! I don't understand you."
return robo_response
else:
# Provide a more detailed response by adding more sentences
start_idx = max(0, idx - 2) # Start 2 sentences before the most relevant
one
end_idx = min(len(sent_tokens), idx + 3) # End 3 sentences after the most
relevant one
# Collect a paragraph of sentences for more context
robo_response = ' '.join(sent_tokens[start_idx:end_idx])
return robo_response
Flask Backend
from flask import Flask, request, jsonify
from chatbot import response
app = Flask(__name__)
@app.route('/chat', methods=['POST'])
def chat():
user_input = request.json['input']
bot_response = response(user_input)
return jsonify({'response': bot_response})
if __name__ == '__main__':
app.run(debug=True)
HTML Frontend
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="(link unavailable)">
<link rel="stylesheet" href="(link unavailable)">
<script src="(link unavailable)"></script>
<link rel="stylesheet" href="style.css">
<title>ChatGPT-Like Chatbot</title>
</head>
<body>
<div class="chat-container">
<div class="chat-header">Aiza Chatbot</div>
<div class="chat-messages" id="chat-messages"></div>
<div class="message-input">
<input type="text" id="user-input" placeholder="Enter your message...">
<button id="send-button">
<span class="material-icons-outlined">send</span>
</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
setTimeout(() => addMessage("My name is Aiza. How can I help you today?"), 1000);
CSS Styles
body {
font-family: 'Poppins', sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.chat-container {
width: 400px;
height: 600px;
background-color: white;