FINAL
FINAL
FINAL
BACHELOR OF TECHNOLOGY
IN
Submitted By
SCHOOL OF ENGINEERING
Department of Computer Science and Engineering
2024-2025
CERTIFICATE
External Examiner
DECLARATION
Date:
Signature:
ACKNOWLEDGEMENT
We express our sincere gratitude to our guide Mrs. P. MANASA, Assistant Professor,
in Computer Science and Engineering Department, NNRESGI, who motivated throughout
the period of the project and also for his valuable and intellectual suggestions apart from his
adequate guidance, constant encouragement right throughout our work.
We wish to record our deep sense of gratitude to our Project In-charge Mrs. INDRANI,
Assistant Professor, in Computer Science and Engineering Department, NNRESGI, for
giving her insight, advice provided during the review sessions and providing constant
monitoring from time to time in completing our project and for giving us this opportunity to
present the project work.
We wish to express our sincere thanks to Dr. G. Janardhana Raju, Dean School of
Engineering, NNRESGI, for providing the facilities for completion of the project.
By :
G. VARUN TEJA 227Z5A6603
G. SHRUTHI 217Z1A6623
B. ABHIRAM 217Z1A6607
ABSTRACT
Emotion recognition from text is a critical task in Natural Language Processing (NLP),
with wide-ranging applications in sentiment analysis, mental health monitoring, and human
computer interaction. It investigates the application of Bidirectional Encoder
Representations from Transformers (BERT) for emotion recognition. BERT’s ability to
capture contextual dependencies and nuances in text, through its bidirectional transformer
architecture, makes it ideal for this task. By fine-tuning pre-trained BERT models on an
emotion-labeled dataset, we classify emotions such as happiness, sadness, anger, surprise,
fear, and others. The model leverages BERT's pre-trained knowledge of language and its
capacity to understand complex syntactic and semantic patterns. Experimental evaluations
show that BERT achieves superior accuracy in detecting emotions compared to traditional
machine learning models like SVM and logistic regression, due to its deep contextual
embeddings. Furthermore, our approach improves generalization across varied emotional
expressions, demonstrating BERT's robustness in emotion aware NLP tasks. These results
suggest that BERT-based models hold significant promise for enhancing the accuracy and
effectiveness of emotion recognition systems. The main tool we have used in this project is
Streamlit. With the appearance of Streamlit, we can create powerful web packages that
leverage the power of NLP for emotion detection.
1. INTRODUCTION 1
1.1 Motivation 1
1.2 Problem Statement 1
1.3 Purpose 1
1.4 Scope 1
1.5 Project objective 2
1.6 Limitations 2
2. LITERATURE SURVEY 3
2.1 Existing System 4
2.3 Proposed System 5
3. SYSTEM ANALYSIS 6
3.1 Functional Requirements 6
3.2 Non Functional Requirements 7
3.3 Interface Requirements 9
4. SYSTEM DESIGN 10
4.1 DFD/ER/UML Diagrams 11
4.2 Modules 16
6. SYSTEM TESTING 29
6.1 Introduction for testing 29
6.2 Various Testcase Scenarios 31
7. CONCLUSION 33
7.1 Project Conclusion 33
7.2 Future Enhancement 33
8. REFERENCES 34
8.1 Paper References 34
8.2 Websites 35
8.3 Text Books 35
LIST OF FIGURES
1.1 MOTIVATION
In the last few years, research to understand human emotions based on text data has
become especially important in areas like customer service, mental health analysis, and
human-computer interactions. However, current emotion detection models do not
capture fine-grained emotions, sarcasm or more nuanced context. The main purpose of
this system is to increase the accuracy of emotion recognition in the text to provide more
insights into emotional states. In addition, the main motivation of the project is to utilize
BERT's contextual understandings to better emotions detected from text while in turn
improving the implications for sentiment analysis and feedback systems.
1.3 PURPOSE
This project will utilize BERT to enhance emotion detection in text, providing more
accurate information for applications such as sentiment analysis and customer service,
resulting in better decisions and interactions..
1.4 SCOPE
With the growing importance of emotion detection in areas like customer service,
mental health, and social media analysis, this project aims to apply BERT for more
accurate emotion recognition in real-world applications, improving human-computer
interactions and decision-making processes.
1
1.5 PROJECT OBJECTIVE
To improve emotion recognition from text using BERT, enhancing accuracy and
effectiveness in applications like customer service and sentiment analysis.
By leveraging BERT’s contextual understanding, the project aims to significantly
improve the accuracy and nuance of emotion detection, enhancing applications such as
sentiment analysis, customer service, and social media monitoring.
1.6 LIMITATIONS
Using BERT for emotion recognition requires significant computational resources
and memory. Its accuracy also depends on the quality and diversity of the training data, and
it may face challenges with domain-specific language or imbalanced datasets.
BERT's pre-training, based on static word embeddings, may also struggle with out-
of-vocabulary words or rapidly evolving language. Moreover, BERT is not inherently
designed for handling long sequences efficiently, as its architecture imposes quadratic
scaling in attention computation with input length, making it less effective for tasks
involving very long texts.
2
.
2. LITERATURE SURVEY
2.1 INTRODUCTION
Emotion recognition from text has become increasingly critical in today’s digital
landscape, impacting various fields such as customer service, mental health analysis,
and social media monitoring. Accurate emotion detection is essential for understanding
user sentiments, improving customer interactions, and analyzing social trends.
Traditional emotion detection models often struggle with capturing the complexity of
human emotions, including subtleties such as sarcasm, irony, and context-specific
sentiments. This limitation can lead to inaccuracies in sentiment analysis and reduced
effectiveness in applications relying on emotional insights.
3
By leveraging BERT’s advanced capabilities, organizations can gain deeper insights
into user emotions, leading to more informed decision-making and improved user
experiences.
Then, we conduct Data Pre-Processing (NLP) which involves the deletion of stop-
words and the use of stemmers to clean and prepare the data to be analyzed. This step
makes sure that the data is in the desired format for the subsequent processes.
The Data Training Process..., in this step, we divide data into testing and training
data. It divides the dataset in such a way that we will have a better method of evaluating
data and minimizing the issues that are arising due to the inaccuracy of the model.
The Model Prediction is then pursued where the model is used to predict emotions
based on new data which is used to train it.
Finally, the result of this is the misidentification of each of the emotions which are
whether the emotion is joy, fear, disgust, sadness, or guilt.
4
2.3 PROPOSED SYSTEM
For emotion detection, we have arrived at a systematic strategy that starts with the
acquisition of an Emotion Dataset.We Initially, it collect data for various emotions such
as joy, fear, disgust, sadness, and guilt are collected.
Then we do Data Pre-Processing (NLP), where the data is cleansed by the removal
of stop words and stemming to clean the data and prepare them for analysis. This process
facilitates the proper data structure for the following steps.
The Data Training Process is accomplished by dividing the dataset into training data
and test data. Modelling is performed using Deep Learning Algorithm (BERT’s) with
the training data. BERT (Bidirectional Encoder Representations from Transformers) is
not without being inklings of interest about its efficacy in comprehending and
interpreting natural language and as a result, it became a widely acknowledged tool that
helps us to understand and predict emotions more effectively.
5
3.SYSTEM ANALYSIS
1. Input Handling
Text Input: The system must accept text inputs (sentences, paragraphs, or documents)
from the user.
Language Support: It should handle multiple languages or specific ones based on the
training data.
Pre-trained BERT Model Loading: The system should load a pre-trained BERT
model (e.g., bert-base-uncased) or a fine-tuned version specific to emotion detection.
Tokenizer Integration: The system must utilize the appropriate tokenizer for the
BERT model to convert input text into token IDs.
GPU/CPU Processing: It should support both GPU and CPU processing, enabling
faster inference if a GPU is available.
3. Emotion Detection
Emotion Prediction: The system must classify the input text into a predefined set of
emotions (e.g., joy, sadness, anger, fear, disgust, surprise, etc.).
Confidence Score: For each prediction, the system must provide a confidence score or
6
probability for the detected emotion.
4. Batch Processing
Batch Input Support: The system should allow for batch processing of multiple texts
at once to increase processing efficiency.
Fine-Tuning BERT: The system should support fine-tuning the pre-trained BERT
model on a specific emotion-labeled dataset.
Training Data Handling: The system must allow loading and preprocessing of custom
datasets for model training and validation.
Visualization: The system should display the predicted emotion and its confidence
score visually, perhaps with bar charts or color-coded labels.
1. Performance
Latency: The system should have minimal response time (e.g., under 1 second per text
input) for emotion prediction, ensuring a smooth user experience, especially in real-time
applications.
7
Throughput: The system should be able to process a high number of text inputs per
second, particularly when handling batch processing or API calls.
Prediction Accuracy: The emotion detection model should maintain high accuracy,
with a desired F1-score, precision, or recall above a specified threshold (e.g., 85%+ on a
benchmark dataset).
Consistency: The system should produce consistent results for similar inputs,
minimizing model instability.
Fault Tolerance: In case of component failure (e.g., model loading errors, processing
errors), the system should recover gracefully without impacting other functionalities.
3. Usability
Learnability: Users should be able to easily learn how to interact with the system,
with minimal training or documentation required.
4. Resource Efficiency
Memory Usage: The system should optimize memory usage to avoid crashes or
slowdowns, particularly for large-scale models like BERT.
CPU/GPU Utilization: The system should make efficient use of available hardware,
leveraging GPUs for model inference when available to minimize processing time.
Energy Efficiency: The system should aim for minimal energy consumption,
particularly in cloud-based deployments, to reduce operational costs and environmental
impact.
8
5. Availability and Uptime
High Availability: The system should have high availability (e.g., 99.9% uptime),
especially in production environments, ensuring that users can access it at any time.
INTERFACE REQUIREMENTS
3.2.1 User Requirements
Data preprocessing
Test frameworks
Web frameworks
Real time Predition
1.Accuracy: Measure how often the model correctly predicts the emotion. High accuracy
is crucial, especially if the application is sensitive to misclassifications.
3.F1 Score: This is the harmonic mean of precision and recall. It is a good metric to assess
the model's performance, especially in cases where class distribution is imbalanced.
9
3. SYSTEM DESIGN
Goals:
The Primary goals in the design of the UML are as follows:
Provide users a ready-to-use, expressive visual modeling Language so that they can
develop and exchange meaningful models.
Provide extendibility and specialization mechanisms to extend the core concepts.
Be independent of particular programming languages and development process.
Provide a formal basis for understanding the modeling language.
Encourage the growth of OO tools market.
Support higher level development concepts such as collaborations, frameworks,
patterns and components.
10
4.1.1 Data Flow Diagram
A data flow diagram (DFD) maps out the flow of information for any process or
system.It uses defined symbols like rectangles, circles and arrows, plus short text
labels, to show data inputs, outputs, storage points and the routes between each
destination. There are 2 levels in the below diagram.
11
4.1.2 Use Case Diagram:
A use case diagram in the Unified Modeling Language (UML) is a type of
behavioral diagram defined by and created from a Use-case analysis. Its purpose is to
present a graphical overview of the functionality provided by a system in terms of
actors, their goals (represented as use cases), and any dependencies between those use
cases. The main purpose of a use case diagram is to show what system functions are
performed for which actor. Roles of the actors in the system can be depicted.
12
4.1.3 Class Diagram:
In software engineering, a class diagram in the Unified Modeling Language (UML)
is a type of static structure diagram that describes the structure of a system by showing
the system's classes, their attributes, operations (or methods), and the relationships
among the classes. It explains which class contains information.
13
4.1.4 Sequence Diagram:
A sequence diagram in Unified Modelling Language (UML) is a kind of
interaction diagram that shows how processes operate with one another and in what
order. It is a construct of a Message Sequence Chart. Sequence diagrams are
sometimes called event diagrams, event scenarios, and timing diagrams.
14
4.1.5 Activity Diagram:
Activity diagrams are graphical representations of workflows of stepwise activities
and actions with support for choice, iteration and concurrency. In the Unified Modeling
Language, activity diagrams can be used to describe the business and operational step-
by-step workflows of components in a system. An activity diagram shows the overall
flow of control.
Data Collection: Collect a diverse set of text data from various sources (e.g., social media
posts, reviews, or conversational data) labelled with different emotions such as joy,
sadness, anger, fear, etc. Ensure the data covers a wide range of emotional expressions to
provide robust training for emotion classification. Obtain pre-trained BERT models for text
embedding as a starting point.
Preprocessing: Clean the collected text data by removing unnecessary elements such as
special characters, URLs, and stopwords. Tokenize the text and convert it into a format
suitable for BERT, including adding [CLS] and [SEP] tokens for sentence representation.
Perform label encoding for emotions to ensure they are in a numerical format for model
training. Split the dataset into training and testing sets for model evaluation.
Training: Fine-tune the pre-trained BERT model using the preprocessed text data. Split
the preprocessed data into training and validation sets for model training. Adjust
hyperparameters such as learning rate, batch size, and maximum sequence length for
optimal performance.
Evaluation: Evaluate the fine-tuned BERT model using evaluation metrics such as
accuracy, F1-score, precision, and recall. Analyze the model's performance across different
emotional categories and review its precision in detecting emotions from text. Visualize
prediction probabilities and compare them with the true labels to assess the model’s
predictive accuracy.
16
4.2.2 End User Module
Users can log into the application using a username and password, ensuring secure
access. Upon successful login, users are directed to the main interface of the emotion detection
system. Users can input any text or sentence into the provided text field for emotion analysis.
The input text is preprocessed and prepared for emotion detection using the BERT-based
model. Upon submitting the text, the system predicts the emotion conveyed in the input. The
BERT-based model processes the text and classifies it into one of the predefined emotion
categories (e.g., joy, anger, sadness, etc.).The application displays the predicted emotion
along with a confidence score. Users can view a graphical representation of the emotion
prediction, such as a probability chart or emoji-based feedback for better understanding. The
application may also provide a comparison of various emotion probabilities detected in the
input text.
17
5 IMPLEMENTATION AND RESULTS
Python
Python is an interpreted high-level programming language for general-purpose
programming. Created by Guido van Rossum and first released in 1991, Python has
a design philosophy that emphasizes code readability, notably using significant
whitespace.
Python is Interactive − you can actually sit at a Python prompt and interact
with theinterpreter directly to write your programs.
Python also acknowledges that speed of development is important. Readable and
terse code is part of this, and so is access to powerful constructs that avoid tedious
repetition of code. Maintainability also ties into this may be an all but useless metric,
but it does say something about how much code you have to scan, read and/or the
easewith which a programmer of other languages can pick up basic Python skills
and the huge standard library is key to another area where Python excels. All its tools
have been quick to implement, saved a lot of time, and several of them have later
been patched and updated by people with no Python background - without breaking.
18
5.1.2 What is Deep Learning : -
Deep learning is a subset of machine learning that focuses on using neural networks
with multiple layers, known as deep neural networks, to model complex patterns and
representations from data. It is an approach that has revolutionized artificial intelligence (AI),
enabling machines to perform tasks such as image recognition, natural language processing,
speech recognition, and autonomous driving with unprecedented accuracy. Unlike traditional
machine learning algorithms, deep learning does not require manual feature extraction but
learns features automatically from raw data through hierarchical representations.
Neural Networks: The Foundation of Deep Learning At the core of deep learning
lies the artificial neural network (ANN), which is inspired by the structure and function of the
human brain. A neural network consists of interconnected layers of nodes, also called neurons,
that process input data and learn to make decisions based on it.
• Input Layer: This layer receives the input data, such as an image, a piece of text, or a sound.
• Hidden Layers: These layers lie between the input and output layers. Each neuron in a hidden
layer transforms the data using a mathematical function and passes it to the next layer. In deep
learning, the number of hidden layers is large, allowing the network to capture increasingly
abstract representations of the data.
• Output Layer: This layer produces the final output, such as a classification (e.g., recognizing
an object in an image) or a prediction (e.g., estimating the price of a stock).
19
Modules Used in Project :-
ktrain
The ktrain module is a lightweight Python library designed to simplify the process
of building, training, and deploying machine learning models. It provides an intuitive
and user-friendly interface for both traditional machine learning algorithms and modern
deep learning models, making it easier for developers and researchers to work with
complex frameworks like TensorFlow and Keras.
Numpy
Numpy is a general-purpose array-processing package. It provides a high-
performance multidimensional array object, and tools for working with these arrays. It
is the fundamental package for scientific computing with Python. It contains various
features including these important ones:
A powerful N-dimensional array object.
Sophisticated (broadcasting) functions.
Tools for integrating C/C++ and Fortran code.
Useful linear algebra, Fourier transform, and random number capabilities.
Besides its obvious scientific uses, Numpy can also be used as an efficient multi-
dimensional container of generic data. Arbitrary data-types can be defined using Numpy
which allows Numpy to seamlessly and speedily integrate with a wide variety of
databases.
Pandas
Pandas is an open-source Python Library providing high-performance data
manipulation and analysis tool using its powerful data structures. Python was majorly
used for data munging and preparation. It had very little contribution towards data
analysis. Pandas solved this problem.
20
Using Pandas, we can accomplish five typical steps in the processing and analysis
of data, regardless of the origin of data load, prepare, manipulate, model, and analyze.
Python with Pandas is used in a wide range of fields including academic and commercial
domains including finance, economics, Statistics, analytics, etc.
Matplotlib
Matplotlib is a Python 2D plotting library which produces publication quality figures
in a variety of hardcopy formats and interactive environments across platforms.
Matplotlib can be used in Python scripts, the Python and IPython shells,the
Jupyter Notebook, web application servers, and four graphical user interface toolkits.
Matplotlib tries to make easy things easy and hard things possible. You can generate
plots, histograms, power spectra, bar charts, error charts, scatter plots, etc., with just a
few lines of code. For examples, see the sample plots and thumbnail gallery.
For simple plotting the pyplot module provides a MATLAB-like interface, particularly
when combined with IPython. For the power user, you have full control of line styles,
font properties, axes properties, etc, via an object oriented interface or via a set of
functions familiar to MATLAB users.
Scikit – learn
Scikit-learn provides a range of supervised and unsupervised learning algorithms via
a consistent interface in Python. It is licensed under a permissive simplified BSD license
and is distributed under many Linux distributions, encouraging academic and
commercial use. Python
21
5.2 EXTENSION OF KEY FUNCTION:
The neattext library's functions module under the alias nfx. The neattext library is a
tool for cleaning and preprocessing text data, often used in NLP (Natural Language
Processing) tasks.
The remove_userhandles function from the neattext library to each entry in the Text
column of the DataFrame df. The remove_userhandles function removes user handles (e.g.,
@username) commonly found in social media posts like tweets. The cleaned text is stored in
a new column called Clean_Text.
22
5.2.2 Training
BERT Algorithm:
This function is used to convert raw text into a format suitable for BERT-based
models. It tokenizes the input text and ensures the data is prepared for efficient processing
during model training. BERT requires specific input formats like tokenized sequences and
attention masks, and this function helps in setting that up.
23
5.2.3 Evaluation
Accuracy:
The one-cycle learning rate policy involves varying the learning rate during training. The
learning rate starts small, increases to a maximum value, and then decreases. This approach can
help in achieving better convergence.
Over the epochs, you can see that both training and validation accuracies generally improve,
indicating that the model is learning and generalizing well.
Overall, the model shows improvement in both training and validation metrics over the
epochs, which suggests that the training process is effective.
24
• BERT Algorithm:
BERT (Bidirectional Encoder Representations from Transformers) is a powerful
transformer-based model introduced by Google for natural language understanding tasks. It has
been widely used in various NLP tasks due to its ability to capture deep contextual relationships
in text.
Unlike traditional models that process text in a left-to-right or right-to-left manner, BERT
processes text bidirectionally. This means it looks at the entire sentence (or context)
simultaneously to understand the meaning of each word in relation to others.
BERT is initially trained on a large corpus of text data (such as Wikipedia) to learn general
language representations. It uses tasks like Masked Language Modeling (MLM) and Next
Sentence Prediction (NSP) during this phase.
After pre-training, BERT can be fine-tuned on specific tasks (like text classification) using
a smaller, task-specific dataset. This step adapts BERT's general language understanding to the
particular task at hand.
• LSTM Algorithm:
LSTM (Long Short-Term Memory) is a type of Recurrent Neural Network (RNN)
architecture specifically designed to handle sequential data and long-term dependencies. Unlike
traditional RNNs, which struggle with the vanishing gradient problem, LSTMs are capable of
learning long-range dependencies in sequences. Here’s a detailed explanation of the LSTM
algorithm based on its common usage and principles.
LSTM networks are well-suited for tasks involving sequences of data, such as time series
forecasting, text analysis, and speech recognition. They process data sequentially, maintaining a
hidden state that captures information from previous steps.
LSTMs are effective for tasks where understanding the order and context of elements in a
sequence is crucial. They excel in scenarios like sentiment analysis and sequence prediction,
where maintaining long-term dependencies in data is necessary
25
5.3 OUTPUT SCREENS :
In the above screen you need to give the input in text area
26
Fig 5.1.3 Input
27
Fig 5.1.4 Output
A bar chart is also provided to show the prediction probabilities for various
emotions. Among the emotions such as joy, sadness, fear, anger, neutral, and
others, anger has the highest probability, reaching around 0.7 (or 70%). Other
emotions, including joy, sadness, and neutral, register very low probabilities,
essentially close to zero. This visualization further reinforces the system’s conclusion
that anger is the dominant emotion in the analyzed text.
28
6. SYSTEM TEST
Integration testing
Integration tests are designed to test integrated software components to determine if they
actually run as one program. Testing is event driven and is more concerned with the basic
outcome of screens or fields. Integration tests demonstrate that although the components
were individually satisfaction, as shown by successfully unit testing, the combination of
components is correct and consistent. Integration testing is specifically aimed at exposing
the problems that arise from the combination of components.
29
Functional test
Functional tests provide systematic demonstrations that functions tested are available as
specified by the business and technical requirements, system documentation, and user
manuals.
System Test
System testing ensures that the entire integrated software system meets requirements. It
tests a configuration to ensure known and predictable results. An example of system testing
is the configuration oriented system integration test. System testing is based on process
descriptions and flows, emphasizing pre-driven process links and integration points.
30
Black Box Testing
Black Box Testing is testing the software without any knowledge of the inner workings,
structure or language of the module being tested. Black box tests, as most other kinds of
tests, must be written from a definitive source document, such as specification or
requirements document, such as specification or requirements document. It is a testing in
which the software under test is treated, as a black box .you cannot “see” into it. The test
provides inputs and responds to outputs without considering how the software works.
31
TC03 Multiple 1. Verify “I am “The output “Only one Fail
Emotions the Happy but should emotion is
output also show two been
with the Nervous” emotions” predicted”
input text
expresse
s mixed
emotions
.
TC04 Empty 1. Verify “ ” The The output Success
input if the program is invalid
model should input.
handles handle
empty errors
input indicating
gracefull invalid
y. input
32
7. CONCLUSION AND FUTURE ENHANCEMENT
33
8. REFERENCES
[2] Jia Guo. "Deep learning approach to text analysis for human emotion detection
from big data" , Journal of Intelligent Systems, 2022.J. Vijayalakshmi, K. PandiMeena,
Agriculture TalkBot Using AI, International Journal of Recent Technology and
Engineering (IJRTE), July 2019
[4] M. Abdul-Mageed and L. Ungar, ‘‘Emonet: Fine Grained Emotion Detection With
Gated Recurrent Neural Networks’’ in Proc. 55th Annu. Meeting Assoc. Comput.
Linguistics, vol. 4, Jul. 2017, pp. 718–728.
[6] Binali, Haji, Chen Wu, and Vidyasagar Potdar. "Computational approaches for
emotion detection in text." In Digital Ecosystems and Technologies (DEST), 2010 4th
IEEE International Conference on, pp. 172-177. IEEE, 2010.
[7] Wu, Chung-Hsien, Ze-Jing Chuang, and Yu-Chung Lin. "Emotion recognition from
text using semantic labels and separable mixture models." ACM transactions on Asian
language information processing (TALIP) 5, no. 2(2006):165-183
34
[9] P. Zhong and C. Miao, ‘‘Ntuer At Semeval-2019 Task 3: Emotion Classification
With Word And Sentence Representations In RCNN’’ Feb. 2019.
[10] https://www.geeksforgeeks.org/explanation-of-bert-model-nlp/
[11] https://www.analyticsvidhya.com/blog/2020/05/what-is-tokenization-nlp/
8.2 WEBSITES
[1] https://www.sciencedirect.com/science/article/abs/pii/S0893608022000958
[2] https://ieeexplore.ieee.org/document/9317523
[3] https://ieeexplore.ieee.org/document/10053943
3. "Natural Language Processing with Python: Analyzing Text with the Natural
Language Toolkit" by Steven Bird, Ewan Klein, and Edward Loper
4. “Deep Learning for Natural Language Processing" by Palash Goyal, Sumit Pandey, and Karan
Jain
35