Simple Sentiment Analysis Using Python
Introduction
This recipe shows how to analyze the sentiment of a simple passage of text, such as a tweet.
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
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:
Status
Submitted by GregWS on Sun, 09/25/2016 - 22:08