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

Minor Project Report Akshita

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)
16 views

Minor Project Report Akshita

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
You are on page 1/ 108

SMART BUDDY – CHATBOT

(Minor Project)
Submitted in partial fulfillment of the requirements
for the award of the degree of

Bachelor of Computer Application


(2022-2025)
Academic Session 2024-2025

Submitted to: Submitted by:

Ms. ARNIMA PATHAK AKSHITA SHARMA


ASSISTANT PROFESSOR 03217002022
SEM- 5TH
SHIFT- 1ST

1
ACKNOWLEDGEMENT

I express my sincere gratitude to Ms. Arnima Pathak (Assistant Professor) for her valuable guidance and
timely suggestions during the entire duration of my dissertation work, without which this work would
not have been possible. I would also like to convey my deep regards to all other faculty members who
have bestowed their great effort and guidance at appropriate times without which it would have been
very difficult on my part to finish this work. Finally, I am grateful to my friends for their insightful
advice and encouragement. Their contributions, including pointing out my mistakes, have been crucial in
refining this dissertation.

2
CERTIFICATE FROM THE GUIDE

This is to certify that this project entitled "SMART BUDDY-CHATBOT " submitted in partial
fulfillment of the degree of Bachelor of Computer Applications to the "Tecnia Institute Of
Advanced Studies" through BCA done by Mr./Ms. Akshita Sharma, Roll No 03217002022 is an
authentic work carried out by him/her at Department of ICT under my guidance. The matter
embodied in this project work has not been submitted earlier for award of any degree to the best of
my knowledge and belief.

Signature of the student Signature of the Guide


Akshita Sharma Ms. Arnima Pathak
03217002022 (Assistant Professor)

3
SELF CERTIFICATE
This is to certify that the dissertation/project report entitled " SMART BUDDY-CHATBOT " is
done by me is an authentic work carried out for the partial fulfillment of the requirements for the
award of the degree of Bachelor of Computer Applications under the guidance of Ms. Arnima
Pathak (Assistant Professor). The matter embodied in this project work has not been submitted
earlier for award of any degree or diploma to the best of my knowledge and belief.

Signature of the student


Akshita Sharma
03217002022

4
INDEX
S/No. TOPIC PAGE NO. SIGN

1. Acknowledgement
2. Certificate From The Guide
3. Self-Certificate
4. List Of Figures
5. List Of Abbreviations
6. Synopsis
7. CHAPTER 01 : Objective & Scope
8. CHAPTER 02 : Theoretical Background & Definition Of
Problem
9. CHAPTER 03 : System Analysis & Design Vis-À-Vis User
Requirement
10. CHAPTER 04 : System Planning (Pert Chart)
11. CHAPTER 05 : Methodology Adopted , System
Implementation & Details Of Hardware And Software Used
system maintenance & evaluation

12. CHAPTER 06 : Detailed Life Cycle Of The Project


• ERD, DFD
• Input And Output Screen Design
• Process Involved
• Methodology Used Testing
• Test report & code sheet

13. CHAPTER 07 : Coding And Output Screenshots


14. CHAPTER 08 : Conclusion & Future Scope
15. CHAPTER 09 : References

5
LIST OF FIGURES

S.NO FIGURE NAME PAGE NO

1 PERT Chart

2 ER Diagram

3 Data flow Diagram

4 Input Screen Design

5 Output Screen Design

6
LIST OF ABBREVIATIONS

SNO. ABBREVIATION DESCRIPTION


1 NLP Natural Language Processing

2 AI Artificial Intelligence

3 ML Machine Learning

4 API Application Programming Interface


5 UI User Interface

7
SYNOPSIS

CHATBOT- SMART BUDDY

1. INTRODUCTION

Smart Buddy is an innovative chatbot developed using Python and a Database


Management System (DBMS). This approach is used to link queries with pre-trained
replies, allowing Smart Buddy to generate contextually relevant responses. Also, it is
a basic base model and can be further used in various fields after implementations.

2. PROBLEM DEFINITION
The project aims to develop a robust chatbot that:

1. Links Words to Responses: Utilizes algorithms to accurately match user inputs with
pre-trained replies based on word frequency.
2. Understands Context: Analyzes the context of user queries, enhancing the rele- vance
of responses for meaningful interactions.
3. Learns Continuously: Incorporates adaptive learning mechanisms, enabling the
chatbot to evolve and improve its performance through user interactions.
4. Utilizes Efficient Data Management: Integrates seamlessly with a database to ef-
ficiently store, manage, and retrieve training data for real-time responses.
5. Offers User-Friendly Experience: Features an intuitive interface designed for easy
navigation and interaction, enhancing user satisfaction.
6. Handles Diverse Language Inputs: Adapts to various dialects, slang, and sen- tence
structures, improving understanding across different user backgrounds.
7. Manages Multiple Intents: Capable of interpreting and responding to multiple
intents within a single user query, providing comprehensive answers.
8. Ensures Scalability: Designed to handle a growing volume of queries and data,
maintaining performance as user interactions increase.

3. LITERATURE REVIEW

The earliest chatbots were rule-based, relying on pattern matching to answer queries. While easier
to design and implement, they struggled with complex questions and often produced inaccurate
responses for unknown patterns. Additionally, manually creating these rules was time-consuming
and challenging. Unlike rule-based models, AI chatbots use machine learning algorithms to learn
from large datasets of human conversations. This removes the need for manually coded rules,
making them more flexible and less reliant on domain- specific knowledge. Chatbots and artificial
intelligence (AI) have been increasingly used into research and teaching in recent years,
particularly since the end of 2022.

8
4. OBJECTIVE

1. Improve Decision-Making: Offer suggestions or solutions based on customer inputs


or preferences.
2. Offer Quick Responses: Deliver information or solutions promptly to enhance user
satisfaction.
3. Handle Simple Queries: Manage basic, repetitive inquiries without needing human
intervention.
4. Mimic Human Conversation: Engage in natural, conversational language to make
interactions smoother.
5. Remain Available: Be accessible anytime to assist users with their needs.

5. METHODOLOGY

1. Requirement Analysis: Gathered user requirements through interviews and


surveys to understand the expectations and needs for an intelligent chatbot.

2. System Design: Designed the architecture of Smart Buddy, incorporating three main
components: Chatbot Module, Database Management System, and Python Interface
Module.

3. Development: Implemented the word occurrence formula algorithm for analyzing user
input.

4. Testing: Conducted thorough testing to ensure the accuracy and reliability of Smart
Buddy’s responses. Debugged and refined the system based on testing feedback.

5. Training and User Interaction: Initiated a training process for Smart Buddy by exposing
it to a diverse set of queries. Enabled the chatbot to learn from user interactions, refining
its knowledge base for better responses.

6. Deployment: Deployed Smart Buddy for user interaction, making it accessible through the
Python-based interface. Ensured proper integration with the Database Management System
for real-time data retrieval.

6. TOOLS

HARDWARE SYSTEM CONFIGURATION:


1. Processor- Intel(R) Core (TM) i5-8265U CPU @ 1.60GHz 1.80 GHz
2. Ram- 8.0 GB
3. Hard Disk- 1TB

9
SOFTWARE SYSTEM CONFIGURATION:

1. Database Management System (e.g., SQLite, MySQL)


2. User Interface: Designed the interface using Pythons Tkinter library for a user- friendly
experience.
3. Development Environment: Used a Python IDE or code editor (e.g., VSCode, PyCharm) for
coding and debugging.
4. Operating System: Smart Buddy is platform-independent (Windows, Linux,
5. macOS).
6. Version Control: Employed version control tools (e.g., Git) for collaborative development
and project management.

RESEARCH GAP

1. Understanding Variations in Language: Basic models struggle with slang, dialects, and
variations in sentence structure, often leading to misunderstandings.
2. Limited Context Awareness: Basic chatbots often fail to track and understand previous
parts of a conversation, leading to disjointed responses.
3. Handling Ambiguity: Difficulty in interpreting ambiguous or vague user inputs without clear
direction, resulting in incorrect responses.
4. Lack of Adaptive Learning: Most basic models do not improve their responses over time as they
lack self-learning or adaptive capabilities.
5. Rigid Responses: Chatbots tend to have pre-scripted or limited responses, reducing the
flexibility and natural flow of conversations.
6. User Intent Recognition: Limited ability to accurately detect complex user intents, especially
when there are multiple intents in a single query.

REFERENCE

1. https://towardsdatascience.com/how-to-build-your-own-chatbot-using- deep-
learning-bb41f970e281
2. https://www.edenai.co/post/top-free-chatbot-tools-apis-and-open-source-
models
3. https://doi.org/10.3390/info13010041
4. https://www.simplilearn.com/tutorials/python-tutorial/how-to-make-a-
chatbot-in-python
5. https://insights.daffodilsw.com/blog/the-history-and-evolution-of-chatbots
6. https://www.techtarget.com/searchcustomerexperience/infographic/The-
evolution-of-chatbots-and-generative-AI
7. https://www.researchgate.net/publication/336734161_A_Brief_History_of_
Chatbots

10
Chapter 01: Objective & Scope of the Project

The Smart Buddy chatbot project is designed with the goal of creating an intelligent conversational agent
that leverages Python programming and a Database Management System (DBMS) to provide contextually
relevant replies to user queries. Central to the chatbot’s functionality is the implementation of a Word
Occurrence Formula, which counts the frequency of words in a given user input. This formula allows the
chatbot to identify key terms in the user's sentence, forming the basis for determining the most relevant
response. By associating the frequency of specific words with pre-trained replies, the chatbot establishes a
direct link between the user's query and the most appropriate response based on the heaviest word
occurrence. The Python Interface ensures a smooth and intuitive interaction between the user and the
Smart Buddy, offering a conversational experience that is easy to navigate. Through the integration of a
DBMS, the chatbot is able to store, manage, and retrieve large sets of training data, which allows for more
dynamic and scalable learning. This integration also helps to ensure that the chatbot can improve over
time by adapting to new inputs and learning from previous interactions.As the system interacts with more
people and receives feedback, it can refine its responses, becoming more accurate and contextually aware.
Overall, the project is designed to create a chatbot that can not only understand and respond to user
queries but also evolve and enhance its performance with continued use.

Key Objectives:-

1. Word Occurrence Formula


• Frequency Analysis: The formula analyzes the frequency of individual words in user inputs to
identify the most significant terms for response generation.
• Contextual Word Weighting: It assigns varying weights to words based on their relevance,
ensuring that the most critical terms are given priority in response matching.
• Phrase Recognition: The system is capable of recognizing common phrases or key expressions,
enhancing its ability to accurately process diverse queries.

2. Response Linking
• Keyword Association: The chatbot maps user input to a predefined set of responses by
identifying key terms and their frequency within the query.
• Pattern Matching: It employs pattern recognition to link similar sentence structures or question
formats to appropriate responses.

3. Python Interface
• User-Friendly Interaction: The interface facilitates intuitive user interaction, providing a
seamless experience through a text-based or graphical interface.
• Cross-Platform Accessibility: It supports multiple platforms, enabling users to interact with the
chatbot via web, desktop, or mobile applications.
• Real-Time Response: The interface is designed for fast, real-time feedback, allowing users to
receive prompt answers to their inquiries.

4. Database Integration
• Data Persistence: A robust database management system (DBMS) is used to persistently store user
interactions and training data for ongoing learning and adaptability.

