Sentiment Analysis

Sentiment Analysis

Unveiling the Power of Natural Language Processing (NLP)

Natural Language Processing (NLP) is a branch of artificial intelligence (AI) that focuses on the interaction between computers and humans using natural language. NLP enables computers to interpret, understand, and generate human language, facilitating seamless communication between machines and users. From voice assistants to sentiment analysis and language translation, NLP plays a pivotal role in various fields.

The roots of NLP trace back to the 1950s, with groundbreaking research by Alan Turing and his contemporaries. Over the years, significant developments have propelled NLP forward, including the creation of context-free grammars, the introduction of machine learning algorithms, and the advent of deep learning techniques. Modern NLP owes much of its progress to advancements in neural networks and large-scale language models.

Prominent NLP Models

BERT (Bidirectional Encoder Representations from Transformers): Introduced by Google in 2018, BERT revolutionized NLP with its bidirectional transformer architecture, enabling deep contextual understanding of words in a sentence. BERT has been widely adopted for various NLP tasks, such as question-answering and language translation.

GPT-4 (Generative Pre-trained Transformer): Developed by OpenAI, GPT is one of the largest language models to date (eight models with 220 billion parameters each). It excels in generating human-like text and has been applied in chatbots, content creation, and even programming tasks.

ELMo (Embeddings from Language Models): ELMo pioneered the concept of contextual word embeddings, where word representations are contextualized based on their surrounding text. This method enhanced the performance of NLP models in capturing nuances in language.

Real-Life Applications of NLP

Sentiment Analysis: Companies use sentiment analysis to gauge customer feedback from reviews, social media, and surveys. NLP models can identify sentiment polarity, helping businesses understand public perception of their products or services.

Chatbots and Virtual Assistants: NLP powers chatbots and virtual assistants, enabling them to interpret user queries, provide relevant information, and engage in meaningful conversations. From customer support to voice-activated assistants, NLP enhances user experiences.

Language Translation: NLP underpins machine translation systems like Google Translate, breaking down language barriers and facilitating communication across different languages. These systems leverage NLP techniques to accurately translate text and speech.

As NLP continues to evolve, the future holds exciting possibilities. Advancements in multimodal AI, zero-shot learning, and low-resource languages are expanding the horizons of NLP. With increased focus on sustainability, fairness, and user-centric design, NLP is poised to revolutionize how we interact with technology.

Decoding Sentiments: An Exploration of Sentiment Analysis

Sentiment Analysis, also known as opinion mining, is a branch of Natural Language Processing (NLP) that involves extracting and categorizing subjective information from text data. By analyzing the sentiments expressed in text, Sentiment Analysis empowers businesses, researchers, and individuals to gain valuable insights into public opinion, customer feedback, and social trends.

The origins of Sentiment Analysis can be traced back to early research in text classification and machine learning. In the 2000s, sentiment classification gained momentum with the rise of social media platforms and the need to analyze the vast amounts of user-generated content. Over time, advancements in NLP techniques and machine learning algorithms have enhanced the accuracy and scalability of sentiment analysis models.

Prominent Sentiment Analysis Models

VADER (Valence Aware Dictionary and sEntiment Reasoner): Developed by researchers at Georgia Tech, VADER is a lexicon and rule-based sentiment analysis tool that assigns sentiment scores to text based on a predefined sentiment lexicon. VADER is known for its simplicity and effectiveness in capturing sentiment nuances.

LSTM (Long Short-Term Memory) Networks: LSTM networks, a type of recurrent neural network (RNN), have been widely used for sequence modeling and sentiment analysis tasks. Their ability to capture long-term dependencies in text data makes them well-suited for sentiment classification.

BERT (Bidirectional Encoder Representations from Transformers): As mentioned, leveraging transformer architecture, BERT has revolutionized sentiment analysis by providing a deep contextual understanding of language. Fine-tuned BERT models have demonstrated state-of-the-art performance in sentiment classification tasks.

Real-Life Scenarios for Sentiment Analysis

Brand Reputation Management: Companies use sentiment analysis to monitor online mentions and customer feedback, allowing them to proactively address issues, track brand sentiment, and enhance customer satisfaction.

