DEV Community

Cover image for Building a Sentiment Analysis Chatbot Using Neural Networks
Jay Codes
Jay Codes

Posted on • Originally published at Medium

Building a Sentiment Analysis Chatbot Using Neural Networks

Understanding and responding to user sentiments is crucial to building engaging and effective conversational systems in today's digital world. Think of a friend who responds to your questions and adapts his tone and words based on your emotions. This article will explore the fascinating intersection of sentiment analysis and chatbot development. We'll explore building a sentiment analysis chatbot using neural networks and rule-based patterns.

Problem Statement

As developers, we often seek to create applications that provide accurate information and connect with users on a deeper level. Traditional chatbots must improve at delivering empathetic and relevant responses, mainly when user emotions come into play. This project addresses the challenge of building a chatbot that understands the sentiments behind user messages and tailors its responses accordingly.

A photo showing emotional intelligence
Let's say you have a friend to whom you can relate all that troubles you, and you suddenly talk to them, and they respond in a way that doesn't resonate with your emotions. Of course, you'd feel some amount of disappointment.

Combining sentiment analysis and rule-based response generation, we aim to enhance the user experience and create a more engaging conversational environment.

In the following sections, we'll discuss the steps involved in developing this sentiment analysis chatbot. We'll explore the dataset used for training, the neural network architecture powering the sentiment analysis model, the integration of sentiment analysis into the chatbot's logic, and the rule-based approach for generating contextual responses. By the end of this journey, you'll have gained insights into both sentiment analysis and chatbot development, and you'll be equipped to create your own intelligent and emotionally aware chatbots.

Dataset and Preprocessing

Building a robust sentiment analysis model requires access to a suitable dataset that covers a wide range of emotions and expressions. For this project, we utilized the Topical Chat dataset sourced from Amazon. This dataset comprises over 8,000 conversations and a staggering 184,000 messages, making it a valuable resource for training our sentiment analysis model.

Dataset Description

The Topical Chat dataset captures real-world conversations, each with an associated sentiment label representing the emotion expressed in the message. The dataset covers many sentiments, including happiness, sadness, curiosity, and more. Understanding user emotions is crucial for the chatbot's ability to generate empathetic and contextually relevant responses.

Preprocessing Steps

Before feeding the data into our model, we performed preprocessing to ensure the data's quality and consistency. The preprocessing pipeline included the following steps:

python
#IMPORT NECESSARY LIBRARIES
import tensorflow as tf
from tensorflow import keras
import numpy as np
import pandas as pd
import nltk
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
import string
# Load the data
# Load the dataset
bot_dataset = pd.read_csv("\topical_chat.csv")
# Download stopwords and punkt tokenizer
nltk.download('punkt')
nltk.download('stopwords')

# Preprocessing function
def preprocess_text(text):
    # Tokenize
    tokens = word_tokenize(text)

    # Remove stopwords and punctuation
    tokens = [word.lower() for word in tokens if word.isalnum() and word.lower() not in stopwords.words("english")]

    return " ".join(tokens)

# Apply preprocessing to the "message" column
bot_dataset["processed_message"] = bot_dataset["message"].apply(preprocess_text)
Enter fullscreen mode Exit fullscreen mode
  1. Tokenization: Breaking down sentences into individual words or tokens facilitates analysis and model training.
  2. Text Cleaning: Removing special characters, punctuation, and unnecessary whitespace to make the text more uniform
  3. Stopword Removal: Eliminating common words that don't contribute much to sentiment analysis
  4. Label Encoding: Converting sentiment labels into numerical values for model training By conducting these preprocessing steps, we transformed raw conversational data into a format the neural network model could understand and learn. Now, let's delve into the architecture of the neural network model used for sentiment analysis and explore how it predicts emotions from text messages.

Model Architecture

In this project, we'll use a neural network model to understand and predict user emotions from text messages. The architecture of this model is a crucial component that enables the chatbot to discern sentiments and generate appropriate responses.

Neural Network Layers