11
• Efficient Data Retrieval: The system ensures rapid access to stored information, enabling the chatbot
to provide relevant, accurate responses efficiently.

5. Adaptive Learning
• Continuous Improvement: The chatbot evolves over time by learning from user interactions,
progressively refining its ability to generate contextually appropriate responses.
• User Feedback Integration: The system incorporates direct user feedback to adjust its response
strategies, thereby enhancing overall performance.
• Contextual Adaptability: It learns to recognize nuanced language, slang, and varying sentence
structures, improving its capacity to understand diverse user queries.

Project Scope:
The Smart Buddy Chatbot project is aimed at developing an intelligent conversational agent using
Python programming and integrating it with a Database Management System (DBMS). The core
functionality of the chatbot is to process user inputs, identify significant keywords, and generate
contextually relevant responses. This project is designed to create a chatbot capable of providing adaptive,
conversational experiences, improving through continuous learning.

The scope of the Smart Buddy chatbot project encompasses the following key components and
functionalities:

• Core Functionality:

• Word Occurrence Formula: The chatbot will implement a formula that counts the frequency of
words in a user's input, identifying key terms to trigger the most relevant responses.
• Response Matching: The chatbot will associate words and phrases from the user's input with pre-
trained responses in a database, ensuring contextually suitable replies based on keyword
occurrences.

• Technology Stack:

• Programming Language: The chatbot will be developed using Python, leveraging libraries such
as NLTK for Natural Language Processing (NLP) and SQLite/MySQL for database management.
• Database Management: The project will incorporate a DBMS to store user queries, responses,
and training data. This will allow the chatbot to efficiently retrieve and adapt based on historical
interactions.

• Adaptive Learning Mechanism:

• Learning from Interactions: The chatbot will continuously learn from user interactions,
improving its ability to generate contextually appropriate and accurate responses.
• User Feedback: The system will incorporate user feedback mechanisms to adapt and refine its
responses over time, enhancing overall performance.

12
• Future Expansion and Enhancements

• Integration with External APIs: The chatbot will support integration with third-party APIs for
retrieving real-time data like weather, news, and calendar events. This feature will add value by
allowing the chatbot to deliver dynamic information. APIs could include weather services (e.g.,
OpenWeatherMap), news APIs (e.g., NewsAPI), and calendar or task management integrations
(e.g., Google Calendar).

• Data Storage for Training and User Interactions:

• Structured Database Design: Design a DBMS schema to store user queries, responses, and training
data. This includes tables for managing user input, response logs, user profiles, and feedback.
• Ensure that the database supports efficient data retrieval for real-time responses and stores
conversational data for analysis and further training.

• Data Management and Security:

• Implement robust data validation and sanitization methods to prevent SQL injection attacks and
ensure data integrity.
• Ensure that sensitive data, such as user information, is handled according to data privacy
regulations like GDPR and CCPA. This includes encryption, and secure data access controls.

13
Chapter 02: Theoretical Background Definition of Problem
Theoretical Background:

The foundation of Smart-Buddy’s architecture lies in its use of transformer-based models like GPT, which
excel at processing sequential data and generating human-like text. These models are pre-trained on vast
datasets comprising diverse topics and styles, allowing Smart Buddy to possess a broad understanding of
language and context. Through fine-tuning on specific data, Smart Buddy can specialize for distinct tasks,
whether it’s customer support, financial advice, educational assistance, or technical troubleshooting. This
adaptability ensures that Smart-Buddy remains not only conversationally fluent but also deeply
knowledgeable in any designated field. One of chatbot’s most distinctive features is its ability to track
context, maintaining continuity throughout a conversation.

This continuity enables Smart Buddy to recall past messages, answer follow-up questions accurately, and
deliver responses that are consistent with previous answers, a key factor in building trust and rapport with
users. Additionally, the model’s reinforcement learning capabilities allow it to improve with user
feedback, iterating on previous interactions to enhance future performance and response accuracy. It also
prioritizes ethical AI considerations.

With mechanisms to mitigate biases and protect user privacy, the model adheres to the latest standards in
responsible AI, making it suitable for sensitive applications. Through Natural Language Understanding
(NLU) capabilities, Smart Buddy can identify user intent, sentiment, and even detect nuanced emotional
cues, enriching its responses to provide a more empathetic and humanized experience.The chatbot’s
scalability means it can handle vast volumes of interactions without compromising quality, making it an
invaluable tool for companies looking to deliver efficient yet personalized support. It’s an intelligent
conversational agent that bridges the gap between human and machine interaction, harnessing the power
of deep learning to deliver engaging, informative, and contextually aware conversations in any
environment where it’s deployed.

• Natural Language Processing (NLP):


Natural Language Processing is a field of artificial intelligence that focuses on the interaction between
computers and human language. In the context of Smart Buddy, NLP techniques are leveraged to
analyze and understand the semantics of user input. This involves parsing sentences, extracting
meaningful information, and facilitating the generation of contextually relevant responses.

• Word Occurrence-Based Algorithms:


Word occurrence-based algorithms are foundational in text analysis and information retrieval. In
Smart Buddy, this theoretical concept is applied to count the occurrences of each word in a sentence.
By associating these occurrences with pre-trained replies, the chatbot aims to determine the most
contextually appropriate response for a given input. This approach draws inspiration from statistical
language modeling and aims to capture the importance of specific words in understanding the user's
intent.

14
• Adaptive Learning:
Adaptive learning refers to the chatbot's ability to evolve and improve its responses over time. As
users interact with Smart Buddy, the chatbot dynamically learns from the input and refines its
knowledge base. This adaptive learning process is crucial for continuously enhancing the chatbot's
ability to provide suitable replies based on the unique word occurrence formula.

• Database Management Systems (DBMS):


DBMS plays a pivotal role in storing and retrieving training data for Smart Buddy. The integration of
a DBMS allows efficient management of the dataset, supporting the chatbot's learning process. By
structuring data in a database, Smart Buddy can easily access and update information, contributing to
its adaptability and responsiveness.

• Access Control and Authentication:


To ensure that only authorized personnel can access or modify the system, The security measures
prevent unauthorized individuals from altering training data, adjusting chatbot responses, or accessing
sensitive user information, thereby preserving the integrity of the system.

• Bias Mitigation Techniques:


Uses bias mitigation techniques during model training and adaptive learning, ensuring that the chatbot
does not favor or discriminate against any user group based on gender, ethnicity, or any other sensitive
criteria. Further help to identify and address any emerging biases, promoting fair and inclusive
interactions for all users.

Definition of the Problem:

In an increasingly digital world, where customers and users demand quick, accurate, and human-like
interactions, there is a growing need for intelligent conversational agents that can handle complex user
queries. The project addresses this problem by developing a chatbot that not only understands but also
responds appropriately to user inquiries based on contextual relevance. Unlike simple rule-based bots,
which rely on rigid, predefined scripts, designed to recognize the significance of words and phrases within
user input, allowing it to deliver responses that feel natural and tailored to each conversation. This project
aims to tackle several core challenges in building an effective chatbot:

1. Links Words to Responses

Uses word frequency analysis algorithms to interpret the importance of each word within user queries. By
evaluating the frequency of specific words and phrases, the chatbot can identify core keywords that shape
the query's intent. This approach allows chatbot to map user input to the most suitable pre-trained
responses, ensuring that the replies are contextually accurate. Rather than simply matching keywords,
algorithm understands how often certain words appear in different contexts, enabling it to make precise
associations between input and responses.

15
2. Understands Context

Beyond word matching, it is designed to grasp the broader context of each conversation. By analysing the
surrounding language, sentence structure, and previous messages within a conversation, enhances
response relevance. This contextual understanding ensures that the chatbot doesn’t deliver isolated or
generic responses; instead, it adapts its answers based on the user's intent, previous inputs, and any
ongoing themes in the conversation. As a result, it is able to provide meaningful, contextually appropriate
answers that engage users in dynamic interactions.

3. Learns Continuously

incorporates adaptive learning mechanisms to improve its performance over time. Through machine
learning techniques, the chatbot learns from user interactions, adjusting its responses based on feedback
and common patterns. For example, if users consistently correct or refine the chatbot’s responses, adaptive
learning will pick up on these adjustments, fine-tuning its understanding of complex queries. This
continuous learning ability enables it to evolve and grow more accurate with each interaction, making it
increasingly effective and reliable for users.

4. Utilizes Efficient Data Management

To support real-time responses, integrates seamlessly with a Database Management System (DBMS). The
DBMS stores and organizes training data, user interactions, and any updates from adaptive learning.
Efficient data management allows it to quickly retrieve relevant responses without delays. By optimizing
data storage and retrieval, it can offer rapid responses, essential for maintaining a smooth user experience.
This integration also aids in organizing historical data, enabling it to reference past conversations when
delivering responses.

5. Offers User-Friendly Experience

A core focus of it’s design is its intuitive and user-friendly interface. This interface is built to streamline
the interaction experience, providing users with an easy-to-navigate chat environment. Clear prompts,
simple instructions, and straightforward buttons make it accessible for users of all backgrounds, regardless
of technical expertise. The interface is structured to minimize friction in interactions, ensuring that users
can effortlessly input queries and receive replies, which in turn enhances overall user satisfaction and
engagement.

6. Handles Diverse Language Inputs

Designed to understand and respond to varied language inputs, including dialects, slang, abbreviations,
and unconventional sentence structures. With advanced natural language processing (NLP) capabilities,
the it can parse through colloquial expressions and informal language, adjusting its understanding
accordingly. This adaptability improves the relevance across different user demographics and
geographical regions, making it capable of engaging in conversations with a diverse user base while
providing accurate responses regardless of language variations.

16
7. Manages Multiple Intents

In many cases, users may ask questions that contain multiple requests or topics within a single query. It is
capable of identifying and addressing multiple intents within a single message, allowing it to deliver
comprehensive answers that address each component. By analysing sentence structure and isolating
different intents, the chatbot can respond with multiple answers in a coherent way. This capability ensures
that users receive full responses to complex queries without having to rephrase or split them up, thereby
improving interaction efficiency.

8. Ensures Scalability

It is designed with scalability in mind, making it capable of handling an increasing volume of interactions
as user demand grows. The system architecture supports the addition of more data and increased
processing capacity without compromising performance. Whether it’s handling a surge in concurrent
queries or expanding to support new functionalities, design ensures that it can scale seamlessly. This
flexibility makes it a robust solution for businesses and organizations that anticipate growing chatbot
usage over time, as it maintains response speed and quality even under high demand.

Addressing these challenges will enable to create a chatbot that is not only capable of delivering accurate
and relevant responses but also adaptable and user-centric. By analysing word frequency and learning
from user interactions, also will be able to serve as an intelligent conversational agent that grows and
improves with each interaction. These functionalities collectively make it a versatile, responsive, and user-
centric chatbot, capable of delivering accurate, contextually relevant, and meaningful interactions across
various use cases and applications

17
Chapter 03: System Analysis & Design vis-a-vis User
Requirements
System Analysis:

