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
- Python 3
- An iPython notebook editor such as Jupyter
- A passage of text to analyze
- Example code in The Art of Literary Text Analysis
Exercise Steps
- Setup the Data
- Create a string containing the passage to be analyzed
- Create a list of positive words
- Create a list of negative words
- Tokenize the Text
- Import the re library in Python
- Use the re.findall() method to tokenize the text
- 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
- 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
- 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.
- Create a series of if/else statements:
Further Information
- Try repeating this process for a database of passages instead of just analyzing a single one.
- This example is based on Neal Caron's An introduction to text analysis with Python, Part 1.
- This example is also available in The Art of Literary Text Analysis.
- Look at the General Sentiment Analysis method.
Submitted by Jinman on Sat, 04/08/2017 - 10:43