Report Sentiment Analysis Using NLP and Deep Learning
Report Sentiment Analysis Using NLP and Deep Learning
EXAMINER APPROVAL
The Project entitled “Sentiment Analysis using NLP and Deep Learning”
submitted by Aeshna Jain(0827CS201017), Bhavik Mundra(0827CS201057),
has been examined and is hereby approved towards partial fulfillment for the
award of Bachelor of Engineering degree in Computer Science & Engineering
discipline, for which it has been submitted. It is understood that by this approval
the undersigned do not necessarily endorse or approve any statement made,
opinion expressed or conclusion drawn therein, but approve the project only for
the purpose for which it has been submitted.
Date: Date:
i
Sentiment Analysis using NLP and Deep Learning
GUIDE RECOMMENDATION
This is to certify that the work embodied in this project “Sentiment Analysis
using NLP and Deep Learning” submitted by Aeshna Jain(0827CS201017),
Bhavik Mundra(0827CS201057), is a satisfactory account of the bonafide work
done under the supervision of Prof. Ritika Bhatt are recommended towards
partial fulfillment for the award of the Bachelor of Engineering (Computer Science
& Engineering) degree by Rajiv Gandhi Proudyogiki Vishwavidhyalaya, Bhopal.
ii
Sentiment Analysis using NLP and Deep Learning
STUDENTS UNDERTAKING
This is to certify that project entitled “Sentiment Analysis using NLP and Deep
Learning” has developed by us under the supervision of Prof. Ritika Bhatt. The
whole responsibility of work done in this project is ours. The sole intension of this
work is only for practical learning and research.
We further declare that to the best of our knowledge; this report does not contain
any part of any work which has been submitted for the award of any degree either
in this University or in any other University / Deemed University without proper
citation and if the same work found then we are liable for explanation to this.)
Aeshna Jain(0827CS201017)
Bhavik Mundra(0827CS201057)
iii
Sentiment Analysis using NLP and Deep Learning
Acknowledgement
We thank the almighty Lord for giving me the strength and courage to sail out
through the tough and reach on shore safely. There are number of people without
whom this projects work would not have been feasible. Their high academic
standards and personal integrity provided me with continuous guidance and
support. We owe a debt of sincere gratitude, deep sense of reverence and respect
to our guide and mentors Prof. Ritika Bhatt, Associate Professor, AITR, for their
motivation, sagacious guidance, constant encouragement, vigilant supervision and
valuable critical appreciation throughout this project work, which helped us to
successfully complete the project on time.
We express profound gratitude and heartfelt thanks to Dr. Kamal Kumar Sethi,
HOD CSE, AITR Indore for his support, suggestion and inspiration for carrying out
this project. I am very much thankful to other faculty and staff members of CSE
Dept, AITR Indore for providing me all support, help and advice during the project.
We would be failing in our duty if do not acknowledge the support and guidance
received from Dr. S.C. Sharma, Director, AITR, Indore whenever needed. We take
opportunity to convey my regards to the management of Acropolis Institute,
Indore for extending academic and administrative support and providing me all
necessary facilities for project to achieve our objectives.
We are grateful to our parent and family members who have always loved and
supported us unconditionally. To all of them, we want to say, “Thank you”, for being
the best family that one could ever have and without whom none of this would
have been possible.
Aeshna Jain(0827CS201017)
Bhavik Mundra(0827CS201057)
iv
Sentiment Analysis using NLP and Deep Learning
Executive Summary
The project is based on Natural Language Processing and Deep Learning, which is
a sub field of machine learning, concerned with algorithms inspired by the
structure and function of the brain called artificial neural networks. In this project,
BERT model is used for sentiment analysis and deep convolution network is used
for emotion detection.
v
Sentiment Analysis using NLP and Deep Learning
vi
Sentiment Analysis using NLP and Deep Learning
List of Figures
vii
Sentiment Analysis using NLP and Deep Learning
List of Abbreviations
viii
Sentiment Analysis using NLP and Deep Learning
Table of Contents
Chapter 1: Introduction 1
1.1. Introduction 1
1.2. Overview 1
1.3. Background and Motivation 2
1.4. Problem Statement and Objectives 3
1.5. Scope of the Project 3
1.6. Team Organization 4
1.7. Report Structure 5
Chapter 2: Review of Literature 7
2.1 Review of Literature 7
2.2. Preliminary Investigation 8
2.2.1. Current System 8
2.3. Requirement Identification and Analysis for Project 8
2.4. Conclusion 11
Chapter 3: Proposed System 12
3.1. The Proposal 12
3.2. Benefits of the Proposed System 13
3.3. Feasibility Study 14
3.3.1. Technical 15
3.3.2. Economical 16
3.3.3. Operational 17
3.4. Design Representation 18
3.4.1. Use Case Diagram 18
3.4.2. Activity Diagram 19
3.4.3. Entity Relationship Diagram 20
3.4.4. Text Analyzer 20
3.4.5. Review Analyzer 21
3.4.6. Dataset Structure 21
3.5. Deployment Requirements 22
ix
Sentiment Analysis using NLP and Deep Learning
3.5.1. Hardware 22
3.5.2. Software 22
Chapter 4: Implementation 23
4.1. Implementation 23
4.2. Technique Used 23
4.2.1. Web Scrapping 23
4.2.2. Natural Language Processing 24
4.2.3. Deep Neural Network 25
4.3. Tools Used 26
4.3.1. Beautiful Soup 26
4.3.2. Hugging Face Transformer BERT model 26
4.3.3. Scikit Learn 27
4.3.4. Pandas 27
4.4. Language Used 28
4.5. Glimpse of Project 29
4.6. Testing 31
4.6.1. Strategy Used 31
4.6.2. Results 33
Chapter 5: Conclusion 34
5.1. Conclusion 34
5.2. Limitations of the Work 34
5.3. Suggestion and Recommendations for Future Work 35
Bibliography 36
Source Code 37
x
Sentiment Analysis using NLP and Deep Learning
Chapter 1: Introduction
1.1 Introduction
This project report aims to shed light on the need, benefits, and solutions
offered by sentiment analysis tools. We will explore how these tools are
instrumental in addressing the challenges posed by the deluge of textual data and
how they empower businesses, researchers, and individuals to gain valuable
insights, enhance decision-making processes, and uncover hidden trends and
patterns within their data. This report not only serves as an exploration of
sentiment analysis but also as a guide to harness its potential, demonstrating its
versatility in deciphering the intricate world of human emotions and opinions
conveyed through written text.
1.2 Overview
The project revolves around the utilization of Natural Language Processing (NLP)
and Deep Learning techniques to develop a model capable of evaluating comments
Page 1 of 54
Sentiment Analysis using NLP and Deep Learning
and reviews from a designated website. Users will input the source URL where
these reviews are hosted, and through the integration of NLP and Deep Learning,
the system will assess the emotions and sentiments expressed in these comments.
The primary goal of this system is to deliver trustworthy sentiment analysis of
product reviews, equipping stakeholders with valuable insights to make well-
informed business decisions.
In recent years, the exponential growth of digital content on the internet has
transformed the way information is shared, opinions are expressed, and products
are reviewed. This digital revolution has brought about a staggering amount of
user-generated content, particularly in the form of comments and reviews on
various online platforms. Understanding the sentiments and emotions embedded
within this vast corpus of text data is crucial for individuals, businesses, and
organizations looking to navigate the intricate landscape of public opinion
effectively.
The motivation behind this project stems from the pressing need for a
robust and reliable sentiment analysis system that harnesses the power of NLP
Page 2 of 54
Sentiment Analysis using NLP and Deep Learning
and Deep Learning. In today's digital age, the opinions and sentiments expressed
in comments and reviews on platforms ranging from e-commerce sites to social
media can significantly impact decision-making processes. Businesses rely on
customer feedback to enhance their products and services, while individuals seek
informed opinions to make purchasing decisions.
In recent years, the exponential growth of digital content on the internet has
transformed the way information is shared, opinions are expressed, and products
are reviewed. This digital revolution has brought about a staggering amount of
user-generated content, particularly in the form of comments and reviews on
various online platforms. Understanding the sentiments and emotions embedded
within this vast corpus of text data is crucial for individuals, businesses, and
organizations looking to navigate the intricate landscape of public opinion
effectively.
Objective: The aim of this system is to give a wider analysis of comments and
reviews posted for a product so that owner and developer of the product can take
a and beneficial business decisions and overcome any further risk posing situation.
This system provides a wider view into the comments and posts posted for a
product on different online platforms such as online shopping site, online food
ordering site etc.
Page 3 of 54
Sentiment Analysis using NLP and Deep Learning
Market Research:
• Aeshna Jain:
I conducted thorough research to select the appropriate technology and
delved deep into its intricacies. I also undertook model development to
create a system capable of analyzing textual data, specifically reviews on
the platform. Furthermore, I designed a Deep Learning model to effectively
classify the emotions expressed in the provided data, ensuring a precise
and comprehensive analysis of user experiences.
Page 4 of 54
Sentiment Analysis using NLP and Deep Learning
• Bhavik Mundra:
I conducted research, gathered essential resources, and performed web
scraping to collect the data required for building the model. Additionally, I
dedicated effort to developing the application's frontend and seamlessly
integrating it with the model. This integration allows for the presentation
of results, enabling decision-making for the system's users or stakeholders.
The project Sentiment Analysis using NLP and Deep Learning is primarily
concerned with the Natural Language Processing and Deep Learning
Techniques and whole project report is categorized into five chapters.
Page 5 of 54
Sentiment Analysis using NLP and Deep Learning
for the project undertaken. Later it gives details of the different deployment
requirements for the developed project.
Page 6 of 54
Sentiment Analysis using NLP and Deep Learning
Page 7 of 54
Sentiment Analysis using NLP and Deep Learning
There are many sentiment analysis tools launched by google and Facebook
among most prominent are:
• Google Insights
• Google Alerts
• Facebook Insights
• Other tools like Brand watch.
But all these tools are not customized to a specific intent and therefore
there is a need to develop a sentiment analyzer for particular purpose.
One of the major challenges in developing Sentiment Analysis systems using NLP
and Deep Learning is the lack of standardized data. Reviews vary on the daily basis,
new reviews are posted and deleted sometimes daily. Additionally, the accuracy of
the predictions may be affected by the quality of the data and the availability of
adequate amount of data.
Page 8 of 54
Sentiment Analysis using NLP and Deep Learning
• Creating a dataset from scratch for a NLP project can be a challenging task.
The main challenge is to webscrape a particular site o daily basis for fresh
reviews and if reviews are not adequate it will affect the training of the
model.
• Data collection can be a time-consuming process, and it may be difficult to
ensure the data is of high quality and representative of the problem being
solved.
• Furthermore, the dataset may need to be cleaned and pre-processed to
remove outliers, missing values, or irrelevant features. This can be a tedious
and error-prone process that requires careful attention to detail.
• Web scraping is the process of automatically extracting data from websites.
For sentiment analysis system, web scraping techniques can be used to
collect data from various online shopping sites.
• The web scraping process can be automated using programming tools such
as Python, Beautiful Soup, and Selenium. These tools can be used to extract
information from the HTML code of web pages and convert it into a
structured format such as a CSV or JSON file.
• It is important to note that web scraping can be a complex process, as
websites may use different structures and formats to present information.
Additionally, web scraping can raise legal and ethical concerns, as it may
violate website terms of service or infringe on user privacy. Therefore, it is
important to use web scraping techniques responsibly and with caution.
• Cleaning a dataset created from web scraping is an essential step in
preparing the data for machine learning. The web scraping process can
introduce errors and inconsistencies into the data, such as missing values,
duplicate entries, or incorrect formatting. These errors can negatively
impact the performance of the machine learning model.
• To clean the dataset, several steps may be necessary. First, missing values
and duplicates must be identified and removed. This can be done using
Page 9 of 54
Sentiment Analysis using NLP and Deep Learning
Page 10 of 54
Sentiment Analysis using NLP and Deep Learning
• In the other component the reviews are analyzed based on the products
and that review serves as a fundamental for visualizing various business
decisions. The application will provide graphical way to analyze the
reviews based on sentiment and emotions of the posts.
• The first step in creating the Sentiment detection model is to use the
pretrained BERT model which then returns a score ranging between 1 to 5.
The score less than 3 denotes the negative review, review with score 3 are
considered as neutral and larger than 3 are considered as positive reviews.
• Next, the emotional analysis is done by developing a convolution neural
network and trained this model on emotion detection dataset available on
Kaggle.
• Overall, both these analysis gives a wider view towards the perspective
shown by customers of specific product and the application will also
provide a graphical way to all these analysis to make it easy for
stakeholders to take important business decisions.
2.4 Conclusion
This chapter reviews the literature surveys that have been done during the
research work. In conclusion Sentiment Analysis using NLP and Deep Learning
have the potential to provide a valuable tool for both users and organizations.
While there are still challenges to overcome, the research in this field is promising
and suggests that accurate sentiment predictions can be achieved. Future research
should focus on developing more accurate models and standardizing the data used
to train them.
Page 11 of 54
Sentiment Analysis using NLP and Deep Learning
Page 12 of 54
Sentiment Analysis using NLP and Deep Learning
The proposed sentiment analyzer uses NLP BERT model to get the sentiments
score ranging from 1 to 5 where sentiment score less than 3 is a negative comment
and greater than 3 is a positive comment and score with exact 3 is a neutral
statement. Some key points of explanation are:
Page 13 of 54
Sentiment Analysis using NLP and Deep Learning
Overall, the proposed sentiment analysis system offers several benefits, including
improved accuracy, efficiency, customizability, insights for decision-making, and
scalability.
Page 14 of 54
Sentiment Analysis using NLP and Deep Learning
personal information in the dataset and the handling of sensitive data must
be addressed.
• Operational feasibility: The system's operational feasibility depends on
the availability of necessary resources, such as trained personnel, sufficient
data storage, and reliable internet connectivity. The use of automated tools
such as web scraping and using pretrained model can streamline
operations and make the system more efficient.
• Schedule feasibility: The development and implementation of the system
will require time and resources. The project must be appropriately planned
and scheduled to ensure timely delivery.
3.3.1 Technical
Page 15 of 54
Sentiment Analysis using NLP and Deep Learning
3.3.2 Economical
The main costs of the system include the development and implementation
of the software, data collection and processing, computational resources,
and maintenance costs. The costs associated with software development
and implementation include the cost of hiring developers, purchasing
necessary software and hardware, and any other development-related
expenses. Data collection and processing costs include the cost of web
scraping tools and the time and effort required to collect and process the
data.
On the other hand, the potential benefits of the system include increased
accuracy in sentiment prediction, reduced human error, improved
efficiency in the analysis process, and reduced operational costs. The
system can automate the process of sentiment prediction and its
visualization, allowing taking effective business decisions, leading to
increased profitability and productivity.
3.3.3 Operational
The study also considers the training needs for the employees who will use
the system and assesses their ability to adapt to the new technology.
Additionally, it evaluates the system's usability, reliability, and
performance.
review data. The training needs for the employees can be addressed by
providing proper training and support materials.
Page 18 of 54
Sentiment Analysis using NLP and Deep Learning
Page 19 of 54
Sentiment Analysis using NLP and Deep Learning
Page 20 of 54
Sentiment Analysis using NLP and Deep Learning
Page 21 of 54
Sentiment Analysis using NLP and Deep Learning
3.5.1 Hardware
• Processor: Intel Core i5 or higher
• RAM: 8 GB or higher
• Hard Disk Space: 1 TB or higher
• Graphics Card: NVIDIA or AMD with a minimum of 2 GB
memory
• Internet Connection: Broadband or higher
3.5.2 Software
• Operating System: Windows 10 or Ubuntu 18.04 or higher
Python 3.7 or higher
• Transformers from Hugging Face Library
• Scikit-learn Library
• BeautifulSoup Library
• StreamlitLibrary
• Tensorflow Library
Overall, the hardware and software requirements for the proposed system require
careful consideration to ensure that the system functions correctly and provides
users with a satisfactory experience.
Page 22 of 54
Sentiment Analysis using NLP and Deep Learning
Chapter 4: Implementation
4.1 Implementation
Page 23 of 54
Sentiment Analysis using NLP and Deep Learning
rent prediction system, web scraping will be used to collect data from
various real estate websites to create a dataset for training the machine
learning model.
Web scraping can be performed using various tools and libraries such as
Beautiful Soup, Scrapy, Selenium, and many more. These tools allow
developers to extract data from websites in a structured manner, making it
easy to collect the required information.
In this project, NLP hugging face library is used to predict the sentiment
score. BERT model is a pretrained model trained on millions of textual data
and is fine tuned to predict sentiment score for the reviews.
Page 25 of 54
Sentiment Analysis using NLP and Deep Learning
Beautiful Soup is a Python library that is used for web scraping purposes.
It is used to extract the data from HTML and XML files by parsing the
content of the files. Beautiful Soup provides a simple and intuitive interface
for working with HTML and XML files, making it easy to extract the
required data.
In this system, Beautiful Soup will be used to extract data from specific
product site. The library will be used to parse the HTML files of the web
pages and extract the reviews. The data extracted by Beautiful Soup will be
used to analyze sentiment. Beautiful Soup can be installed using pip or
conda package manager.
4.3.4 Pandas
Page 27 of 54
Sentiment Analysis using NLP and Deep Learning
In the sentiment analysis system, Pandas can be used for various data-
related tasks, such as:
In the context of the system, Pandas can be used for cleaning and
preprocessing the data scraped from websites, as well as manipulating and
analyzing the dataset to prepare it for training the deep learning model.
Additionally, Pandas can be used to load the trained model and apply it to
new data to predict the rent.
Page 29 of 54
Sentiment Analysis using NLP and Deep Learning
Page 30 of 54
Sentiment Analysis using NLP and Deep Learning
4.6 Testing
Testing is the process of evaluation of a system to detect differences between given
input and expected output and also to assess the feature of the system. Testing
assesses the quality of the product. It is a process that is done during the
development process.
• Implementation testing
The texting method used here is Black Box Testing. It is carried out to test
functionality of the program. It is also called ‘Behavioral’ testing. The tester
in this case, has a set of input values and respective desired results. On
providing input, if the output matches with the desired results, the program
is tested ‘ok’, and problematic otherwise.
Finally, user acceptance testing was conducted to ensure that the system
meets the expectations and requirements of end-users. This involved a
group of users interacting with the system to verify that it is easy to use,
user-friendly, and provides accurate and reliable results.
Page 32 of 54
Sentiment Analysis using NLP and Deep Learning
4.6.2 Results
Sample Input and output:
Page 33 of 54
Sentiment Analysis using NLP and Deep Learning
Chapter 5: Conclusion
5.1 Conclusion
Sentiment analysis systems have become increasingly indispensable in today's
world due to their ability to extract valuable insights from the vast amount of
textual data generated daily. These systems help in solving various real-world
problems and offer significant advantages in our daily lives. In the business realm,
sentiment analysis is employed to gauge customer opinions and sentiment about
products and services, enabling companies to adapt and improve their offerings
based on feedback. It also aids in identifying emerging trends and issues early,
allowing for timely responses. In the realm of social media, sentiment analysis is
used to track public sentiment on important topics, from political events to public
health crises, helping governments and organizations make informed decisions.
Sentiment analysis is also applied in customer service to detect and address
negative feedback swiftly, enhancing user experiences. In news and media, it
assists in classifying news articles and identifying potentially biased reporting. By
providing a better understanding of public sentiment and opinions, sentiment
analysis systems play a pivotal role in enhancing decision-making, customer
satisfaction, and overall communication in the digital age.
Page 34 of 54
Sentiment Analysis using NLP and Deep Learning
Page 35 of 54
Sentiment Analysis using NLP and Deep Learning
Bibliography
Page 36 of 54
Sentiment Analysis using NLP and Deep Learning
Source Code
import pickle
import streamlit as st
import pandas as pd
def plot_sentiment_distribution(excel_file):
"""
Read data from an Excel file, filter sentiment scores, and create a
bar chart.
Parameters:
Returns:
• None
"""
try:
df = pd.read_excel(excel_file)
sentiment_scores = [1, 2, 3, 4, 5]
Page 37 of 54
Sentiment Analysis using NLP and Deep Learning
filtered_data = df[df['sentiment'].isin(sentiment_scores)]
review_counts =
filtered_data['sentiment'].value_counts().sort_index()
# plt.bar(review_counts.index, review_counts.values)
# plt.xlabel('Sentiment Score')
# plt.ylabel('Number of Reviews')
# plt.xticks(sentiment_scores)
# plt.show()
st.bar_chart(review_counts)
st.xlabel('Sentiment Score')
st.ylabel('Number of Reviews')
st.xticks(sentiment_scores)
except Exception as e:
# Example usage:
def plot_sentiment_pie_chart(excel_file):
try:
Page 38 of 54
Sentiment Analysis using NLP and Deep Learning
# Data Extraction
df = pd.read_excel(excel_file)
# Data Preparation
sentiment_counts = df['sentiment'].value_counts()
plt.figure(figsize=(6, 6))
sizes = [
sentiment_counts.get(1, 0) # Bad
plt.title('Composition of Reviews')
st.pyplot(plt)
except Exception as e:
Page 39 of 54
Sentiment Analysis using NLP and Deep Learning
def plot_emotion_category_bar_chart(excel_file):
try:
# Data Extraction
df = pd.read_excel(excel_file)
# Data Preparation
emotions = df['emotion_score']
emotion_counts = emotions.value_counts()
plt.figure(figsize=(10, 6))
emotion_counts.plot(kind='bar', color='yellow')
plt.xlabel('Emotion Category')
plt.ylabel('Number of Reviews')
st.pyplot(plt)
except Exception as e:
Page 40 of 54
Sentiment Analysis using NLP and Deep Learning
def main():
st.title("Emotion Detector")
choice = st.sidebar.selectbox("Menu",menu)
submit_button = st.form_submit_button(label="Submit")
if submit_button:
response = model.Completion.create(
engine="text-davinci-003",
temperature=0,
max_tokens=10
print(response)
sentiment = response.choices[0].text
st.text(sentiment)
else:
st.write("Review Analyzer")
if productChoice == "":
st.write("Selected product is :
{}".format(productChoice))
st.write("Selected product is :
{}".format(productChoice))
elif productChoice=="Yelp":
plot_sentiment_distribution('categorised_data2.xlsx')
plot_sentiment_pie_chart('categorised_data2.xlsx')
plot_emotion_category_bar_chart('categorised_data2.xlsx')
if __name__ == '__main__':
main()
#Training Model
"""semantic analysis.ipynb
Page 42 of 54
Sentiment Analysis using NLP and Deep Learning
https://colab.research.google.com/drive/1Cv1pGVt5TDnJatb-
T_DuNcgvQkupvTnq
"""
import torch
import requests
import re
tokenizer = AutoTokenizer.from_pretrained('nlptown/bert-base-
multilingual-uncased-sentiment')
model1 =
AutoModelForSequenceClassification.from_pretrained('nlptown/bert-
base-multilingual-uncased-sentiment')
result = model1(tokens)
Page 43 of 54
Sentiment Analysis using NLP and Deep Learning
result
result.logits
result.logits[0]
int(torch.argmax(result.logits))+1
import pickle
tokenizer = AutoTokenizer.from_pretrained('nlptown/bert-base-
multilingual-uncased-sentiment')
model =
AutoModelForSequenceClassification.from_pretrained('nlptown/bert-
base-multilingual-uncased-sentiment')
model_and_tokenizer = {
'tokenizer': tokenizer,
'model': model
Page 44 of 54
Sentiment Analysis using NLP and Deep Learning
pickle.dump(model_and_tokenizer, file)
r = requests.get('https://www.yelp.com/biz/social-brew-cafe-
pyrmont')
regex = re.compile('.*comment.*')
# https://www.yelp.com/biz/social-brew-cafe-pyrmont
reviews
import numpy as np
import pandas as pd
df = pd.DataFrame(np.array(reviews), columns=['review'])
df
def sentiment_score(review):
result = model1(tokens)
return int(torch.argmax(result.logits))+1
sentiment_score(df['review'].iloc[1])
print(len(df.index))
Page 45 of 54
Sentiment Analysis using NLP and Deep Learning
# for i in range(0,512):
# df['score'].iloc[i]=sentiment_score(df['review'].iloc[i])
df['sentiment'] = df['review'].apply(lambda x:
sentiment_score(x[:512]))
df
df.tail()
"""**Emotion** **Detection**"""
import tensorflow as tf
import keras
import pandas as pd
import re
data.shape
data.isna().any(axis=1).sum()
#text preprocessing
ps = PorterStemmer()
Page 46 of 54
Sentiment Analysis using NLP and Deep Learning
def preprocess(line):
#apply Stemming
import nltk
nltk.download('stopwords')
data['text']=data['text'].apply(lambda x: preprocess(x))
label_encoder = preprocessing.LabelEncoder()
data['N_label'] = label_encoder.fit_transform(data['label'])
data['text']
Page 47 of 54
Sentiment Analysis using NLP and Deep Learning
cv = CountVectorizer(max_features=5000,ngram_range=(1,3))#example:
the course was long-> [the,the course,the course was,course, course
was, course was long,...]
data_cv = cv.fit_transform(data['text']).toarray()
data_cv
model = Sequential()
model.add(Dense(12, input_shape=(X_train.shape[1],),
activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(6, activation='softmax'))
model.compile(loss='sparse_categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
import pandas as pd
import numpy as np
text=preprocess(text)
array = cv.transform([text]).toarray()
pred = model.predict(array)
a=np.argmax(pred, axis=1)
label_encoder.inverse_transform(a)[0]
tf.keras.models.save_model(model,'my_model.keras')
import pickle
vectorizer_filename = 'count_vectorizer.pkl'
pickle.dump(cv, file)
label_encoder_filename='label_encoder.pkl'
pickle.dump(label_encoder,file)
Page 49 of 54
Sentiment Analysis using NLP and Deep Learning
import tensorflow as tf
loaded_model = tf.keras.models.load_model('my_model.keras')
model_and_weights = {
'model': loaded_model
import pickle
pickle.dump(model_and_weights, file)
df['review'].iloc[1]
text = preprocess(text)
array = cv.transform([text]).toarray()
pred = model.predict(array)
Page 50 of 54
Sentiment Analysis using NLP and Deep Learning
predicted_emotion =
label_encoder.inverse_transform([predicted_class])[0]
return predicted_emotion
# text=preprocess(text)
# array = cv.transform([text]).toarray()
import pickle
loaded_model_dict = pickle.load(file)
loaded_model = loaded_model_dict['model']
predictions = loaded_model.predict(array)
a=np.argmax(predictions, axis=1)
label_encoder.inverse_transform(a)[0]
# for i in range(0,len(df)):
# df['emotion'].iloc[i]=emotion_f(df['review'].iloc[i])
# for i in range(0,512):
# df['score'].iloc[i]=sentiment_score(df['review'].iloc[i])
print(predict_emotion(df['review'].iloc[0],model,cv,label_encoder))
df['emotion_score'] = df['review'].apply(lambda x:
predict_emotion(x[:512],model,cv,label_encoder))
df
df3=df.copy()
Page 51 of 54
Sentiment Analysis using NLP and Deep Learning
df3
# df = pd.DataFrame(data)
excel_filename = 'data2.xlsx'
df.to_excel(excel_filename, index=False)
df
df
def catogery(review):
for i in range(0,len(df)):
df['category']="good"
df['category']="bad"
df
df2 = df.copy()
df2
Page 52 of 54
Sentiment Analysis using NLP and Deep Learning
import pandas as pd
def categorize_reviews(df):
sentiment = row['sentiment']
emotion_score = row['emotion_score']
return df
# # Example usage:
# data = {
Page 53 of 54
Sentiment Analysis using NLP and Deep Learning
# }
# df = pd.DataFrame(data)
df2 = categorize_reviews(df2)
excel_filename = 'categorised_data2.xlsx'
df2.to_excel(excel_filename, index=False)
excel_filename = 'categorised_data2.xlsx'
df2 = pd.read_excel(excel_filename)
df2
# print(sentiment_score(text))
# print(predict_emotion(text,model,cv,label_encoder))
Page 54 of 54