The system analysis focuses on creating a robust yet straightforward platform for efficient user interaction,
leveraging essential components like a keyword-based response algorithm, a lightweight SQLite database,
and a simple user interface (UI) designed for ease of use. The chatbot’s primary functionality centers
around mapping user inputs to pre-defined responses through keyword matching, allowing the system to
respond quickly to a wide range of common queries. This approach keeps the design lightweight and
highly responsive, as the bot can quickly identify relevant keywords and retrieve the correct responses
from the database. SQLite serves as an ideal choice for data storage in this project due to its minimal setup
requirements and compatibility with Python, ensuring that user data, queries, and response histories are
stored efficiently without the need for a separate server setup. To improve user experience, the chatbot
uses a simple interface where users can easily enter queries and get answers. This interface is created
using either Flask for web-based interactions for desktop applications. Even novice users may easily
operate the chatbot thanks to important UI features including input fields, clear response display regions,
and optional feedback prompts. The system's modular design makes it simple to scale in the future,
enabling the addition of sophisticated features like machine learning for adaptive replies for improved
intent identification without interfering with core functions. In order to maintain the chatbot's
dependability and credibility, the analysis also places a strong emphasis on data protection and security,
including measures to anonymize user data and prevent unwanted access. By focusing on core functional
requirements, a modular and adaptable design, and efficient data management, this chatbot can meet user
needs effectively, offering a responsive, intuitive interaction experience with potential for seamless future
upgrades.

Key User Requirements:

1. Accurate Response Matching


• Description: The chat-bot must be able to recognize keywords and intents in user queries and
retrieve appropriate pre-defined responses. This requires a simple, rule-based or keyword-
matching system where it can quickly analyze user input and deliver responses that directly
address the user’s needs.
• Impact: Ensures that users receive correct and coherent answers, which is critical for building trust
and reliability in the capabilities.

2. Ease of Use and Simplicity


• Description: The chat-bot should have a straightforward, easy-to-navigate interface where users
can type queries naturally and receive answers without needing to learn specific commands or
syntax. The responses should also be clear and concise to make interactions smooth.
• Impact: A user-friendly design makes it easier for individuals to engage with the chat-bot,
leading to a better experience and increased user satisfaction.
18
3. Basic Context Awareness
• Description: Although basic, the chatbot should have a simple form of context awareness to
handle multi-turn conversations, like responding correctly if a user follows up on a previous
question. Basic context awareness could involve remembering the last query within a session to
provide relevant answers to follow-up questions.
• Impact: Adds coherence to conversations, making interactions feel more natural and less like
isolated exchanges.

4. Quick and Responsive Interaction


• Description: The chatbot should be optimized for quick processing, ensuring minimal lag
between user input and chatbot replies. This can be achieved with efficient algorithms and a
lightweight design that can handle responses in real time without requiring extensive processing
power.
• Impact: Fast responses make interactions feel more conversational, providing a positive user
experience and reducing frustration from waiting.

5. Ability to Handle Frequently Asked Questions (FAQs)


• Description: By integrating a set of frequently asked questions and standard responses, the
chatbot can address a range of common user needs. This basic functionality helps the chatbot
serve as a reliable, self-help tool for general queries, reducing the need for human intervention in
simple cases.
• Impact: Enhances the chatbot’s value by allowing it to respond to routine questions, saving time
for both users and support staff.

6. Error Handling and Guidance


• Description: It should be able to recognize when it doesn’t understand a query and provide
guidance, such as asking the user to rephrase the question or offering options to clarify. This
could involve returning a polite message like “I’m sorry, I didn’t understand. Could you try
asking in a different way?”
• Impact: Improves user experience by preventing frustration, helping users feel guided and
supported even when the chatbot doesn’t have the perfect answer.

7. Simple Feedback Mechanism


• Description: It should offer a basic feedback option, allowing users to rate responses or report
inaccuracies. This feedback can be used to refine the responses and improve accuracy over time.
• Impact: Enables continuous improvement of the performance based on real user interactions,
which increases overall user satisfaction.

8. Privacy and Data Security


• Description: While collecting minimal data, ensure that any stored data is protected and
anonymized where possible. For example, if user interaction history is retained for training
purposes, sensitive data should be removed or encrypted to protect user privacy.
• Impact: Builds user trust by safeguarding data and adhering to basic privacy standards, especially
important if it is handling any personal information.
19
These key user requirements lay the foundation for a chatbot that is functional, user-friendly, and capable
of delivering a satisfying interaction experience, even without advanced AI capabilities.
.
Key User Requirements-
1. User Interface (tkcanvas.py):
Responsible for creating the graphical user interface (GUI) for Smart Buddy.
Allows users to interact with the chatbot by inputting queries and receiving responses.
Developed using Python's Tkinter library.

2. Backend Components:
• Bot Logic (bot.py):
Core logic of Smart Buddy. Implements the word occurrence formula, response generation, and
adaptive learning mechanism.

• Message Processor (messageprocessor.py):


Scans and processes user messages.

Communicates with the Bot Logic to analyze and generate responses

3. Data Storage (liststore.py, sqlitestore.py, mysqlstore.py):


• List Store (liststore.py):
Stores data in Python lists (for testing or small datasets).

• SQLite Store (sqlitestore.py):


Utilizes SQLite as the Database Management System (DBMS) for storing and retrieving training data.

• MySQL Store (mysqlstore.py):


Uses MySQL as the DBMS for larger datasets and scalability.

4. Interactions:
• User Interaction:
Users interact with the User Interface (tkcanvas.py) to input queries.

• Backend Interaction:
The User Interface communicates with the Backend Components (bot.py, messageprocessor.py) to
process queries and generate responses.
• Data Interaction:
Backend Components interact with the Data Storage (liststore.py, sqlitestore.py, mysqlstore.py) to
retrieve and update training data.

System Scalability:

20
List Store vs. Database Stores:
The List Store is suitable for testing and small datasets.
SQLite and MySQL Stores provide scalability for larger datasets and better performance.

MySQL Store for Scalability:


MySQL Store can handle a growing dataset and accommodate more extensive user interactions.

Design Considerations:
1. Pre-Defined Responses and FAQs

• Consideration: A basic chatbot should rely on a comprehensive list of pre-defined responses for
common queries.
• Design Approach: Create a set of FAQs and canned responses to cover the most likely user
questions. Organize these responses in a database or structured file, enabling easy retrieval based
on recognized keywords or intents.
• Objective: Ensures the chatbot has answers to frequently asked questions, making it helpful and
reliable for common inquiries.

2. User Interface (UI) and Experience (UX)

• Consideration: An intuitive and accessible UI/UX is essential, even for basic chat-bot.
• Design Approach: Uses a clean interface where users can easily see and interact with the chat-bot.
Design clear text boxes for input and output, and include minimalistic icons, buttons, or prompts to
guide users through the process.
• Objective: Ensures a smooth user experience, reducing friction and frustration for users who may
be interacting with the chat-bot for the first time.

3. Modular and Scalable Design

• Consideration: Although basic, the chat-bot should be designed with scalability in mind to
accommodate future improvements.
• Design Approach: Uses a modular design where each component (e.g., intent recognition,
response generation) can be improved independently without major redesigns. This approach
allows for incremental upgrades, like adding a database for responses or integrating advanced NLP
later.
• Objective: Facilitates future expansion and improvement, making the chat-bot adaptable to
evolving requirements.

4. Real-Time Processing and Responsiveness

• Consideration: Users expect the chat-bot to respond quickly without noticeable lag.
• Design Approach: Use efficient algorithms and lightweight code that minimizes response time.
Avoid resource-intensive processes in the backend, which can slow down interactions. If
necessary, pre-fetch common responses to reduce processing delays.

21
• Objective: Ensures that interactions feel natural and conversational, which is especially important
for maintaining user interest.

5. Error Logging and Basic Analytics

• Consideration: Logging errors and tracking user interactions provide insights into how the chat-
bot can improve.
• Design Approach: Implement basic logging to track instances where the chat-bot fails to
understand user input or where it receives repeated questions. Include analytics that shows popular
queries, response times, and user satisfaction based on feedback.
• Objective: Enables developers to monitor performance, identify recurring issues, and make data-
driven improvements.

By carefully analyzing these requirements and design considerations, the project can be developed to meet
user needs effectively and provide a valuable tool for farmers looking to optimize their crop selection,
resource use, and overall productivity.

This system architecture provides a modular and scalable design for Smart Buddy, separating the user
interface, backend logic, and data storage components. It accommodates different data storage options
based on the scale of the chat-bot application. The interaction flow ensures a seamless user experience
while the adaptive learning mechanism improves the chatbot's performance over time.

22
Chapter 04: System Planning (PERT Chart)

PERT CHART

23
Chapter 05: Methodology adopted; System Implementation &
Details of Hardware & Software used System Maintenance &
Evaluation

Methodology Adopted:
1. Requirement Analysis:
• Gathered user requirements through interviews and surveys to understand the expectations and
needs for an intelligent chatbot.
• Defined functional and non-functional requirements, emphasizing the word occurrence formula
and adaptive learning.

2. System Design:
• Designed the architecture of Smart Buddy, incorporating three main components: Chatbot
Module, Database Management System, and Python Interface Module.
• Defined the word occurrence formula, adaptive learning mechanism, and database schema.
• Specified the design of the Python-based interface for a seamless user experience.

3. Development:
• Implemented the word occurrence formula algorithm for analyzing user input.
• Developed the adaptive learning mechanism to allow the chatbot to evolve and improve over
time.
• Integrated a Database Management System to efficiently store and retrieve training data.
• Implemented the Python-based interface for user interactions.

4. Testing:
• Conducted thorough testing to ensure the accuracy and reliability of Smart Buddy's responses.
• Included test cases covering various scenarios to validate the effectiveness of the word occurrence-
based approach.
• Debugged and refined the system based on testing feedback.

5. Training and User Interaction:


• Initiated a training process for Smart Buddy by exposing it to a diverse set of queries.
• Enabled the chatbot to learn from user interactions, refining its knowledge base for better
responses.
• Emphasized continuous user interaction to enhance the chatbot's adaptive learning.

6. Deployment:
• Deployed Smart Buddy for user interaction, making it accessible through the Python-based
interface.
• Ensured proper integration with the Database Management System for real-time data retrieval.

System Implementation:
1. Word Occurrence Formula Algorithm (Python):
• Developed a Python algorithm to count the occurrences of words in a given sentence.
24
• Linked the word occurrences to pre-trained replies to generate contextually relevant responses.

2. Adaptive Learning Mechanism (Python):


• Implemented a learning mechanism to enable Smart Buddy to adapt and improve its responses
over time.
• Dynamically updated the knowledge base based on user interactions.

3. Database Management System (DBMS):


• Integrated a DBMS (e.g., SQLite, MySQL) for efficient storage and retrieval of training data.
• Designed a database schema to manage word occurrences and their corresponding replies.

4. Python-Based Interface:
• Developed a user-friendly interface using Python's GUI libraries (e.g., Tkinter).
• Enabled users to input queries and receive responses seamlessly through the interface.

Details of Hardware & Software Used:


Hardware:
The hardware requirements for Smart Buddy are minimal:
A standard computer or server to host the chatbot.
Adequate storage for database management.
1. Processor- Intel(R) Core (TM) i5-8265U CPU @ 1.60GHz 1.80 GHz
2. Ram- 8.0 GB
3. Hard Disk- 1TB

Software:
1. Database Management System (e.g., SQLite, MySQL)
2. User Interface: Designed the interface using Pythons Tkinter library for a user- friendly
experience.
3. Development Environment: Used a Python IDE or code editor (e.g., VSCode, PyCharm) for
coding and debugging.
4. Operating System: Smart Buddy is platform-independent (Windows, Linux,
5. macOS).
6. Version Control: Employed version control tools (e.g., Git) for collaborative development
and project management.