Market Research and Consumer Insights: Market researchers leverage sentiment analysis to analyze product reviews, social media conversations, and surveys to understand consumer preferences, trends, and sentiments towards products or services.

Social Media Monitoring: Sentiment analysis tools are employed to track social media sentiment towards brands, events, or public figures. Social media monitoring helps organizations gauge public opinion, identify trends, and respond to emerging issues.

Understanding language subtleties and context is a big challenge in sentiment analysis. Models must effectively capture subtle elements like sarcasm, irony, and context-specific sentiments to improve accuracy by considering linguistic variations and cultural diversity. Advancements in deep learning, transfer learning, and multimodal sentiment analysis are reshaping the landscape of sentiment analysis, making it more versatile and accurate than ever before.

AI Check of Customer Reviews Using Python and the Natural Language Toolkit (NLTK) Library

Analyzing Sentiment in Movie Reviews

In the digital age, the abundance of user-generated content, particularly in the form of online reviews and opinions, presents a wealth of valuable data that organizations can leverage to understand customer sentiment. Sentiment analysis offers a solution for extracting insights from text data to gauge public opinion, make informed decisions, and enhance customer experiences. Let's delve into a practical application of sentiment analysis using Python and machine learning techniques in the realm of movie reviews.

Scenario: An online streaming service is keen on understanding user sentiments towards movies and TV shows available on their platform to enhance content recommendations and user engagement.

By implementing sentiment analysis on movie reviews, the streaming service can analyze user feedback, identify popular content, and discover areas for improvement. Through sentiment analysis, the service can categorize reviews as positive or negative, enabling them to tailor content recommendations based on user sentiment. This data-driven approach empowers the streaming service to personalize user experiences, boost viewer satisfaction, and drive content retention.

The Code

The code provided showcases the implementation of sentiment analysis on the popular NLTK movie reviews dataset. This code employs a Multinomial Naive Bayes classifier, a well-known machine learning algorithm that excels in text classification tasks.

  1. Data Preparation: The movie reviews are loaded from the NLTK corpus, and the text data is processed to convert it into numerical features using CountVectorizer.
  2. Model Training: A Multinomial Naive Bayes model is trained on the processed data to predict sentiment labels (positive or negative) based on the text content.
  3. Model Evaluation: The accuracy of the trained model is assessed by predicting sentiment labels on test data and calculating the accuracy score.
  4. Model Persistence: The trained model and the vectorizer used for numerical conversion are saved to disk for future reuse.

Ensure Required Libraries are Installed:

pip install nltk scikit-learn flask        

https://www.nltk.org/

https://scikit-learn.org/stable/

https://flask.palletsprojects.com/en/3.0.x/


The model train/test full code in "sentiment.py":

import nltk
import random
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.metrics import accuracy_score
import pickle
import numpy as np

# Download necessary NLTK data
nltk.download('movie_reviews')
nltk.download('punkt')

from nltk.corpus import movie_reviews

# Load movie reviews from NLTK corpus
documents = [(list(movie_reviews.words(fileid)), category)
             for category in movie_reviews.categories()
             for fileid in movie_reviews.fileids(category)]

random.shuffle(documents)

# Convert the list of words to a single string for each document
data = [" ".join(words) for words, category in documents]
labels = [category for words, category in documents]

# Split data into training and testing sets
data_train, data_test, labels_train, labels_test = train_test_split(data, labels, test_size=0.2, random_state=1)

# Convert text data to numerical data using CountVectorizer
vectorizer = CountVectorizer()
train_features = vectorizer.fit_transform(data_train)
test_features = vectorizer.transform(data_test)

# Train the Multinomial Naive Bayes model
model = MultinomialNB()
model.fit(train_features, labels_train)

# Evaluate the model
predictions = model.predict(test_features)
accuracy = accuracy_score(labels_test, predictions)
print(f'Accuracy: {accuracy * 100:.2f}%')

# Save the model and vectorizer for later use
with open('models/sentiment_model.pkl', 'wb') as model_file:
    pickle.dump(model, model_file)

with open('models/vectorizer.pkl', 'wb') as vec_file:
    pickle.dump(vectorizer, vec_file)        

Compile it!

You will notice that accuracy is 83.75%.