The model architecture is structured as follows:

  1. Embedding Layer: The embedding layer converts words or tokens into numerical vectors. Each word is represented by a dense vector that captures its semantic meaning.
  2. LSTM (Long Short-Term Memory) Layers: LSTM layers process the embedded sequences, capturing the sequential dependencies in the text. LSTMs are well-suited for tasks involving sequences and can capture context over long distances.
  3. Dense Layer: The final dense layer produces an output representing the predicted sentiment. This output is then used to generate responses that match the user's emotional tone. ###Activation Functions and Parameters Throughout the architecture, activation functions such as ReLU (Rectified Linear Unit) are applied to introduce non-linearity and enhance the model's ability to capture complex relationships in the data. Additionally, hyperparameters such as batch size, learning rate, and the number of LSTM units are tuned to optimize the model's performance.
python
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout

model = Sequential([
    Embedding(input_dim=5000, output_dim=128, input_length=100),
    LSTM(128, return_sequences=True),
    LSTM(64),
    Dense(64, activation='relu'),
    #Dropout(0.5),
    Dense(8, activation='linear')
])
#Print the model summary
model.summary()
Enter fullscreen mode Exit fullscreen mode

The model.summary() function will print the outline of the model layers, as seen in the picture below:

Model Summary

Training and Optimization

The model is trained using the preprocessed Topical Chat dataset. The model learns to map text sequences to sentiment labels during training through backpropagation and gradient descent optimization. Loss functions, such as categorical cross-entropy, guide the training process by quantifying the difference between predicted and actual sentiments.

Next, we'll delve into the training process, evaluate the model's performance, and explore how sentiment analysis is integrated into the chatbot's logic.

Training and Evaluation

Training a sentiment analysis model involves exposing it to labeled data and allowing it to learn the patterns that link text sequences to specific emotions. This section will look at training and evaluating the model's performance.

Model Training

The model is trained using the preprocessed Topical Chat dataset. The training process includes the following steps:

  1. Input Sequences: Text sequences from conversations are fed into the model. Each sequence represents a message along with its associated sentiment label.
  2. Forward Pass: The input sequences pass through the model's layers. The embedding layer converts words into numerical vectors, while the LSTM layers capture the sequential context.
  3. Prediction and Loss: The model generates predictions for the sentiment labels. The categorical cross-entropy loss quantifies the difference between predicted and actual labels.
  4. Backpropagation: Gradient descent and backpropagation adjust the model's parameters. The model learns to minimize the loss by iteratively updating its weights.
#TRAIN THE MODEL
model.fit(X_train_padded, y_train_encoded, epochs=5, batch_size=45, validation_split=0.1)
Enter fullscreen mode Exit fullscreen mode

Model Evaluation

After training, the model's performance is evaluated using a separate data set, often called the validation or test set. The evaluation metrics include accuracy, precision, recall, and F1-score. These metrics provide insights into how well the model generalizes to unseen data.

#Evaluate the model
loss, accuracy = model.evaluate(X_test_padded, y_test_encoded)
print("Test accuracy:", accuracy)
Enter fullscreen mode Exit fullscreen mode

Hyperparameter Tuning

Hyperparameters, such as learning rate, batch size, and LSTM units, significantly influence the model's performance. So, we iteratively experiment and validate to find the optimal set of hyperparameters that yield the best results.

As we progress, we'll examine how sentiment predictions are integrated into the chatbot's logic. We'll use the rule-based approach for generating responses based on predicted sentiments.

Integration with Chatbot

Let's explore how sentiment analysis seamlessly integrates into the chatbot's logic, enabling it to generate contextually relevant and emotionally aware responses.

Sentiment-Based Response Generation

The key innovation of our sentiment analysis chatbot lies in its ability to tailor responses based on predicted sentiments. When a user inputs a message, the chatbot performs the following steps:

  1. Sentiment Analysis: The message is passed through the trained sentiment analysis model, which predicts the sentiment label.
  2. Response Generation: Based on the predicted sentiment, the chatbot generates a response that matches the emotional tone of the user's message. For example, a sad sentiment might trigger a comforting response, while a happy sentiment might foster an enthusiastic reply.
def predict_sentiment(text):
    processed_text = preprocess_text(text)
    sequence = tokenizer.texts_to_sequences([processed_text])
    padded_sequence = pad_sequences(sequence, maxlen=100, padding="post", truncating="post")
    sentiment_probabilities = model.predict(padded_sequence)
    predicted_sentiment_id = np.argmax(sentiment_probabilities)
    predicted_sentiment = label_encoder.inverse_transform([predicted_sentiment_id])[0]
    return predicted_sentiment

user_input = input("Enter a message: ")
predicted_sentiment = predict_sentiment(user_input)
print("Predicted sentiment:", predicted_sentiment)
Enter fullscreen mode Exit fullscreen mode