Programming Language:
Smart Buddy is built entirely using Python for its core logic, interface, and data management.

Database Management System:


Integrated a DBMS (e.g., SQLite, MySQL) to store and retrieve training data efficiently.

25
User Interface:
Designed the interface using Python's Tkinter library for a user-friendly experience.

Development Environment:
Used a Python IDE or code editor (e.g., VSCode, PyCharm) for coding and debugging.

Operating System:
Smart Buddy is platform-independent and can run on various operating systems (Windows, Linux,
macOS).

Version Control:
Employed version control tools (e.g., Git) for collaborative development and project management.

These details provide an overview of the methodology, system implementation, and the hardware and
software environment for your Smart Buddy chatbot project. Adapt and expand these sections based on
specific tools, libraries, and platforms used in your implementation.

System Maintenance:
1. Continuous Monitoring:
Implement a monitoring system to track the chatbot's performance and user interactions.
Regularly review system logs, error reports, and user feedback to identify potential issues.

2. Bug Tracking and Resolution:


Establish a bug tracking system to document and prioritize reported issues.
Schedule regular bug-fixing sessions to address identified issues promptly.

3. Database Management:
Periodically optimize the database for improved performance.
Regularly backup and maintain the database to prevent data loss.

4. Adaptive Learning Updates:


Continue the adaptive learning process by exposing the chatbot to new queries and user interactions.
Periodically update the training dataset to enhance the chatbot's knowledge base.

5. User Training:
Provide ongoing training sessions for users to familiarize them with Smart Buddy's capabilities and
features.
Offer guidance on how users can optimize interactions for better responses.

System Evaluation:
1. Performance Metrics:
Define key performance metrics such as response time, accuracy, and user satisfaction.
Regularly evaluate these metrics to ensure the chatbot meets performance expectations.
26
2. User Feedback Analysis:
Solicit user feedback through surveys or feedback mechanisms within the chatbot interface.
Analyze user comments and suggestions to identify areas for improvement.

3. Accuracy Testing:
Conduct periodic accuracy testing to evaluate the chatbot's ability to provide contextually relevant
responses.
Use a diverse set of test cases to assess the chatbot's effectiveness.

4. Scalability Assessment:
Evaluate the system's scalability to accommodate an increasing number of users and a growing dataset.
Identify potential bottlenecks and implement optimizations as needed.

5. Security Audits:
Perform regular security audits to identify and address potential vulnerabilities.
Ensure that user data is securely stored and that the chatbot is resilient to security threats.

6. User Experience:
Conduct usability testing to assess the user interface's intuitiveness and effectiveness.
Implement updates based on user experience evaluations to enhance interface design.

7. Adaptability and Learning:


Assess the chatbot's adaptability by introducing new scenarios and queries.
Evaluate the learning mechanism to verify that the chatbot continues to improve over time.

8. Version Control and Documentation:


Maintain version control for the chatbot's codebase to track changes and updates.
Keep documentation up-to-date to aid in system maintenance and troubleshooting.

Development Tools:
Programming Language: Python

• Why Python: Python is popular for chatbot development due to its simplicity, readability, and
strong support for AI, machine learning, and natural language processing (NLP) libraries.
• IDE: Use an integrated development environment like PyCharm, Visual Studio Code, or
Jupyter Notebook to write and test Python code.
• Version: Python 3.x, as it provides better performance and support for modern libraries.

Database Management System (DBMS)

• SQLite: Ideal for basic chatbots because it’s lightweight, requires minimal setup, and is embedded
in Python’s standard library. It’s excellent for managing small-scale data needs without requiring a
separate server.

27
• MySQL or PostgreSQL: If your chatbot project grows and requires more robust data handling,
these relational databases offer scalable solutions. MySQL and PostgreSQL are compatible with
Python and can handle larger datasets and more complex queries.

Python Libraries for Database Connection:

• SQLite: sqlite3 (built-in with Python)


• MySQL: mysql-connector-python or SQLAlchemy

Flask for Web Interface

• Flask: A lightweight Python web framework perfect for creating a simple web interface for the
chatbot. Flask makes it easy to create endpoints that can receive user messages and return chatbot
responses.

Conclusion: Smart Buddy's System Maintenance and Evaluation strategies aim to ensure ongoing
reliability, performance, and user satisfaction. Regular monitoring, bug resolution, database management,
and continuous learning will contribute to the chatbot's effectiveness and adaptability over time.

28
Chapter 06: Detailed Life Cycle of the Project

• Entity Relationship Diagram

29
• Data Flow Diagram

30
Input and Output Screen Design

The Input Screen consists of an entry widget for users to input their questions and a submit button to
trigger the processing of the input.The Output Screen contains a text widget to display the conversation
between the user and Smart Buddy. It updates with each user interaction, showing both the user's input
and Smart Buddy's response.
Both the input and output screen come under a canvas which makes it a user-friendly chat interface like
any other online chatting platform.

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
botTk.py
from messageProcessor import process, start, end

# start conversation with greeting message


# in an endless loop till user enters empty line
def run():
start()
botMessage = 'hello'
while True:
47
print("B: " + botMessage)
userMessage = str(input("U: "))
if userMessage == "":
end()
break
botMessage = process(userMessage, botMessage)

#run()

listStore.py
# # list of messages where each message is a tuple of text and usage
# messageStore = []

# # list of words where each word is a text


# wordStore = []

# # list of associations where each association is a tuple (wordId - messageId - weight)


# associationStore = []

# # list of results where each result is a tuple (messageId - weight)


# resultStore = []

# dictionary where key is name of storage and value is a list


botStorage = {}

def connect():
create("messageStorage")
create("wordStorage")
create("associationStorage")

def save():
# save storage to file
print("saving")

def load():
# load storage from file
print("loading")

def update(entityName, elementName, entityIndex):


entity = getEntity(entityName)
elementIndex = getElementIndex(entityName, elementName)

# find record from list for given index


tempTuple = entity[entityIndex]

# convert tuple list into dictionary


tempDict = {0: tempTuple[0], 1: tempTuple[1]}
48
tempDict[elementIndex] = tempDict.get(elementIndex, 0) + 1

# convert dictionary into tuple list


tempTuple2 = (tempDict[0], tempDict[1])

# update list record for given index


entity[entityIndex] = tempTuple2

def minId():
"""return least used message id from messageTupleStore"""
minId = -1

entity = getEntity("messageStorage")

length = len(entity)

if length = = 0:
return minId
elif length = = 1:
return 0
else:
minId = 0
minUsage = entity[0][1]

for id in range (1, length):


messageTuple = entity[id]
usage = messageTuple[1]
if usage < minUsage:
minUsage = usage
minId = id

return minId

def validateExists(entityName, key):


entity = getEntity(entityName)
return key > = 0 and key < len(entity)

def group(entityName, elementName):


tempDict = {}
tempList = []
tupleList = getEntity(entityName)
index = getElementIndex(entityName, elementName)
for sentence in tupleList:
sentenceId = sentence[index]
49
tempDict[sentenceId] = tempDict.get(sentenceId, 0) + sentence[1]
for key in tempDict:
tempTuple = (key, tempDict[key])
tempList.append(tempTuple)
return tempList

def sort(entityName, elementName):


index = getElementIndex(entityName, elementName)
tupleList = getEntity(entityName)
tupleList.sort(key = lambda x: x[index] , reverse = True)
return tupleList

def create(entityName):
botStorage[entityName] = []

def getResult():
sentenceId = -1
if validateExists("resultStorage", 0):
group("resultStorage", "messageId")
sort("resultStorage", "weight")
resultTuple = retrieve("resultStorage", 0)
sentenceId = resultTuple[0]
return sentenceId

def getEntity(entityName):
entity = botStorage[entityName]
return entity

def insert(entityName, item):


entity = getEntity(entityName)

# store item in list if not already stored


if item not in entity:
entity.append(item)

# return ID of item in list


return entity.index(item)

def retrieve(entityName, itemIndex):


entity = getEntity(entityName)
return entity[itemIndex]

50
def retrieveElement(tupleEntity, entityName, elementName):
index = getElementIndex(entityName, elementName)
return tupleEntity[index]

def getElementIndex(entityName, elementName):


index = -1

if entityName = = 'associationStorage':
if elementName = = 'wordId':
index = 0
elif elementName = = 'messageId':
index = 1
elif elementName = = 'weight':
index = 2
elif entityName = = 'wordStorage':
if elementName = = 'wordId':
index = 0
elif entityName = = 'messageStorage':
if elementName = = 'messageId':
index = 0
elif elementName = = 'usage':
index = 1
elif entityName = = 'resultStorage':
if elementName = = 'messageId':
index = 0
elif elementName = = 'weight':
index = 1
return index

def findAll(entityName, elementName, elementValue):


entity = getEntity(entityName)
entryList = []
index = getElementIndex(entityName, elementName)
for entry in entity:
if entry[index] = = elementValue:
entryList.append(entry)
return entryList

messageProcessor.py
import random
from math import sqrt
#import listStore as store
#import sqliteStore as store
import mySqlStore as store

def start():
store.connect()
51
# store.load()

def end():
# store.save()
print()

def updateUsageCountOfMessage(responseKey):
return store.update("messageStorage", "usage", responseKey)

def validKey(responseKey):
return store.validateExists("messageStorage", responseKey)

def getMessage(responseKey):
messageTuple = store.retrieve("messageStorage", responseKey)
return messageTuple[0]

def getLeastUsedMessageId():
return store.minId()

def getResult():
sentenceId = -1 # wrong value as default
sentenceId = store.getResult()
print("!!!!!!!!sentenceId: ")
print(sentenceId)
return (sentenceId,)

def saveResults(association, weight):


# create result tuple (messageId, weight)
messageId = association[1]
associationWeight = association[2]
messageTuple = store.retrieve("messageStorage", (messageId,))
usage = messageTuple[1]
resultWeight = associationWeight * weight / (4 + usage)
resultTuple = (messageId, resultWeight)

store.insert("resultStorage", resultTuple)

def createResultStore():
# resultStorage list is created in starting
# empty list to use again
store.create("resultStorage")

def findAssociations(wordId):
associations = store.findAll("associationStorage", "wordId", wordId)
return associations

52
def storeAssociation(association):
return store.insert("associationStorage", association)

def storeMessage(message):
messageTuple = (message, 1) # usage default value is 1
messageId = store.insert("messageStorage", messageTuple)
return messageId

def storeWord(word):
return store.insert("wordStorage", word)

def getWordWeight(occurrence, length):


weight = sqrt(occurrence / float(length))
return weight

def getWordsLength(wordsWithOccurrences):
length = sum([occurrence * len(word) for word, occurrence in wordsWithOccurrences])
return length

def getWordsWithOccurrences(sentence):
# get list of words in a sentence
words = sentence.split()
# TODO: handle punctuation

# create word dictionary (unique lower case word, occurrence)


wordDict = {}
for word in words:
word = word.lower()
wordDict[word] = wordDict.get(word, 0) + 1

# convert dictionary to tuple list


wordsWithOccurrences = []
for key in wordDict:
wordTuple = (key, wordDict[key])
wordsWithOccurrences.append(wordTuple)

