Simple Sentiment Analysis Using Python

Introduction 

This recipe shows how to conduct dictionary-based sentiment analysis on a collection of passages, such as tweets or reviews. It uses pre-existing dictionaries of positive and negative words, and loads a text file of passages to analyze.

Ingredients 
Exercise Steps 
  1. Setup the Data
    • Create a string containing the passage to be analyzed
    • Create a list of positive words
    • Create a list of negative words
  2. Tokenize the Text
    • Import the re library in Python
    • Use the re.findall() method to tokenize the text
  3. Count Positive and Negative Words
    • Use a for loop to go through the passage and count positive words
    • Use a for loop to go through the passage and count negative words
  4. Calculate the percentages of positive and negative words
    • Positivity: divide the number of positive words by the total number of words
    • Negativity: divide the number of negative words by the total number of words
  5. Determine Whether the Passage is Positive or Negative and Print Results
    • Create a series of if/else statements:
      • If positive words > negative words, the passage is positive.
      • If negative words > positive words, it is negative.
      • If the count is equal, the passage is neutral.
Further Information