An accuracy of 83.75% in sentiment analysis can be considered a reasonably good result, especially in the context of text classification tasks. However, whether this accuracy level is deemed adequate depends on the specific requirements of the application and the expectations for the sentiment analysis model.

Factors to Consider:

  • Domain and Application Context: Consider the specific industry or domain where the sentiment analysis model will be deployed. Some industries may require higher accuracy levels for decision-making.
  • Use Case and Impact: Assess the impact of misclassifications. In applications where misclassifications have significant consequences, a higher accuracy may be necessary.
  • Baseline Performance: Compare the achieved accuracy against baseline performances or industry standards. An accuracy of 83.75% may be high or low relative to other sentiment analysis models in the same domain.

Considerations for Improvement:

  • Fine-tuning and Optimization:? Further model fine-tuning, hyperparameter optimization (please check my article ), or using more advanced algorithms may help improve accuracy.
  • Ensemble Methods: Consider using ensemble methods or combining multiple models to enhance predictive performance.
  • Feature Engineering: Exploring additional features or incorporating more advanced text processing techniques could lead to improved accuracy.

Development of application that will utilize the trained sentiment analysis model

Wouldn't it be cool if I made an application that would work with our trained model and enable sentiment analysis for the input we provide?

Let’s start!

The compile action will create two files in the “'models” directory (right beside your python file):

The files “sentiment_model.pkl” and “vectorizer.pkl” are used for saving and storing the trained sentiment analysis model and the CountVectorizer object, respectively, in Python. These files enable the model to be saved, reused, and deployed efficiently for sentiment analysis tasks, streamlining the process of text classification and enhancing the consistency and performance of sentiment analysis applications. Here's an explanation of each file:

sentiment_model.pkl:

This file stores the trained sentiment analysis model, in this case, a Multinomial Naive Bayes classifier, after it has been trained on the movie reviews dataset. Inside “sentiment_model.pkl” is the serialized form of the trained model, which includes the algorithm's internal state, parameters, and the learned patterns from the training data.

By saving the model in a pickle file, it can be easily loaded and reused in future applications without the need to retrain the model each time. Storing the model enables quick deployment, replication across different environments, and seamless integration into other Python scripts or applications for sentiment analysis tasks.

vectorizer.pkl:

This file saves the CountVectorizer object used to convert text data into numerical features for model training and prediction. Within “vectorizer.pkl” is the serialized form of the CountVectorizer object, which includes the vocabulary mapping and other settings used during the text-to-numerical transformation.

Storing the vectorizer allows for consistency in feature extraction when applying the trained model to new text data for sentiment analysis. By saving and reusing the vectorizer, the same text preprocessing steps can be applied consistently to new data, ensuring compatibility with the model's input requirements and maintaining accuracy in sentiment predictions.

Web Application

Let's create a simple web application using Flask to serve our trained sentiment analysis model. This way, you can input text and get real-time sentiment analysis results via a web interface.

Flask is a lightweight and flexible web framework for Python that is designed for building web applications quickly and with minimal overhead. Known for its simplicity and ease of use, Flask provides the essential tools to create web applications, such as URL routing, request handling, and template rendering, without imposing a specific structure or requiring extensive boilerplate code. Due to its minimalist core, developers have the freedom to extend Flask with a wide array of extensions to add functionality like database integration, form validation, and authentication. This makes Flask an excellent choice for both beginners and advanced developers who need to build scalable, customizable web applications or APIs efficiently.

https://flask.palletsprojects.com/

Create directories and files to work with our model as shown below. If you created “sentiment_model.pkl” and “vectorizer.pkl” files outside the “models” directory, move them there.

Full directory structure for Flask application:

Backend ("app.py")

"app.py"-typically serves as the backend of a web application when using Flask. In a Flask application, backend contains the server-side code that handles HTTP requests, processes data, interacts with databases, and returns appropriate HTTP responses, often rendering HTML templates for the frontend or sending JSON data in the case of APIs. In our implementation, this backed enables communication between the model and the frontend.

import os
from flask import Flask, request, render_template
import pickle

# Load the trained model and vectorizer
MODEL_DIR = os.path.join(os.path.dirname(__file__), 'models')