return wordsWithOccurrences

def prepareResponse(responseKey):
# if message with given message Id is not found
# then get message Id of least used message
if validKey(responseKey) == False:
print("%%%%%%%%%% rkey1: ")
print(responseKey)
responseKey = getLeastUsedMessageId()

53
# get message text
print("%%%%%%%%%% rkey2: ")
print(responseKey)
reply = getMessage(responseKey)

# update used count of message by +1


updateUsageCountOfMessage(responseKey)

# return message text


return reply

def analyze(userMessage):
# get details of words from user message
wordsWithOccurrences = getWordsWithOccurrences(userMessage)
length = getWordsLength(wordsWithOccurrences)

# create empty list of result sets (message Id, weightage)


createResultStore() # each result is a tuple (messageId - weight)

# save list of result sets from existing linking


for word, occurrence in wordsWithOccurrences:
# get word details
wordId = storeWord(word)
weight = getWordWeight(occurrence, length)

# find all associations from store for given word id


associations = findAssociations(wordId)

# create a tuple for each association if found


# and then store tuple in result list
for association in associations:
print("association: ")
print(association)
saveResults(association, weight)

# get Id of message for response


messageId = getResult()

return messageId
def learn(userMessage, botMessage):
# store user message
messageId = storeMessage(userMessage)

# get details of words from bot message


wordsWithOccurrences = getWordsWithOccurrences(botMessage)
length = getWordsLength(wordsWithOccurrences)

54
# store words from bot message and link each with user message
for word, occurrence in wordsWithOccurrences:
wordId = storeWord(word)
weight = getWordWeight(occurrence, length)
association = (wordId, messageId, weight) # tuple
storeAssociation(association)

# 1. learn from each dialogue in conversation


# 2. analyze user input to find a suitable reply
# from a growing list of messages created using user inputs
# 3. reply the message found in analysis

def process(userMessage, botMessage):


"""Process each dialogue in a conversation by analyzing messages and replying suitably"""
learn(userMessage, botMessage)
responseKey = analyze(userMessage)
return prepareResponse(responseKey)

mySqlStore.py
import mysql.connector as mySQL

dbQueries = {
"CREATETABLEMESSAGESTORAGE": "CREATE TABLE IF NOT EXISTS
messageStore(rowid int NOT NULL AUTO_INCREMENT PRIMARY KEY, message text, msgUsage
integer);",
"CREATETABLEWORDSTORAGE": "CREATE TABLE IF NOT EXISTS wordStore(rowid int
NOT NULL AUTO_INCREMENT PRIMARY KEY, word varchar(99))",
"CREATETABLEASSOCIATIONSTORAGE": "CREATE TABLE IF NOT EXISTS
associationStore(wordId int, messageId int, weight float)",
"CREATETABLERESULTSTORAGE": "CREATE TABLE IF NOT EXISTS
resultStore(messageId int, weight int)",
"INSERTINTOMESSAGESTORAGE": "INSERT INTO messageStore VALUES (NULL, ?, ?)",
"INSERTINTOWORDSTORAGE": "INSERT INTO wordStore VALUES (NULL, ?)",
"INSERTINTOASSOCIATIONSTORAGE": "INSERT INTO associationStore VALUES (?, ?, ?)",
"INSERTINTORESULTSTORAGE": "INSERT INTO resultStore VALUES (?, ?)",
"SELECTBYIDFROMMESSAGESTORAGE": "SELECT message, msgUsage FROM messageStore
WHERE rowid = ?",
"SELECTFROMMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE message
= ? LIMIT 1",
"SELECTFROMWORDSTORAGE": "SELECT rowid FROM wordStore WHERE word = ?
LIMIT 1",
"SELECTFROMRESULTSTORAGE": "SELECT messageId, SUM(weight) AS sumWeight
FROM resultStore GROUP BY messageId ORDER BY sumWeight DESC LIMIT 1",
"SELECTFROMASSOCIATIONSTORAGE": "SELECT wordId, messageId, weight FROM
55
associationStore where wordId = ?",
"UPDATEUSAGEMESSAGESTORAGE": "UPDATE messageStore SET msgUsage = msgUsage +
1 WHERE rowid = ?",
"GETMINIDMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE msgUsage
= (SELECT MIN(msgUsage) FROM messageStore) ORDER BY RAND() LIMIT 1",
"DROPRESULTSTORAGE": "DROP TABLE resultStore",
"DROPMESSAGESTORAGE": "DROP TABLE messageStore",
"DROPWORDSTORAGE": "DROP TABLE wordStore",
"DROPASSOCIATIONSTORAGE": "DROP TABLE associationStore"
}

def connect():
# print("connecting")
global connection
connection = mySQL.connect(host = "localhost", user = "root", passwd = "admin", database =
"botDB")
# global cursor
# cursor = connection.cursor()
create("messageStorage")
create("wordStorage")
create("associationStorage")

# def load():
# print("loading")

# def save():
# print("saving")

'''remove word store error ASAP'''

def getQuery(queryType, entityName):


return dbQueries[queryType + entityName.upper()]

def update(entityName, elementName, entityIndex):


# print("updating")
dbQueryType = "UPDATEUSAGE"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
56
cursor.execute(dbQuery, entityIndex)

def validateExists(entityName, key):


# print("validating")
isExist = False
dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, key)
row = cursor.fetchone()
if row:
isExist = True
return isExist

def retrieve(entityName, itemIndex):


dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
print("########### itemIndex: ")
print(itemIndex)
cursor.execute(dbQuery, itemIndex)
row = cursor.fetchone()
return row

def minId():
dbQueryType = "GETMINID"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor()
cursor.execute(dbQuery)
row = cursor.fetchone()
return row

def insert(entityName, item):


# print("inserting")
rowId = -1
parameter = item[0]
isUnique = False
57
if entityName == 'messageStorage':
parameter = item[0]
isUnique = True
elif entityName == 'wordStorage':
parameter = item
isUnique = True

row = None

# check if item already present in store


if isUnique == True:
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, [parameter])
row = cursor.fetchone()

if row:
rowId = row[0]
else:
# insert new record only if already not present
dbQueryType = "INSERTINTO"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
if entityName == 'messageStorage' or entityName == 'resultStorage':
cursor.execute(dbQuery, item)
elif entityName == 'wordStorage':
cursor.execute(dbQuery, [item])
elif entityName == 'associationStorage':
cursor.execute(dbQuery, item)
rowId = cursor.lastrowid

return rowId

def create(entityName):
dbQueryType = "CREATETABLE"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor()
cursor.execute(dbQuery)

58
def findAll(entityName, elementName, elementValue):
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, (elementValue,))
rows = cursor.fetchall()
return rows

def getResult():
rowId = -1
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor = connection.cursor()
cursor.execute(dbQuery)
row = cursor.fetchone()
if row:
rowId = row[0]
dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor.execute(dbQuery)
return rowId

def reset():
cursor = connection.cursor()
dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "MESSAGESTORAGE")
cursor.execute(dbQuery)

dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "WORDSTORAGE")
cursor.execute(dbQuery)

dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "ASSOCIATIONSTORAGE")
cursor.execute(dbQuery)

59
sqliteStore.py
import sqlite3
dbName = 'botStorage.sqlite'

dbQueries = {
"CREATETABLEMESSAGESTORAGE": "CREATE TABLE IF NOT EXISTS
messageStore(message varchar, usage int)",
"CREATETABLEWORDSTORAGE": "CREATE TABLE IF NOT EXISTS wordStore(word
varchar(99));",
"CREATETABLEASSOCIATIONSTORAGE": "CREATE TABLE IF NOT EXISTS
associationStore(wordId int, messageId int, weight float)",
"CREATETABLERESULTSTORAGE": "CREATE TABLE IF NOT EXISTS
resultStore(messageId int, weight int)",
"INSERTINTOMESSAGESTORAGE": "INSERT INTO messageStore VALUES (?, ?)",
"INSERTINTOWORDSTORAGE": "INSERT INTO wordStore VALUES (?)",
"INSERTINTOASSOCIATIONSTORAGE": "INSERT INTO associationStore VALUES (?, ?, ?)",
"INSERTINTORESULTSTORAGE": "INSERT INTO resultStore VALUES (?, ?)",
"SELECTBYIDFROMMESSAGESTORAGE": "SELECT message FROM messageStore WHERE
rowid = ?",
"SELECTFROMMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE message
= ?",
"SELECTFROMWORDSTORAGE": "SELECT rowid FROM wordStore WHERE word = ?",
"SELECTFROMRESULTSTORAGE": "SELECT rowid, SUM(weight) AS sumWeight FROM
resultStore GROUP BY rowid ORDER BY sumWeight DESC LIMIT 1",
"SELECTFROMASSOCIATIONSTORAGE": "SELECT wordId, messageId, weight FROM
associationStore where wordId = ?",
"UPDATEUSAGEMESSAGESTORAGE": "UPDATE messageStore SET usage = usage + 1
WHERE rowid = ?",
"GETMINIDMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE usage =
(SELECT MIN(usage) FROM messageStore) ORDER BY RANDOM() LIMIT 1",
"DROPRESULTSTORAGE": "DROP TABLE resultStore"
}

def connect():
# print("connecting")
connection = sqlite3.connect(dbName)
global cursor
cursor = connection.cursor()
create("messageStorage")
create("wordStorage")
create("associationStorage")

60
# def load():
# print("loading")

# def save():
# print("saving")

def getQuery(queryType, entityName):


return dbQueries[queryType + entityName.upper()]

def update(entityName, elementName, entityIndex):


# print("updating")
dbQueryType = "UPDATEUSAGE"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, entityIndex)

def validateExists(entityName, key):


# print("validating")
isExist = False
dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, (key,))
row = cursor.fetchone()
if row:
isExist = True
return isExist

def retrieve(entityName, itemIndex):


dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, itemIndex)
row = cursor.fetchone()
return row

def minId():
dbQueryType = "GETMINID"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery)
row = cursor.fetchone()
61
return row

def insert(entityName, item):


# print("inserting")
rowId = -1
parameter = item[0]
isUnique = False

if entityName = = 'messageStorage':
parameter = item[0]
isUnique = True
elif entityName = = 'wordStorage':
parameter = item
isUnique = True

row = None

# check if item already present in store


if isUnique = = True:
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, [parameter])
row = cursor.fetchone()

if row:
rowId = row[0]
else:
# insert new record only if already not present
dbQueryType = "INSERTINTO"
dbQuery = getQuery(dbQueryType, entityName)
if entityName = = 'messageStorage' or entityName = = 'resultStorage':
cursor.execute(dbQuery, item)
elif entityName = = 'wordStorage':
cursor.execute(dbQuery, [item[0]])
elif entityName = = 'associationStorage':
cursor.execute(dbQuery, item)
rowId = cursor.lastrowid

return rowId

def create(entityName):
dbQueryType = "CREATETABLE"
62
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery)

def findAll(entityName, elementName, elementValue):


dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, (elementValue,))
rows = cursor.fetchall()
return rows

def getResult():
rowId = -1
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor.execute(dbQuery)
row = cursor.fetchone()
if row:
rowId = row[0]
dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor.execute(dbQuery)
return rowId

tkCanvas.py
import tkinter as tk
from tkinter import *
from messageProcessor import process, start
from PIL import Image, ImageTk

