Sentiment Analysis with Python: A Comprehensive Guide

In this article, we’ll learn how to perform sentiment analysis in Python and tools that can be used for this task including NLTK, VADER, TextBlob, PyTorch, and OpenAI.
by Josephine Loo · May 2024

Contents

    Sentiment analysis, also known as opinion mining, is a technique used to understand people’s opinions, emotions, and attitudes. It’s a great tool for businesses to gain insights, spot market trends, and analyze customer feedback, which helps in making better decisions.

    Given Python's widespread use in web scraping, sentiment analysis has become a hot topic in the Python community. In this article, we’ll learn more about sentiment analysis and how it can be performed using Python.

    What is Sentiment Analysis

    Sentiment analysis is a technique that deciphers the emotional undertones of a text to understand the sentiments, attitudes, and emotions conveyed. It employs natural language processing (NLP), text analysis, and computational linguistics to identify and extract subjective information from the text.

    The sentiment of a text is typically classified as positive, negative, or neutral. That said, some sentiment analysis methods are designed for specific use cases. For example, some sentiment analysis approaches can identify specific emotions expressed in the text, such as joy, anger, sadness, or fear. Some even consider the context of the text to provide a more accurate sentiment interpretation.

    Moreover, sentiment analysis can be customized for specific areas like social media, customer reviews, or news articles, enhancing its accuracy and relevance. This tailoring allows for a more nuanced understanding of sentiments in different domains.

    Obtaining Data for Sentiment Analysis

    There are several methods to obtain data for sentiment analysis, depending on your specific needs. Some common methods include:

    • Web Scraping : Extracting data from websites using tools like BrowserBear.
    • APIs : Using APIs to fetch data from platforms like Twitter, Facebook, or Reddit.
    • Public Datasets : Utilizing publicly available datasets for sentiment analysis.
    • Surveys : Conducting surveys to collect data directly from users.

    How to Do Sentiment Analysis in Python

    Python provides several libraries that make it relatively easy to perform sentiment analysis and determine the sentiment expressed in text. One popular library for this task is NLTK (Natural Language Toolkit).  

    Here's a basic example of how you can perform sentiment analysis in Python using the library:

    Step 1. Install NLTK

    Run the command below in your terminal/command prompt to install the nltk library in your project directory:

    pip install nltk
    

    Note: Replace pip with pip3 if you’re using Python 3.x.

    Step 2. Import the Required Libraries

    Create a new Python file (e.g., script.py) in your project directory. Then, import the nltk library, SentimentIntensityAnalyzer class, and download the VADER lexicon which contains a list of words and their associated sentiment scores.

    import nltk
    from nltk.sentiment.vader import SentimentIntensityAnalyzer
    
    nltk.download('vader_lexicon')
    

    Step 3. Analyze Sentiment

    Create an instance of the SentimentIntensityAnalyzer class and use the .polarity_scores() method to get the sentiment scores for a given text:

    sid = SentimentIntensityAnalyzer()
    text = "I am happy!"
    scores = sid.polarity_scores(text)
    

    Then, print the sentiment scores:

    print(scores)
    

    Here’s the output when you execute the file by running python script.py or python3 script.py in the terminal/command prompt:

    {'neg': 0.0, 'neu': 0.2, 'pos': 0.8, 'compound': 0.6114}
    

    The result returned is a dictionary with four keys: 'neg', 'neu', 'pos', and 'compound'. The 'neg', 'neu', and 'pos' scores indicate the strength of each sentiment and the 'compound' score is a normalized score that ranges from -1 (most negative) to +1 (most positive).

    In this example, the text has a positive sentiment, with a 'pos' score of 0.8 and a 'compound' score of 0.6114.

    You can also use these sentiment scores to classify the overall sentiment of the text. For example, if the 'compound' score is greater than 0.05, you can classify the text as positive. If it's less than -0.05, you can classify it as negative. Otherwise, you can classify it as neutral:

    if scores['compound'] > 0.05:
        print("Positive")
    elif scores['compound'] < -0.05:
        print("Negative")
    else:
        print("Neutral")
    

    Other Sentiment Analysis Tools

    Besides NLTK, there are also other tools that you can use in Python to analyze sentiment from text. Here are some of them:

    TextBlob

    TextBlob is another popular sentiment analysis tool in Python. It is actually a Python library for processing textual data and provides a simple API for diving into common natural language processing (NLP) tasks like part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more. It's built on top of the NLTK and Pattern libraries and offers a beginner-friendly interface for basic sentiment analysis tasks.

    Besides sentiment analysis, here’s a list of other features offered by TextBlob:

    • Noun phrase extraction
    • Part-of-speech tagging
    • Classification (Naive Bayes, Decision Tree)
    • Tokenization (splitting text into words and sentences)
    • Word and phrase frequencies
    • Parsing

    VADER

    VADER (Valence Aware Dictionary and sEntiment Reasoner) is a lexicon and rule-based sentiment analysis tool that is specifically designed for analyzing sentiments expressed in text (yes, it is the same VADER that is used in the code above). It is particularly well-suited for analyzing short, informal text such as social media posts, reviews, and comments.

    VADER uses a combination of sentiment lexicon (e.g., a dictionary of words and phrases scored for sentiment polarity) and rules to determine the sentiment (positive, negative, or neutral) of a piece of text.  Not only that, it can also determine the intensity of the sentiment (how strong the sentiment is).

    PyTorch

    PyTorch is an open-source machine-learning library for Python. PyTorch, along with its ecosystem of libraries, provides the tools needed to build and train neural networks for various natural language processing (NLP) tasks, including sentiment analysis.

    PyTorch provides two high-level features—Tensor computation with strong GPU acceleration and deep neural networks built on a tape-based autograd system. You can use them to create models that analyze and classify the sentiment of text data. Besides that, PyTorch has several libraries useful for NLP tasks, such as torchtext and transformers. These libraries provide pre-built components for processing text data, tokenization, and working with popular NLP models like BERT.

    OpenAI

    Besides the popular ChatGPT, the OpenAI API offers the moderations endpoint. The "moderations" endpoint is specifically designed for content moderation tasks, such as identifying potentially sensitive or harmful content. Although it is not directly intended for sentiment analysis, it can be used to detect sentiment in a broader context of content moderation.

    The model would check whether the text is potentially harmful across several categories like “hate”, “harassment”, “violence”, etc. This helps to identify content that might be harmful to other users and allows developers to build functions in their applications to filter or remove this type of content.

    Conclusion

    Sentiment analysis is a powerful tool for gaining valuable insights from text data. With lexicon-based methods like NLTK, VADER, and TextBlob, as well as machine-learning-based methods such as OpenAI and PyTorch, Python offers a diverse set of tools for performing sentiment analysis. As technology continues to evolve, sentiment analysis will likely become even more sophisticated, offering even deeper insights into human emotions and opinions.

    About the authorJosephine Loo
    Josephine is an automation enthusiast. She loves automating stuff and helping people to increase productivity with automation.

    Automate & Scale
    Your Web Scraping

    Browserbear helps you get the data you need to run your business, with our nocode task builder and integrations

    Sentiment Analysis with Python: A Comprehensive Guide
    Sentiment Analysis with Python: A Comprehensive Guide