model_path = os.path.join(MODEL_DIR, 'sentiment_model.pkl')
vectorizer_path = os.path.join(MODEL_DIR, 'vectorizer.pkl')

with open(model_path, 'rb') as model_file:
    model = pickle.load(model_file)

with open(vectorizer_path, 'rb') as vec_file:
    vectorizer = pickle.load(vec_file)

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/predict', methods=['POST'])
def predict():
    if request.method == 'POST':
        message = request.form['message']
        data = [message]
        vectorized_input = vectorizer.transform(data)
        probabilities = model.predict_proba(vectorized_input)
        
        # Get probabilities for each class
        prob_positive = probabilities[0][1]
        prob_negative = probabilities[0][0]
        
        # Calculate a simple sentiment score
        sentiment_score = prob_positive - prob_negative
        
        # Determine the sentiment
        if sentiment_score > 0.1:
            result = 'positive'
        elif sentiment_score < -0.1:
            result = 'negative'
        else:
            result = 'neutral'
        
        return render_template('index.html', 
                               prediction_text=f'Sentiment: {result}', 
                               score_text=f'Score: {sentiment_score:.2f}')

if __name__ == '__main__':
    app.run(debug=True)        

Key Points Explained:

Loading the Model and Vectorizer:pickle.load” is used to deserialize and load the pre-trained sentiment analysis model and vectorizer from files located in the “models” directory.

Home Route: The “home” function renders the “index.html” template when the root URL (“/”) is accessed.

Predict Route: The “predict” function handles POST requests:

  • Extracts the user input message.
  • Transforms it using the vectorizer.
  • Predicts sentiment probabilities with the model.
  • Computes a sentiment score.
  • Determines sentiment as positive, negative, or neutral.
  • Renders the “index.html” template with the prediction results.

Run the Flask Application: The Flask application is run in debug mode, which is useful for development and debugging.

Frontend (index.html)

In the structure of a Flask application, the "index.html" file serves as the frontend component that users interact with through their web browsers. This HTML file is placed in the templates directory, adhering to Flask’s default convention for organizing templates.

The "index.html" file will be our homepage for the Flask web application. It provides a user interface where users can input their text (e.g., movie reviews) and submit it for sentiment analysis. The file is rendered by the Flask app when the user navigates to the root URL (/).

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sentiment Analysis</title>
</head>
<body>
    <h1>Sentiment Analysis</h1>
    <form action="/predict" method="POST">
        <textarea name="message" rows="4" cols="50" placeholder="Enter your text here..."></textarea><br><br>
        <input type="submit" value="Analyze Sentiment">
    </form>

    {% if prediction_text %}
        <h2>{{ prediction_text }}</h2>
        <h3>{{ score_text }}</h3>
    {% endif %}
</body>
</html>        

Key Points Explained:

Form for Input: Form sends data to /predict route via POST method when submitted.

?<form action="/predict" method="POST">
    <textarea name="message" rows="4" cols="50" placeholder="Enter your text..."></textarea><br><br>
    <input type="submit" value="Analyze Sentiment">
</form>        

Display Results: Conditionally renders the prediction and score if available, using Jinja2 template syntax.

?{% if prediction_text %}
    <h2>{{ prediction_text }}</h2>
    <h3>{{ score_text }}</h3>
{% endif %}        

Running the Flask Web Application

Navigate to the “flask_app” directory in your terminal and run the Flask application with:

python app.py        

The app is running on https://127.0.0.1:5000

Open the link in your browser, and the web app to test sentiment based on your input will load.

Testing the Model

Let’s try the following examples in our web app:

a) Neutral Example Input

“The movie was okay. It had some good moments, but it wasn't very memorable. Just average.”        

Expected Output:

Sentiment: neutral

Score: within -0.1 to 0.1 range


b) Positive Example Input

“I was blown away by the storytelling and the visuals. The actors delivered an amazing performance. One of the best movies I've seen this year!”        

Expected Output:

Sentiment: positive

Score: greater than 0.1


c) Negative Example Input

“I found the movie boring and predictable. The characters were flat, and the dialogue was cringe-worthy. Not worth the time.”        

Expected Output:

Sentiment: negative