row = -1
botMessage = "Hello"
messageImageTk = None

# def reset():
# querry = Toplevel(window)
# resetBot = Button(querry)
# resetBot.pack()

def loadImages():
messageImage = Image.open('test.png')
global messageImageTk
messageImageTk = ImageTk.PhotoImage(messageImage)
63
def populate(frame, message, sender):
'''Put in some fake data'''
global row
row = row + 1
if sender == "BOT":
owner = "B"
colNum = 0
textPlace="nw"
elif sender == "USER":
owner = "U"
colNum = 2
textPlace="ne"

tk.Label(frame, text = owner, width=2, borderwidth= 3,bg="#00e6b8", relief="sunken",font =("times


new roman",10)).grid(row=row, column=colNum)
tk.Label(frame, text = message, width=41, background="#bfbfbf", borderwidth=1,wraplength=400,
anchor= textPlace, font =("verdana",10)).grid(row=row,column=1)

def onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))

def getUserMessage():
userMessage = input.get().strip()
global botMessage
if (userMessage != ""):
botMessage = process(userMessage, botMessage)
input.delete(0, len(userMessage))
populate(frame, userMessage, "USER")
populate(frame, botMessage, "BOT")

start()

window = tk.Tk()
window.title("Smart Buddy")
window.resizable(0, 0)

canvas = tk.Canvas(window, background="#262626", borderwidth=1, relief="solid")


frame = tk.Frame(canvas, background="#262626",)
64
vsb = tk.Scrollbar(window, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side='top')
canvas.create_window((5,5), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

# will create a reset button if bot misbehaves

# menubar = Menu(window)
# filemenu = Menu(menubar, tearoff=0)
# filemenu.add_command(label="Reset_Bot", command=reset)

# loadImages()

populate(frame, botMessage, "BOT")

frame2 = tk.Frame(window, bg="#262626")


input = tk.Entry(frame2 , bg = "#00e6b8",width=47, relief='sunken',font = ("verdana", 8))
input.pack(side="left",fill="both")

submitButton = tk.Button(frame2, text = "Enter", bg = '#ffffff', activebackground = "#999999",bd=3 ,


command = getUserMessage, font = ("verdana", 8))

submitButton.pack(side="right", fill="both")
frame2.pack()

window.mainloop()

65
Output

The output design of the Smart Buddy chatbot, as shown in the screenshot, presents a simple, user-friendly interface
that facilitates seamless interactions between the user and the bot. The layout is structured as a conversational
interface, where the chatbot and user exchanges are displayed in a scrolling, vertically-aligned window. Each
message is clearly labeled, with "B" indicating the bot’s responses and "U" representing the user’s inputs, allowing
users to distinguish between the source of each message easily.

The visual design uses a gray background for the chat bubbles with alternating alignment, creating a clear division
between bot and user messages. Each message is encased in a bordered box to enhance readability. The user’s
typing area at the bottom of the interface includes a clear input field with an “Enter” button to submit text,
promoting ease of use.

The interface’s minimalist style is appropriate for a chatbot in its initial phase, focusing on core functionality
without complex design elements. This basic structure allows for further customization and scalability if additional
features or a more sophisticated UI are needed in the future. The design, although simple, is intuitive, making it
ideal for users to engage with Smart Buddy effectively while maintaining a clean and organized look that is easy to
follow. This output design meets the project requirements by providing a visually accessible, easily navigable, and
functionally sufficient platform for user interactions with the chatbot.

66
Process Involved The operation of the Smart Buddy chatbot is straightforward, involving a series of steps that
enable the bot to process user input, retrieve relevant responses, and store data for future learning. This process
ensures that the chatbot provides accurate and contextually appropriate replies, enhancing its effectiveness over
time. The steps below outline the process in detail:

1. User Input of Data:


• The interaction begins when the user enters text into the chatbot’s input field. This could
range from a simple greeting to a question or a specific query.
• The input is then captured by the bot’s interface and sent to the processing unit for analysis.
This step initiates the conversation and allows the bot to begin parsing the content of the
user’s input.

2. Bot Processes the Text and Stores the Data:


• Upon receiving the user input, the bot's processing module analyzes the text to extract key
information. This involves breaking down the sentence into individual words or phrases,
identifying keywords, and determining the intent behind the user’s message.
• The bot may use a basic Natural Language Processing (NLP) approach, like tokenization or
part-of-speech tagging, to aid in this analysis. The processed data is temporarily stored in
the bot's memory, allowing it to refer to this information during the conversation. This step
sets the foundation for understanding the input.

3. Bot Applies Word Occurrence Formula and Links with Pre-stored Data:
• After identifying keywords in the user input, the bot applies a word occurrence formula to
calculate the frequency of certain words and concepts within the text. This formula helps
the chatbot determine the most significant words, which it can then match with pre-defined
keywords or phrases stored in the database.
• By linking these words to corresponding responses in the database, the bot builds a
connection between the user’s current input and the most relevant stored replies. This
process ensures that the chatbot can produce contextually relevant responses based on word
frequency and occurrence patterns.

4. Bot Finds Suitable Replies:


• Once the bot has identified a match between the user’s input and pre-stored data, it
retrieves the most appropriate reply from its response database.
• The chatbot’s database contains a variety of predefined responses associated with different
keywords or phrases, allowing it to respond accurately across a range of common topics.
This retrieval process is designed to be efficient, providing an instant reply to maintain a
smooth conversational flow.
• The chosen response is then displayed in the chat window, completing the interaction cycle
for that specific input.

67
5. Bot Stores Conversation for Future References and Self-Training:
• After responding to the user, the bot logs the entire conversation, including both the user
inputs and its replies, in the database.
• This stored data is valuable for several reasons. First, it enables developers to analyze user
interactions to improve response accuracy and identify new keywords or topics that users
frequently ask about.
• Second, this data contributes to the bot’s adaptive learning mechanism, allowing it to
“train” itself over time by refining its keyword-response links based on previous
interactions.
• By continuously storing and referencing past conversations, the chatbot becomes more
adept at responding to queries and handling various conversation patterns, enhancing its
long-term performance.

This process flow demonstrates how the Smart Buddy chatbot operates in a structured manner to capture,
analyze, respond to, and learn from user interactions. Each step is integral to creating a chatbot that is
responsive, accurate, and adaptive, making it an effective tool for basic conversational tasks. This
structured approach forms a solid foundation for further enhancements, such as incorporating advanced
NLP algorithms, which could improve the bot’s ability to understand context and provide even more
nuanced responses.

Methodology Used Testing for Smart Buddy Chatbot:


The listed below testing methodology covers a comprehensive range of activities to validate and refine the
functionality, performance, and user experience of Smart Buddy. Adopting a systematic testing approach
ensures that the chatbot meets its objectives and delivers a reliable and user-friendly experience.

1. Unit Testing
Objective: Validate the functionality of individual components and ensure they operate as
intended.
Key Points:
• Error Detection in Core Functions: Examine functions individually for handling
unexpected or incorrect input gracefully, ensuring robustness.
• Boundary Testing: Test how components handle edge cases, such as extremely long or
complex user inputs, to ensure stability.
• Code Optimization Checks: Ensure that critical functions, like word counting and
database queries, are optimized for speed and efficiency.

2. Integration Testing
Objective: Assess the interaction between different components to confirm they work
harmoniously.

68
Key Points:
• Data Consistency Verification: Check for consistency in data exchanges between
components, ensuring that data is transferred and stored correctly across all modules.
• System Recovery Testing: Simulate integration failures (e.g., database disconnection) and
test the chatbot’s recovery mechanisms, ensuring continuity of service.
• Interface Responsiveness Testing: Measure the time it takes for responses to display after
processing, to ensure seamless communication between the backend and user interface.

3. Functional Testing
Objective: Validate the chatbot’s performance in real-world usage scenarios and user interactions.
Key Points:
• Fallback Mechanism Testing: Test the chatbot’s fallback responses when it cannot
understand or find an answer to a user’s question, ensuring it handles such situations
gracefully.
• Time-Sensitive Responses Testing: For queries where timeliness matters (e.g., time-
sensitive data or repetitive questions), test the chatbot’s ability to provide accurate answers
promptly.
• Cross-Platform Compatibility: If applicable, test the chatbot on various platforms or
devices (e.g., desktop, mobile) to ensure consistent functionality across environments.

4. User Training Evaluation


Objective: Assess the chatbot’s capacity for learning and improving over time based on user
interactions.
Key Points:
• Long-Term Adaptation Testing: Track the chatbot's improvements over extended testing
sessions to confirm that it retains and effectively applies learned responses.
• Behavioral Consistency Check: Ensure that the chatbot maintains consistent behavior and
doesn't drift into unrelated or unintended topics as a result of frequent interactions.
• User Feedback Processing: Test the chatbot’s ability to interpret and respond to user
feedback (if a feedback loop exists), confirming that such data is effectively utilized for
performance enhancement.

Additional Key Testing-


• Regression Testing for Updates: Test the entire chatbot system after any updates or code
modifications to confirm that new changes do not interfere with established functions.

• Data Retention and Privacy Compliance: Test to ensure user data is retained or deleted as per
data privacy requirements, confirming compliance with data protection standards.

69
Cheat Sheet
botTk.py
from messageProcessor import process, start, end

# start conversation with greeting message


# in an endless loop till user enters empty line
def run():
start()
botMessage = 'hello'
while True:
print("B: " + botMessage)
userMessage = str(input("U: "))
if userMessage == "":
end()
break
botMessage = process(userMessage, botMessage)

#run()

listStore.py
# # list of messages where each message is a tuple of text and usage
# messageStore = []

# # list of words where each word is a text


# wordStore = []

# # list of associations where each association is a tuple (wordId - messageId - weight)


# associationStore = []

# # list of results where each result is a tuple (messageId - weight)


# resultStore = []

# dictionary where key is name of storage and value is a list


botStorage = {}

def connect():
create("messageStorage")
create("wordStorage")
create("associationStorage")

def save():
# save storage to file
print("saving")

def load():
# load storage from file

70
print("loading")

def update(entityName, elementName, entityIndex):


entity = getEntity(entityName)
elementIndex = getElementIndex(entityName, elementName)

# find record from list for given index


tempTuple = entity[entityIndex]

# convert tuple list into dictionary


tempDict = {0: tempTuple[0], 1: tempTuple[1]}

tempDict[elementIndex] = tempDict.get(elementIndex, 0) + 1

# convert dictionary into tuple list


tempTuple2 = (tempDict[0], tempDict[1])

# update list record for given index


entity[entityIndex] = tempTuple2

def minId():
"""return least used message id from messageTupleStore"""
minId = -1

entity = getEntity("messageStorage")

length = len(entity)

if length = = 0:
return minId
elif length = = 1:
return 0
else:
minId = 0
minUsage = entity[0][1]

for id in range (1, length):


messageTuple = entity[id]
usage = messageTuple[1]
if usage < minUsage:
minUsage = usage
minId = id

return minId

def validateExists(entityName, key):


71
entity = getEntity(entityName)
return key > = 0 and key < len(entity)

def group(entityName, elementName):


tempDict = {}
tempList = []
tupleList = getEntity(entityName)
index = getElementIndex(entityName, elementName)
for sentence in tupleList:
sentenceId = sentence[index]
tempDict[sentenceId] = tempDict.get(sentenceId, 0) + sentence[1]
for key in tempDict:
tempTuple = (key, tempDict[key])
tempList.append(tempTuple)
return tempList

def sort(entityName, elementName):


index = getElementIndex(entityName, elementName)
tupleList = getEntity(entityName)
tupleList.sort(key = lambda x: x[index] , reverse = True)
return tupleList

def create(entityName):
botStorage[entityName] = []

def getResult():
sentenceId = -1
if validateExists("resultStorage", 0):
group("resultStorage", "messageId")
sort("resultStorage", "weight")
resultTuple = retrieve("resultStorage", 0)
sentenceId = resultTuple[0]
return sentenceId

def getEntity(entityName):
entity = botStorage[entityName]
return entity

def insert(entityName, item):


entity = getEntity(entityName)

# store item in list if not already stored


72
if item not in entity:
entity.append(item)

# return ID of item in list


return entity.index(item)

def retrieve(entityName, itemIndex):


entity = getEntity(entityName)
return entity[itemIndex]

def retrieveElement(tupleEntity, entityName, elementName):


index = getElementIndex(entityName, elementName)
return tupleEntity[index]

def getElementIndex(entityName, elementName):


index = -1

if entityName = = 'associationStorage':
if elementName = = 'wordId':
index = 0
elif elementName = = 'messageId':
index = 1
elif elementName = = 'weight':
index = 2
elif entityName = = 'wordStorage':
if elementName = = 'wordId':
index = 0
elif entityName = = 'messageStorage':
if elementName = = 'messageId':
index = 0
elif elementName = = 'usage':
index = 1
elif entityName = = 'resultStorage':
if elementName = = 'messageId':
index = 0
elif elementName = = 'weight':
index = 1
return index

def findAll(entityName, elementName, elementValue):


entity = getEntity(entityName)
entryList = []
index = getElementIndex(entityName, elementName)
for entry in entity:
if entry[index] = = elementValue:
entryList.append(entry)
73
return entryList

messageProcessor.py
import random
from math import sqrt
#import listStore as store
#import sqliteStore as store
import mySqlStore as store

def start():
store.connect()
# store.load()

def end():
# store.save()
print()

def updateUsageCountOfMessage(responseKey):
return store.update("messageStorage", "usage", responseKey)

def validKey(responseKey):
return store.validateExists("messageStorage", responseKey)

def getMessage(responseKey):
messageTuple = store.retrieve("messageStorage", responseKey)
return messageTuple[0]

def getLeastUsedMessageId():
return store.minId()

def getResult():
sentenceId = -1 # wrong value as default
sentenceId = store.getResult()
print("!!!!!!!!sentenceId: ")
print(sentenceId)
return (sentenceId,)

def saveResults(association, weight):


# create result tuple (messageId, weight)
messageId = association[1]
associationWeight = association[2]
messageTuple = store.retrieve("messageStorage", (messageId,))
usage = messageTuple[1]
resultWeight = associationWeight * weight / (4 + usage)
resultTuple = (messageId, resultWeight)

store.insert("resultStorage", resultTuple)

74
def createResultStore():
# resultStorage list is created in starting
# empty list to use again
store.create("resultStorage")

def findAssociations(wordId):
associations = store.findAll("associationStorage", "wordId", wordId)
return associations

def storeAssociation(association):
return store.insert("associationStorage", association)

def storeMessage(message):
messageTuple = (message, 1) # usage default value is 1
messageId = store.insert("messageStorage", messageTuple)
return messageId

def storeWord(word):
return store.insert("wordStorage", word)

def getWordWeight(occurrence, length):


weight = sqrt(occurrence / float(length))
return weight

def getWordsLength(wordsWithOccurrences):
length = sum([occurrence * len(word) for word, occurrence in wordsWithOccurrences])
return length

def getWordsWithOccurrences(sentence):
# get list of words in a sentence
words = sentence.split()
# TODO: handle punctuation

# create word dictionary (unique lower case word, occurrence)


wordDict = {}
for word in words:
word = word.lower()
wordDict[word] = wordDict.get(word, 0) + 1

# convert dictionary to tuple list


wordsWithOccurrences = []
for key in wordDict:
wordTuple = (key, wordDict[key])
wordsWithOccurrences.append(wordTuple)

75
return wordsWithOccurrences

def prepareResponse(responseKey):
# if message with given message Id is not found
# then get message Id of least used message
if validKey(responseKey) == False:
print("%%%%%%%%%% rkey1: ")
print(responseKey)
responseKey = getLeastUsedMessageId()

# get message text


print("%%%%%%%%%% rkey2: ")
print(responseKey)
reply = getMessage(responseKey)

# update used count of message by +1


updateUsageCountOfMessage(responseKey)

# return message text


return reply

def analyze(userMessage):
# get details of words from user message
wordsWithOccurrences = getWordsWithOccurrences(userMessage)
length = getWordsLength(wordsWithOccurrences)

# create empty list of result sets (message Id, weightage)


createResultStore() # each result is a tuple (messageId - weight)

# save list of result sets from existing linking


for word, occurrence in wordsWithOccurrences:
# get word details
wordId = storeWord(word)
weight = getWordWeight(occurrence, length)

# find all associations from store for given word id


associations = findAssociations(wordId)

# create a tuple for each association if found


# and then store tuple in result list
for association in associations:
print("association: ")
print(association)
saveResults(association, weight)

# get Id of message for response


messageId = getResult()
76
return messageId
def learn(userMessage, botMessage):
# store user message
messageId = storeMessage(userMessage)

# get details of words from bot message


wordsWithOccurrences = getWordsWithOccurrences(botMessage)
length = getWordsLength(wordsWithOccurrences)

# store words from bot message and link each with user message
for word, occurrence in wordsWithOccurrences:
wordId = storeWord(word)
weight = getWordWeight(occurrence, length)
association = (wordId, messageId, weight) # tuple
storeAssociation(association)

# 1. learn from each dialogue in conversation


# 2. analyze user input to find a suitable reply
# from a growing list of messages created using user inputs
# 3. reply the message found in analysis

def process(userMessage, botMessage):


"""Process each dialogue in a conversation by analyzing messages and replying suitably"""
learn(userMessage, botMessage)
responseKey = analyze(userMessage)
return prepareResponse(responseKey)

mySqlStore.py
import mysql.connector as mySQL

dbQueries = {
"CREATETABLEMESSAGESTORAGE": "CREATE TABLE IF NOT EXISTS
messageStore(rowid int NOT NULL AUTO_INCREMENT PRIMARY KEY, message text, msgUsage
integer);",
"CREATETABLEWORDSTORAGE": "CREATE TABLE IF NOT EXISTS wordStore(rowid int
NOT NULL AUTO_INCREMENT PRIMARY KEY, word varchar(99))",
"CREATETABLEASSOCIATIONSTORAGE": "CREATE TABLE IF NOT EXISTS
associationStore(wordId int, messageId int, weight float)",
"CREATETABLERESULTSTORAGE": "CREATE TABLE IF NOT EXISTS
resultStore(messageId int, weight int)",
"INSERTINTOMESSAGESTORAGE": "INSERT INTO messageStore VALUES (NULL, ?, ?)",
"INSERTINTOWORDSTORAGE": "INSERT INTO wordStore VALUES (NULL, ?)",
"INSERTINTOASSOCIATIONSTORAGE": "INSERT INTO associationStore VALUES (?, ?, ?)",
77
"INSERTINTORESULTSTORAGE": "INSERT INTO resultStore VALUES (?, ?)",
"SELECTBYIDFROMMESSAGESTORAGE": "SELECT message, msgUsage FROM messageStore
WHERE rowid = ?",
"SELECTFROMMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE message
= ? LIMIT 1",
"SELECTFROMWORDSTORAGE": "SELECT rowid FROM wordStore WHERE word = ?
LIMIT 1",
"SELECTFROMRESULTSTORAGE": "SELECT messageId, SUM(weight) AS sumWeight
FROM resultStore GROUP BY messageId ORDER BY sumWeight DESC LIMIT 1",
"SELECTFROMASSOCIATIONSTORAGE": "SELECT wordId, messageId, weight FROM
associationStore where wordId = ?",
"UPDATEUSAGEMESSAGESTORAGE": "UPDATE messageStore SET msgUsage = msgUsage +
1 WHERE rowid = ?",
"GETMINIDMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE msgUsage
= (SELECT MIN(msgUsage) FROM messageStore) ORDER BY RAND() LIMIT 1",
"DROPRESULTSTORAGE": "DROP TABLE resultStore",
"DROPMESSAGESTORAGE": "DROP TABLE messageStore",
"DROPWORDSTORAGE": "DROP TABLE wordStore",
"DROPASSOCIATIONSTORAGE": "DROP TABLE associationStore"
}

