0% found this document useful (0 votes)
5 views

Code

My project

Uploaded by

neha.kyadav02
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Code

My project

Uploaded by

neha.kyadav02
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Code

Import tkinter as tk

From tkinter import scrolledtext, messagebox

From nltk.chat.util import Chat, reflections

Import random

Import pyttsx3

# Define patterns and responses for the chatbot

Patterns = [

(r’hi|hello|hey’, [‘Hello!’, ‘Hi there!’, ‘How can I help you today?’]),

(r’(.*) balance’, [‘Before I can check your balance, could you please provide your account number?’]),

(r’my account number is (\d+)’, [‘Your account balance for account number {} is Rs.{}.’.format(‘{}’,
random.randint(1000, 5000)), ‘The balance for account number {} is Rs.{}.’.format(‘{}’,
random.randint(1000, 5000)), ‘Account number {} has a balance of Rs.{}.’.format(‘{}’,
random.randint(1000, 5000))]),

(r’transfer (.*) to (.*)’, [‘Transferring Rs.\\1 to account Rs.\\2.’, ‘Transfer of Rs.\\1 to account Rs.\\2
successful.’]),

(r’(?:what|which) types of loans (?:do you offer|are available)’, [‘We offer various types of loans
including personal loans, home loans, and car loans. What type of loan are you interested in?’]),

(r’(?:I want to borrow|\bborrow\b) (.+?)(?: home)? Loan’, [‘Great! Based on your request, you could
be eligible for a {} amount of {}. How would you like to proceed?’, ‘We can offer you a {} loan based on
your financial profile. How would you like to proceed?’]),

(r’(?:explain|define|tell me about) (?:the )?loan terms’, [‘We offer loan terms ranging from {} years to
{} months. What term length are you considering?’, ‘The loan term can be customized based on your
preference. Would you like a longer or shorter term?’]),

(r’(?:.*) (?:application process)’, [‘To apply for a loan, you can visit our website and fill out an online
application form. You will need to provide information about your income, employment, and credit
history.’]),

(r’(?:.*) (?:eligibility criteria)’, [‘Our eligibility criteria include factors such as credit score, income level,
and employment status. Would you like more detailed information about our eligibility requirements?’]),

(r’(?:.*) (?:open|create) (?:a)? (?:new)? (?:bank )?account’, [‘You can easily open a new account online
or visit our nearest branch.’]),
(r’(.*) (transaction history|transactions)’, [‘You can view your transaction history by logging into your
online banking account.’]),

(r’(.*) (help|assistance)’, [“Sure! How can I assist you today?”, “I’m here to help. What do you need
assistance with?”]),

(r’(.*)’, [“I’m sorry, I don’t understand. Could you please rephrase your question?”])

# Create a chatbot instance

Chatbot = Chat(patterns, reflections)

Class ChatbotGUI:

Def __init__(self, master):

Self.master = master

Master.title(“Banking Chatbot”)

Master.geometry(“400x450”)

Master.config(bg=”#f0f0f0”)

Self.chat_history = scrolledtext.ScrolledText(master, width=40, height=15, bg=”#ffffff”, font=(“Arial”,


10))

Self.chat_history.grid(row=0, column=0, padx=10, pady=10, columnspan=2)

Self.input_label = tk.Label(master, text=”You:”, bg=”#f0f0f0”, font=(“Arial”, 10))

Self.input_label.grid(row=1, column=0, padx=10, pady=5, sticky=”e”)

Self.user_input = tk.Entry(master, width=30, font=(“Arial”, 10))

Self.user_input.grid(row=1, column=1, padx=10, pady=5, sticky=”w”)

Self.send_button = tk.Button(master, text=”Send”, command=self.send_message, bg=”#0080ff”,


fg=”#ffffff”, font=(“Arial”, 10, “bold”))

Self.send_button.grid(row=2, column=0, columnspan=2, padx=10, pady=10)


Self.clear_button = tk.Button(master, text=”Clear History”, command=self.clear_history,
bg=”#ff0000”, fg=”#ffffff”, font=(“Arial”, 10, “bold”))

Self.clear_button.grid(row=3, column=0, padx=10, pady=10, sticky=”ew”)

Self.exit_button = tk.Button(master, text=”Exit”, command=self.exit_chatbot, bg=”#808080”,


fg=”#ffffff”, font=(“Arial”, 10, “bold”))

Self.exit_button.grid(row=3, column=1, padx=10, pady=10, sticky=”ew”)

# Initialize text-to-speech engine

Self.engine = pyttsx3.init()

Def send_message(self):

User_input = self.user_input.get()

Self.user_input.delete(0, tk.END)

Self.display_message(“You: “ + user_input)

Response = chatbot.respond(user_input)

Self.display_message(“Bot: “ + response.format(user_input.split()[-1]))

# Text-to-speech conversion for bot’s response

Self.engine.say(response)

Self.engine.runAndWait()

Def display_message(self, message):

Self.chat_history.configure(state=’normal’)

Self.chat_history.insert(tk.END, message + “\n”)

Self.chat_history.configure(state=’disabled’)

Self.chat_history.yview(tk.END)
Def clear_history(self):

Self.chat_history.configure(state=’normal’)

Self.chat_history.delete(1.0, tk.END)

Self.chat_history.configure(state=’disabled’)

Def exit_chatbot(self):

If messagebox.askokcancel(“Exit”, “Are you sure you want to exit?”):

Self.master.destroy()

Def main():

Root = tk.Tk()

App = ChatbotGUI(root)

Root.mainloop()

If __name__ == “__main__”:

Main()

You might also like