Score: less than -0.1


Using Python and the NLTK library, we created a sentiment analysis model that accurately classifies movie reviews as positive, negative, or neutral. This model, powered by machine learning, can be deployed through a Flask web application, providing a seamless way for users to input text and receive real-time sentiment feedback. The integration of sentiment analysis into a web app demonstrates how AI can be effectively used to enhance user experiences and make informed decisions based on customer feedback.

Automation of Sentiment Analysis of Multiple Customer Reviews

Let's explore another scenario for using sentiment analysis. In practice, it is practical to automate the analysis of user comments and gain quick insight into their opinions about a product, service, or content.

Let's develop a simple application that will use our trained model to check reviews related to the performance of one of the representatives at the Eurovision Song Contest 2024 (#BabyLasagna #RimTimTagiDim).

https://youtu.be/YIBjarAiAVc?si=fcuRRyCLMwK5SHaw

The resulting report will provide a graphical and detailed analysis of the audience's sentiment regarding performance.

The following file in the JSON format contains user reviews:

customer_reviews.json

{
  "reviews": [
    {
      "reviewer": "EurovisionFan91",
      "comment": "Baby Lasagna delivered a stunning and heartfelt performance of 'Rim Tim Tagi Dim.' His vocals were powerful and the emotional storytelling resonated with audiences across Europe."
    },
    {
      "reviewer": "MusicCriticDaily",
      "comment": "Marko, aka Baby Lasagna, gave Croatia its best Eurovision result in history, and deservedly so. His stage presence and the unique narrative of his song were simply unforgettable."
    },
    {
      "reviewer": "EurovisionWeekly",
      "comment": "'Rim Tim Tagi Dim' stood out for its originality and emotional depth. Baby Lasagna's performance was both captivating and nostalgic, winning over the public vote effortlessly."
    },
    {
      "reviewer": "PopMusicReviews",
      "comment": "One of the standout performances of the night! Baby Lasagna's charisma and the heartfelt message of his song made for a truly memorable Eurovision experience."
    },
	{
      "reviewer": "DeafHrtJury",
      "comment": "Baby Lasagna's performance was nice but didn't leave a lasting impression on me."
    },
    {
      "reviewer": "EuroVisionaries",
      "comment": "A fantastic entry from Croatia with Baby Lasagna at the helm. His genuine emotion and connection with the audience were palpable, making 'Rim Tim Tagi Dim' a highlight of the contest."
    },
    {
      "reviewer": "Euro_Tunes",
      "comment": "Innovative and compelling, Baby Lasagna's song had a way of drawing you into its story. His performance was heartfelt and left a lasting impression."
    },
    {
      "reviewer": "MusicSpectrum",
      "comment": "Baby Lasagna's 'Rim Tim Tagi Dim' was a breath of fresh air at Eurovision. His sincerity and the nostalgic lyrics struck a chord with many viewers."
    },
    {
      "reviewer": "SongFestReviews",
      "comment": "Baby Lasagna encapsulated everything we love about Eurovision - unique, heartfelt, and engaging. His performance was undeniably one of the best."
    },
    {
      "reviewer": "EurosongCentral",
      "comment": "'Rim Tim Tagi Dim' was beautifully performed. Baby Lasagna's stage presence was remarkable and the song's nostalgic touch made it a crowd favorite."
    },
    {
      "reviewer": "EuroSong_Reviews33",
      "comment": "While 'Rim Tim Tagi Dim' wasn't my personal favorite, Baby Lasagna's performance was undeniably strong and it resonated well with a wide audience."
    },
    {
      "reviewer": "SongObserver",
      "comment": "Baby Lasagna’s performance was solid, though the song was a bit unusual for my taste."
    },
    {
      "reviewer": "MelodyNews",
      "comment": "I appreciated the storytelling and Baby Lasagna’s dedication. I can see why it appealed to many."
    },
    {
      "reviewer": "TunWatch",
      "comment": "The performance was good. Baby Lasagna's song had an interesting narrative which was a nice touch."
    },

    {
      "reviewer": "Music_Insight",
      "comment": "The song and performance felt a bit out of place among the more contemporary entries. Baby Lasagna's style might resonate with some, but it wasn't for me."
    },
    {
      "reviewer": "Jealous_CRoDance",
      "comment": "While some may admire the nostalgic elements, I felt 'Rim Tim Tagi Dim' lacked the strong hooks and modernity needed to stand out in this competition."
    },
    {
      "reviewer": "BeatDrop",
      "comment": "Baby Lasagna's performance fell flat for me. The song's whimsical nature didn't translate well on stage, making it one of the more forgettable acts this year."
    }
  ]
}        