def connect():
# print("connecting")
global connection
connection = mySQL.connect(host = "localhost", user = "root", passwd = "admin", database =
"botDB")
# global cursor
# cursor = connection.cursor()
create("messageStorage")
create("wordStorage")
create("associationStorage")

# def load():
# print("loading")

# def save():
# print("saving")

'''remove word store error ASAP'''

78
def getQuery(queryType, entityName):
return dbQueries[queryType + entityName.upper()]

def update(entityName, elementName, entityIndex):


# print("updating")
dbQueryType = "UPDATEUSAGE"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, entityIndex)

def validateExists(entityName, key):


# print("validating")
isExist = False
dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, key)
row = cursor.fetchone()
if row:
isExist = True
return isExist

def retrieve(entityName, itemIndex):


dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
print("########### itemIndex: ")
print(itemIndex)
cursor.execute(dbQuery, itemIndex)
row = cursor.fetchone()
return row

def minId():
dbQueryType = "GETMINID"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor()
79
cursor.execute(dbQuery)
row = cursor.fetchone()
return row

def insert(entityName, item):


# print("inserting")
rowId = -1
parameter = item[0]
isUnique = False

if entityName == 'messageStorage':
parameter = item[0]
isUnique = True
elif entityName == 'wordStorage':
parameter = item
isUnique = True

row = None

# check if item already present in store


if isUnique == True:
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, [parameter])
row = cursor.fetchone()

if row:
rowId = row[0]
else:
# insert new record only if already not present
dbQueryType = "INSERTINTO"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
if entityName == 'messageStorage' or entityName == 'resultStorage':
cursor.execute(dbQuery, item)
elif entityName == 'wordStorage':
cursor.execute(dbQuery, [item])
elif entityName == 'associationStorage':
cursor.execute(dbQuery, item)
rowId = cursor.lastrowid
80
return rowId

def create(entityName):
dbQueryType = "CREATETABLE"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor()
cursor.execute(dbQuery)

def findAll(entityName, elementName, elementValue):


dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor = connection.cursor(prepared = True)
cursor.execute(dbQuery, (elementValue,))
rows = cursor.fetchall()
return rows

def getResult():
rowId = -1
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor = connection.cursor()
cursor.execute(dbQuery)
row = cursor.fetchone()
if row:
rowId = row[0]
dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor.execute(dbQuery)
return rowId

def reset():
cursor = connection.cursor()
dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "MESSAGESTORAGE")
cursor.execute(dbQuery)

dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "WORDSTORAGE")
81
cursor.execute(dbQuery)

dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "ASSOCIATIONSTORAGE")
cursor.execute(dbQuery)

sqliteStore.py
import sqlite3
dbName = 'botStorage.sqlite'

