DEV Community

J Mungai
J Mungai

Posted on

GETTING STARTED WITH SENTIMENT ANALYSIS

Sentiment analysis is a Natural Language Processing technique used in determining the emotional tone or attitude behind a piece of text. This is also known as opinion mining.
For example, an organization's digital space has opinions form their clients. It is important for the organization to se these opinions to get insights about their products and services. To effectively analyze this data, the organization gathers all the opinions in one place and applies sentiment analysis to it, since going through all the data manually is almost next to impossible.
Use cases for sentiment analysis include:

  1. Social media monitoring for brand management

  2. Product/ service analysis

  3. Stock price prediction

  4. Customer feedback analysis

Several libraries are available in python for sentiment analysis. They include:

1. Natural Language Toolkit (NLTK): NLTK is popular for natural language processing in python. It provides a variety of tools for text analysis, including pre-trained sentiment analysis models like VADER.
2. TextBlob: This is a library for text processing and sentiment analysis. It provides a simple API for sentiment analysis an also includes features like part-of-speech tagging and noun phrase extraction.
3. SpaCy: SpaCy provides tools for tokenization, part-of-speech tagging and dependency parsing, as well as pre-trained models for sentiment analysis
4. Scikit Learn: It is a popular machine learning library in python. it provides a variety of tools for text analysis including algorithms for sentiment analysis, like Naïve Bayes and Support Vector Machines.
5.Tensorflow and Keras: These provide tools for building and training deep learning models which can be used for sentiment analysis tasks.

There are several approaches to sentiment analysis, each with its own strengths and weaknesses. The best approach depends on specific needs of the application. They include:

1. Role-Based Approach: This approach relies on manually crafted rules or lexicons to identify sentiment in text. It is based on the assumption that certain words and phrases are inherently positive or negative. For example, the word "happy" is generally considered positive, while the word "sad" is considered negative.
This approach is often relatively simple and transparent but can be limited by specificity and comprehensiveness of the lexicons or rules used.
2. Machine-Learning Approach: Involves training a model on a labelled dataset of text and sentiment. The model uses this training to predict the sentiment of new text. Machine learning approach can be highly accurate but requires a large amount of labelled data to train the model effectively. Common algorithms include Naive Bayes, Support Vector Machines and neural networks.
3. Hybrid Approach: Combines both rule-base and machine-learning approaches.
For example: A hybrid approach might use a lexicon of sentiment-bearing words to identify sentiment in text and then use a machine-learning model to fine tune the sentiment analysis based on context. This approach provides the best of both worlds, but can be complex and difficult to implement.
4. Deep-Learning Approach: Involves training a neural network to learn the representation of text and sentiment. These models can learn complex relationships between words and are often highly accurate. They however require a large amount of labelled data and can be computationally expensive to train.
5. Lexicon-Based Approach: This is a rule-based approach that uses a pre-built sentiment lexicon to determine the sentiment of a text. A sentiment lexicon is a collection of words or phrases with their corresponding polarity, i.e. negative or positive. The sentiment of a text is determined by counting the number of positive and negative words in the text. This approach is fast and easy to implement, but can be limited by the size and quality of the sentiment lexicon used.

Below is an example of sentiment analysis using the twitter dataset on Kaggle:

Load the dataset into a Pandas DataFrame

import pandas as pd
df = pd.read_csv('entity_sentiment_twitter.csv')
Enter fullscreen mode Exit fullscreen mode

Print the first 5 rows of the DataFrame

print(df.head())
Enter fullscreen mode Exit fullscreen mode
                                                text     entity  sentiment
0  I'm excited to share my new course on @kaggle ...     Kaggle   Positive
1  @elonmusk thanks for the Tesla. Can't believe ...  @elonmusk   Positive
2     @SpotifyCares I need help with my account pls.   SpotifyC   Negative
3  Had a great experience with @Apple customer s...      Apple   Positive
4            My favorite game is @PlayHearthstone\n  Hearth...   Positive

Enter fullscreen mode Exit fullscreen mode
import re

def clean_text(text):
    # Remove URLs
    text = re.sub(r'http\S+', '', text)

    # Remove hashtags and mentions
    text = re.sub(r'#\w+', '', text)
    text = re.sub(r'@\w+', '', text)

    # Remove special characters and punctuation
    text = re.sub(r'[^\w\s]', '', text)

    # Convert to lowercase
    text = text.lower()

    return text

# Apply the clean_text function to the 'text' column of the DataFrame
df['clean_text'] = df['text'].apply(clean_text)

Enter fullscreen mode Exit fullscreen mode
from textblob import TextBlob

def get_sentiment(text):
    # Create a TextBlob object from the text
    blob = TextBlob(text)

    # Get the polarity score (-1 to 1)
    polarity = blob.sentiment.polarity

    # Classify the sentiment as positive, negative, or neutral based on the polarity score
    if polarity > 0:
        sentiment = 'Positive'
    elif polarity < 0:
        sentiment = 'Negative'
    else:
        sentiment = 'Neutral'

    return sentiment

# Apply the get_sentiment function to the 'clean_text' column of the DataFrame
df['predicted_sentiment'] = df['clean_text'].apply(get_sentiment)

Enter fullscreen mode Exit fullscreen mode
# Calculate the accuracy of the sentiment analysis
accuracy = (df['sentiment'] == df['predicted_sentiment']).mean()

print(f'Accuracy: {accuracy:.2%}')

Enter fullscreen mode Exit fullscreen mode
Accuracy: 69.58%

Enter fullscreen mode Exit fullscreen mode

Top comments (0)