Backend

The Flask application backend contains all the analytics methods and visualization ("app.py").

from flask import Flask, render_template, request
import json
import pickle
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.naive_bayes import MultinomialNB
import matplotlib.pyplot as plt
import numpy as np

app = Flask(__name__)

# Load the trained model and vectorizer
with open('models/sentiment_model.pkl', 'rb') as model_file:
    model = pickle.load(model_file)

with open('models/vectorizer.pkl', 'rb') as vec_file:
    vectorizer = pickle.load(vec_file)

@app.route('/')
def sentiment_analysis():
    with open('customer_reviews.json', 'r') as file:
        reviews_data = json.load(file)

    sentiments = []
    thresholds = {
        'strong_positive': 0.8,
        'positive_high': 0.8,
        'positive_low': 0.5,
        'neutral_low': -0.5,
        'neutral_high': 0.5,
        'negative_high': -0.5,
        'negative_low': -0.8,
        'strong_negative': -8.0
    }

    sentiment_distribution = {
        'strong_positive': 0,
        'positive': 0,
        'neutral': 0,
        'negative': 0,
        'strong_negative': 0
    }

    for review_entry in reviews_data['reviews']:
        review_text = review_entry['comment']
        review_vectorized = vectorizer.transform([review_text])
        
        prob_array = model.predict_proba(review_vectorized)[0]
        prob_positive = prob_array[1]
        prob_negative = prob_array[0]
        
        sentiment_score = prob_positive - prob_negative

        if sentiment_score > thresholds['positive_high']:
            result = 'strong_positive'
        elif thresholds['positive_low'] < sentiment_score <= thresholds['positive_high']:
            result = 'positive'
        elif thresholds['neutral_low'] < sentiment_score <= thresholds['neutral_high']:
            result = 'neutral'
        elif thresholds['negative_low'] < sentiment_score <= thresholds['negative_high']:
            result = 'negative'
        else:
            result = 'strong_negative'

        sentiments.append({'reviewer': review_entry['reviewer'], 'comment': review_text, 'sentiment': result, 'sentiment_score': sentiment_score})

        # Update sentiment distribution
        sentiment_distribution[result] += 1

    # Ensure all categories are present in sentiment_distribution
    for sentiment_type in ['strong_positive', 'positive', 'neutral', 'negative', 'strong_negative']:
        if sentiment_type not in sentiment_distribution:
            sentiment_distribution[sentiment_type] = 0

    # Generate a bar graph for sentiment distribution
    labels = sentiment_distribution.keys()
    values = sentiment_distribution.values()
    x = np.arange(len(labels))

    plt.bar(x, values, color=['green', 'lightgreen', 'lightgray', 'lightcoral', 'lightsalmon'])
    plt.xticks(x, labels)
    plt.xlabel('Sentiment')
    plt.ylabel('Count')
    plt.title('Sentiment Distribution')

    plt.savefig('static/sentiment_distribution_bar.png')

    return render_template('sentiment_analysis.html', sentiments=sentiments)

if __name__ == '__main__':
    app.run(debug=True)        

The main elements of the provided code:

1. Imports: Various modules and packages are imported.

  • ?“Flask” and related functions are imported for creating a web application.
  • ?“json” is used for handling JSON data.
  • ?“pickle” is used for loading serialized Python objects.
  • ?From “sklearn”, “CountVectorizer” and “MultinomialNB” are imported for text vectorization and applying a Naive Bayes classifier.
  • “matplotlib.pyplot” and “numpy” are used for plotting and numerical operations.

2. Flask App Initialization: The Flask application is instantiated, preparing it to handle incoming web requests.

3. Loading the Model and Vectorizer: The pre-trained sentiment analysis model and the text vectorizer are loaded from files using pickle.