dbQueries = {
"CREATETABLEMESSAGESTORAGE": "CREATE TABLE IF NOT EXISTS
messageStore(message varchar, usage int)",
"CREATETABLEWORDSTORAGE": "CREATE TABLE IF NOT EXISTS wordStore(word
varchar(99));",
"CREATETABLEASSOCIATIONSTORAGE": "CREATE TABLE IF NOT EXISTS
associationStore(wordId int, messageId int, weight float)",
"CREATETABLERESULTSTORAGE": "CREATE TABLE IF NOT EXISTS
resultStore(messageId int, weight int)",
"INSERTINTOMESSAGESTORAGE": "INSERT INTO messageStore VALUES (?, ?)",
"INSERTINTOWORDSTORAGE": "INSERT INTO wordStore VALUES (?)",
"INSERTINTOASSOCIATIONSTORAGE": "INSERT INTO associationStore VALUES (?, ?, ?)",
"INSERTINTORESULTSTORAGE": "INSERT INTO resultStore VALUES (?, ?)",
"SELECTBYIDFROMMESSAGESTORAGE": "SELECT message FROM messageStore WHERE
rowid = ?",
"SELECTFROMMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE message
= ?",
"SELECTFROMWORDSTORAGE": "SELECT rowid FROM wordStore WHERE word = ?",
"SELECTFROMRESULTSTORAGE": "SELECT rowid, SUM(weight) AS sumWeight FROM
resultStore GROUP BY rowid ORDER BY sumWeight DESC LIMIT 1",
"SELECTFROMASSOCIATIONSTORAGE": "SELECT wordId, messageId, weight FROM
associationStore where wordId = ?",
"UPDATEUSAGEMESSAGESTORAGE": "UPDATE messageStore SET usage = usage + 1
WHERE rowid = ?",
"GETMINIDMESSAGESTORAGE": "SELECT rowid FROM messageStore WHERE usage =
(SELECT MIN(usage) FROM messageStore) ORDER BY RANDOM() LIMIT 1",
"DROPRESULTSTORAGE": "DROP TABLE resultStore"
}
82
def connect():
# print("connecting")
connection = sqlite3.connect(dbName)
global cursor
cursor = connection.cursor()
create("messageStorage")
create("wordStorage")
create("associationStorage")

# def load():
# print("loading")

# def save():
# print("saving")

def getQuery(queryType, entityName):


return dbQueries[queryType + entityName.upper()]

def update(entityName, elementName, entityIndex):


# print("updating")
dbQueryType = "UPDATEUSAGE"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, entityIndex)

def validateExists(entityName, key):


# print("validating")
isExist = False
dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, (key,))
row = cursor.fetchone()
if row:
isExist = True
return isExist

def retrieve(entityName, itemIndex):


dbQueryType = "SELECTBYIDFROM"
dbQuery = getQuery(dbQueryType, entityName)
83
cursor.execute(dbQuery, itemIndex)
row = cursor.fetchone()
return row

def minId():
dbQueryType = "GETMINID"
entityName = "MESSAGESTORAGE"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery)
row = cursor.fetchone()
return row

def insert(entityName, item):


# print("inserting")
rowId = -1
parameter = item[0]
isUnique = False

if entityName = = 'messageStorage':
parameter = item[0]
isUnique = True
elif entityName = = 'wordStorage':
parameter = item
isUnique = True

row = None

# check if item already present in store


if isUnique = = True:
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, [parameter])
row = cursor.fetchone()

if row:
rowId = row[0]
else:
# insert new record only if already not present
dbQueryType = "INSERTINTO"
dbQuery = getQuery(dbQueryType, entityName)
if entityName = = 'messageStorage' or entityName = = 'resultStorage':
cursor.execute(dbQuery, item)
84
elif entityName = = 'wordStorage':
cursor.execute(dbQuery, [item[0]])
elif entityName = = 'associationStorage':
cursor.execute(dbQuery, item)
rowId = cursor.lastrowid

return rowId

def create(entityName):
dbQueryType = "CREATETABLE"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery)

def findAll(entityName, elementName, elementValue):


dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, entityName)
cursor.execute(dbQuery, (elementValue,))
rows = cursor.fetchall()
return rows

def getResult():
rowId = -1
dbQueryType = "SELECTFROM"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor.execute(dbQuery)
row = cursor.fetchone()
if row:
rowId = row[0]
dbQueryType = "DROP"
dbQuery = getQuery(dbQueryType, "RESULTSTORAGE")
cursor.execute(dbQuery)
return rowId

tkCanvas.py
import tkinter as tk
from tkinter import *
from messageProcessor import process, start
from PIL import Image, ImageTk

row = -1
botMessage = "Hello"
messageImageTk = None
85
# def reset():
# querry = Toplevel(window)
# resetBot = Button(querry)
# resetBot.pack()

def loadImages():
messageImage = Image.open('test.png')
global messageImageTk
messageImageTk = ImageTk.PhotoImage(messageImage)

def populate(frame, message, sender):


'''Put in some fake data'''
global row
row = row + 1
if sender == "BOT":
owner = "B"
colNum = 0
textPlace="nw"
elif sender == "USER":
owner = "U"
colNum = 2
textPlace="ne"

tk.Label(frame, text = owner, width=2, borderwidth= 3,bg="#00e6b8", relief="sunken",font =("times


new roman",10)).grid(row=row, column=colNum)
tk.Label(frame, text = message, width=41, background="#bfbfbf", borderwidth=1,wraplength=400,
anchor= textPlace, font =("verdana",10)).grid(row=row,column=1)

def onFrameConfigure(canvas):
'''Reset the scroll region to encompass the inner frame'''
canvas.configure(scrollregion=canvas.bbox("all"))

def getUserMessage():
userMessage = input.get().strip()
global botMessage
if (userMessage != ""):
botMessage = process(userMessage, botMessage)
input.delete(0, len(userMessage))
populate(frame, userMessage, "USER")
populate(frame, botMessage, "BOT")
86
start()

window = tk.Tk()
window.title("Smart Buddy")
window.resizable(0, 0)

canvas = tk.Canvas(window, background="#262626", borderwidth=1, relief="solid")


frame = tk.Frame(canvas, background="#262626",)
vsb = tk.Scrollbar(window, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side='top')
canvas.create_window((5,5), window=frame, anchor="nw")
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

# will create a reset button if bot misbehaves

# menubar = Menu(window)
# filemenu = Menu(menubar, tearoff=0)
# filemenu.add_command(label="Reset_Bot", command=reset)

# loadImages()

populate(frame, botMessage, "BOT")

frame2 = tk.Frame(window, bg="#262626")


input = tk.Entry(frame2 , bg = "#00e6b8",width=47, relief='sunken',font = ("verdana", 8))
input.pack(side="left",fill="both")

submitButton = tk.Button(frame2, text = "Enter", bg = '#ffffff', activebackground = "#999999",bd=3 ,


command = getUserMessage, font = ("verdana", 8))

submitButton.pack(side="right", fill="both")
frame2.pack()

window.mainloop()

87
Chapter 07: Coding and Screenshots of the project

88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
Output

105
Chapter 08: Conclusion and Future Scope
Conclusion:

In conclusion, the development of Smart Buddy, a chatbot built exclusively in Python with a DBMS-
backed architecture, marks a significant achievement in the realm of conversational agents. The unique
approach of utilizing a word occurrence formula, coupled with an adaptive learning mechanism,
empowers Smart Buddy to generate contextually suitable responses based on user queries.

The project successfully achieved its primary objectives, including the implementation of the word
occurrence algorithm, response linking mechanism, and integration with a Database Management System
(DBMS) for efficient data handling. The Python-based interface provides users with a straightforward and
interactive platform for seamless communication with Smart Buddy.

Through rigorous testing, the chatbot demonstrated its ability to adapt and learn from user interactions,
providing relevant responses when trained effectively. The iterative development process and thorough
testing phases have contributed to the robustness and reliability of Smart Buddy.

While Smart Buddy currently excels in generating suitable replies based on the heaviest word occurrence
count, there is always room for improvement. User feedback and continuous testing will play a pivotal
role in refining the chatbot's responses, enhancing its adaptability, and addressing any identified areas for
improvement.

Future Scope:

The Smart Buddy chatbot project opens avenues for future enhancements and expansions, paving the way
for a more intelligent and versatile conversational agent. The following are potential areas for future
development:

1. Semantic Analysis:
Integrate advanced natural language processing (NLP) techniques for a deeper understanding of the
semantic context within user queries.

2. Advanced Learning Models:


Explore the incorporation of machine learning models to enhance the chatbot's learning capabilities and
improve response generation.

3. Multilingual Support:
Extend Smart Buddy's functionality to support multiple languages, making it accessible to a broader
audience.
106
4. Enhanced User Interface:
Upgrade the user interface with more interactive features, visual elements, and improved user guidance
to enhance the overall user experience.

5. Voice Recognition:
Implement voice recognition capabilities, allowing users to interact with Smart Buddy through spoken
queries.

6. Contextual Memory:
Develop a contextual memory system to enable the chatbot to retain information from previous
interactions, contributing to more coherent and context-aware responses.

7. Real-Time Updates:
Implement mechanisms for real-time updates, enabling Smart Buddy to adapt quickly to changing trends
and user preferences.

8. Enhanced Security Measures:


Strengthen security measures to safeguard user data and ensure the chatbot is resilient to emerging
security threats.

9. Integration with External APIs:


Explore the integration of external APIs to provide Smart Buddy with access to real-time information
and services.

10. User Analytics:


Implement user analytics to gather insights into user behavior, preferences, and areas for further
improvement.

11. Collaborative Learning:


Explore collaborative learning models, allowing Smart Buddy to learn not only from individual
interactions but also from collective user experiences.

As Smart Buddy continues to evolve, the incorporation of these future enhancements will contribute to its
evolution into a more sophisticated and user-centric chatbot.

The completion of the Smart Buddy project lays a strong foundation for ongoing research and
development in the field of natural language processing and conversational agents. The journey does not
end here, and the project sets the stage for an exciting future of innovation and improvement in the world
of intelligent chatbots.

107
Chapter 09: References

1. https://towardsdatascience.com/how-to-build-your-own-chatbot-using- deep-
learning-bb41f970e281

2. https://www.edenai.co/post/top-free-chatbot-tools-apis-and-open-source- models

3. https://doi.org/10.3390/info13010041

4. https://www.simplilearn.com/tutorials/python-tutorial/how-to-make-a- chatbot-in-
python

5. https://insights.daffodilsw.com/blog/the-history-and-evolution-of-chatbots

6. https://www.techtarget.com/searchcustomerexperience/infographic/The- evolution-of-
chatbots-and-generative-AI

7. https://www.researchgate.net/publication/336734161_A_Brief_History_of_ Chatbots

8. https://pynative.com/python-mysql-database-connection/

9. https://www.analyticsvidhya.com/blog/2023/02/how-to-build-a-chatbot-using-natural-
language-processing/

10. https://www.alltius.ai/glossary/complete-guide-to-build-your-ai-chatbot-with-nlp-in-
python

11. https://analyticsindiamag.com/ai-mysteries/top-python-libraries-for-chatbot-
development/

12. https://softwareengineering.stackexchange.com/questions/363928/build-a-realtime-
chat-app-which-stores-messages-in-a-mysql-database

108

You might also like