By incorporating sentiment analysis into the chatbot's logic, we elevate the conversational experience to a new level of empathy and understanding. Users feel heard and acknowledged as the chatbot responds in ways that resonate with their emotions. This empathetic connection enhances user engagement and fosters a more meaningful interaction.

Rule-Based Approach for Response Generation

While sentiment analysis is a powerful tool for enhancing the chatbot's responses, a rule-based approach further enriches the diversity and appropriateness of the generated content. Let's look at how we implement rule-based patterns to provide contextually relevant and emotionally aligned responses.

def generate_rule_based_response(predicted_sentiment):
    if predicted_sentiment == "Happy":
        response = "I'm glad to hear that you're feeling happy!"
    elif predicted_sentiment == "Sad":
        response = "I'm sorry to hear that you're feeling sad. Is there anything I can do to help?"
    else:
        response = "I'm here to chat with you. How can I assist you today?"

    return response

def generate_rule_based_response_chatbot(user_input):
    # Predict sentiment using your neural network model (code you've shared earlier)
    predicted_sentiment = predict_sentiment_nn(user_input)

    # Generate response based on predicted sentiment using rule-based approach
    response = generate_rule_based_response(predicted_sentiment)

    return response

def generate_pattern_response(user_input):
    patterns = {
        "hello": "Hello! How can I assist you today?",
        "how are you": "I'm just a chatbot, but I'm here to help! How can I assist you?",
        "help": "Sure, I'd be happy to help. What do you need assistance with?",
        "bye": "Goodbye! If you have more questions in the future, feel free to ask.",
        # Add more patterns and responses here
    }

    # Look for pattern matches and return the corresponding response
    for pattern, response in patterns.items():
        if pattern in user_input.lower():
            return response

    # If no pattern matches, use the rule-based response based on sentiment
    return generate_rule_based_response_chatbot(user_input)

while True:
    user_input = input("You: ")
    if user_input.lower() == "exit":
        print("Bot: Goodbye!")
        break
    bot_response = generate_pattern_response(user_input)
    print("Bot:", bot_response)
Enter fullscreen mode Exit fullscreen mode

Pattern Matching

Rule-based patterns involve creating predefined rules that trigger specific responses based on user input. Keywords, phrases, or clear sentiment labels can start with these rules. The chatbot generates responses that resonate with the conversation's context by anticipating user needs and emotions.
Let's illustrate with an example:
User Input: "I feel excited about this project!"
Predicted Sentiment: "Happy"
Based on the predicted sentiment, we implement the following rule:
Rule-Based Response: "I'm glad to hear that you're feeling excited!"
In this way, the chatbot provides contextually relevant and empathetic responses that align with user emotions. The rule-based approach allows the chatbot to generate responses that adhere to specific patterns quickly.

Conclusion

Throughout this project, we've explored the details of combining sentiment analysis with chatbot development, resulting in a system that understands user emotions and responds with empathy and relevance.

Building a sentiment analysis chatbot that connects with users emotionally is a remarkable achievement in AI.

While we've achieved a functional sentiment analysis chatbot, the journey doesn't end here. There are several exciting avenues for further enhancing our sentiment analysis chatbot and pushing the boundaries of conversational AI.
You can visit my Repo on Github for reference.

GitHub logo Jaynwabueze / Simple_Chat_bot

A simple interactive chatbot built with neural networks

Simple chatbot

Build Status License

Description

A chatbot project that combines sentiment analysis with response generation using neural networks.

Usage

  1. Clone the repository: git clone https://github.com/Jaynwabueze/Simple_Chat_bot.git

  2. Run the chatbot script: python chatbot.py

Dataset

The sentiment analysis model is trained using the Topical Chat dataset from Amazon. This dataset consists of over 8000 conversations and over 184000 messages. Each message has a sentiment label representing the emotion of the sender.

Model Information

The sentiment analysis model is built using a neural network architecture. It involves an embedding layer followed by LSTM layers for sequence processing. The model is trained on the sentiment-labeled messages from the dataset to predict emotions.

Chatbot

The chatbot component leverages the trained sentiment analysis model to generate contextually appropriate responses. Based on the predicted sentiment of the user input, the chatbot provides empathetic and relevant responses.

Contact

For questions or feedback, please contact Judenwabueze.




Top comments (0)