4. Route Definition: The main route (“/”) for the application is defined. This route will execute the sentiment analysis logic whenever it is accessed.

5. Loading Customer Reviews: The application loads customer reviews from a JSON file to process them.

6. Sentiment Analysis: For each review, the comment text is vectorized to match the format expected by the model. The model predicts the probability of the review being positive or negative. Sentiment scores are calculated and reviews are categorized into different sentiment types (e.g., strong positive, positive, neutral, negative, strong negative) based on predefined thresholds. A list of sentiments and a distribution of sentiment types are maintained.

7. Sentiment Distribution Plot: A bar graph illustrating the distribution of different sentiment categories is generated using “matplotlib”. This graph is saved as an image.

8. Rendering the Template: The results, including detailed sentiments and the sentiment distribution graph, are rendered in an HTML template and displayed on a web page.

9. Running the Flask App: When the script is executed directly, the Flask app runs in debug mode, ready to serve requests.

Frontend

The Flask application frontend is designed to present the results of sentiment analysis visually, including a bar chart for sentiment distribution and detailed sentiment information for individual reviews.

"sentiment_analysis.html"

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Sentiment Analysis Results</title>
    <style>
        .strong_positive {
            background-color: green;
            color: white;
            padding: 5px;
            display: inline-block;
        }
        .positive {
            background-color: lightgreen;
            color: black;
            padding: 5px;
            display: inline-block;
        }
        .neutral {
            background-color: lightgray;
            color: black;
            padding: 5px;
            display: inline-block;
        }
        .negative {
            background-color: lightcoral;
            color: black;
            padding: 5px;
            display: inline-block;
        }
        .strong_negative {
            background-color: lightsalmon;
            color: black;
            padding: 5px;
            display: inline-block;
        }
    </style>
</head>
<body>
    <h1>Sentiment Analysis Results</h1>
    <img src="{{ url_for('static', filename='sentiment_distribution_bar.png') }}" alt="Sentiment Distribution Bar Chart">
    <ul>
        {% for entry in sentiments %}
            <li><strong>Reviewer:</strong> {{ entry['reviewer'] }}</li>
            <li><strong>Comment:</strong> {{ entry['comment'] }}</li>
            <li><strong>Sentiment Score:</strong> {{ '%.2f' % entry['sentiment_score'] }}</li>
            <li><strong>Sentiment:</strong> <span class="{{ entry['sentiment'] }}">{{ entry['sentiment'] }}</span></li>
            <br>
        {% endfor %}
    </ul>
</body>
</html>        

Explanation of the main elements in the provided HTML code:

1. Document Structure: Outlines the structure and content of a webpage that presents sentiment analysis results. It consists of standard HTML tags such as <html>, <head>, and <body>, which together form the skeleton of the webpage.

2. CSS Styling: Inside the <style> tag, there are CSS classes that define the styles for different sentiment categories. Each class specifies background color, text color, padding, and display properties.

3. “<body>” Section: Contains the visible content of the webpage. An image element that displays the sentiment distribution bar chart. The “src” attribute uses Jinja2 templating syntax to dynamically include the image from the static directory.

4. Jinja2 Template Syntax: The code uses Jinja2 templating syntax to dynamically insert content generated by the Flask backend. A for-loop that iterates over each sentiment entry. The use of Jinja2 templating allows seamless integration of data from the Flask backend.

The files must be created in the following directory structure:

Note that the file "sentiment_distribution_bar.png" will be generated by the "app.py" backend.

The resulting web page will look like this:

Sentiment analysis is a powerful tool that helps us understand emotions and opinions expressed in text, making it invaluable for businesses and organizations. It simplifies the complex task of interpreting human emotions from text, offering a reliable way to gauge public sentiment. Whether it’s for improving customer satisfaction, tracking brand reputation, or understanding market trends, sentiment analysis provides valuable insights that drive better outcomes and deeper connections in today’s digital landscape.



Neven Dujmovic, May 2024


#ArtificialIntelligence #AI #DataScience #MachineLearning #WebDevelopment #Flask #Python #SentimentAnalysis #json


要查看或添加评论,请登录

社区洞察

其他会员也浏览了