What is analysis moods?
Sentiment Analysis — it is the process of "computer" determining whether a piece of email is positive, negative, or neutral. This is also known as opinion analysis , getting the opinion or position of the speaker.
Why sentiment analysis?
- Business: In marketing, companies use it to develop their strategies, to understand how consumers feel about a product or brand, how people react to their campaigns or product launches, and why consumers aren’t buying some of them.
- Politics. In the political sphere, it is used to track political views, identify consistency and inconsistencies between statements and actions at the government level. It can also be used to predict election results!
- Public Action: Sentiment analysis is also used to monitor and analyze social phenomena, identify potentially dangerous situations and determine the overall sentiment of the blogosphere.
- Tweepy: tweepy — Python client for the official Twitter API .
Install it using the following pip command:
pip install tweepy
- TextBlob: textblob — it is a python library for processing text data.
Install it using the following pip command:
pip install textblob
We also need to install multiple NLTK packages using the following command:
python -m textblob.download_corpora
(The corporation — is nothing else, as a large and structured set of texts.)
In order to receive tweets through the Twitter API, you need to register for the app with your Twitter account. Follow these steps for the same:
- Open this link and click the "Create New App" button
- Fill in the application details. You can leave the Callback URL blank.
- Once the application is created, you will be redirected to the application page.
- Click the Access Keys and Tokens tab.
- Copy the "Consumer Key", "Consumer Secret", "Access Token" and "Access Token Secret".
This is what the example output looks like when running the above programs:
Positive tweets percenta ge: 22% Negative tweets percentage: 15% Positive tweets: RT @JohnGGalt: Amazing — after years of attacking Donald Trump the media managed to turn #InaugurationDay into all about themselves. # MakeAme… RT @ vooda1: CNN Declines to Air White House Press Conference Live YES! THANK YOU @CNN FOR NOT LEGITIMI ... RT @Muheeb_Shawwa: Donald J. Trump’s speech sounded eerily familiar ... POTUS plans new deal for UK as Theresa May to be first foreign leader to meet new president since inauguration. @ Realdonaldtrump #Syria #Mexico #Russia & amp; now #Afghanistan. Another #DearDonaldTrump Letter worth a read @AJEnglish Negative tweets: RT @Slate: Donald Trump’s administration: “Government by the worst men.” RT @RVAwonk: Trump, Sean Spicer, et al. lie for a reason. Their lies are not just lies. Their lies are authoritarian propaganda. RT @KomptonMusic: Me: I hate corn Donald Trump: I hate corn too Me: https://t.co/GPgy8R8HB5 It’s ridiculous that people are more annoyed at this than Donald Trump’s sexism. RT @tony_broach: Chris Wallace on Fox news right now talking crap about Donald Trump news conference it seems he can’t face the truth eithe ... RT @fravel: With False Claims, Donald Trump Attacks Media on Crowd Turnout Aziz Ansari Just Hit Donald Trump Hard In An Epic Saturday NIght Live Monologue
We follow these 3 basic steps in our program:
- Authorize Twitter API client.
- Make a GET request to the Twitter API to get tweets for a specific request.
- Parse tweets. Classify each tweet as positive, negative or neutral.
Now let’s try to understand the above code snippet:
- First of all, we create a class TwitterClient . This class contains all the methods for interacting with the Twitter API and parsing tweets. We use the __init__ function to handle API client authentication.
- In the get_tweets function, we use:
fetched_tweets = self.api.search (q = query, count = count)
call the Twitter API to get tweets.
- In get_tweet_sentiment we use the textblob module.
analysis = TextBlob (self.clean_tweet (tweet))
TextBlob — it is a high-level library built on top of the NLTK library. First, we call the clean_tweet, method to remove links, special characters, etc. from the tweet using some simple regex.
Then, when we submit a Tweet to create a TextBlob object, the following processing is done on the text by the textblob library:
- Tokenize a tweet, that is, separate words by text.
- Remove stop words from tokens (stop words — are commonly used words that are not relevant to text analysis, such as me, me, you, etc.)
- Perform POS (part of speech) token labeling and select only important tokens / tokens like adjectives, adverbs, etc.
- Pass tokens to a sentiment classifier which classifies the sentiment in a tweet as positive, negative or neutral, assigning it a polarity of -1.0 to 1.0.
Here’s how a sentiment classifier is created:
- TextBlob uses the Movie Reviews dataset, where reviews are already flagged as positive or negative. Li>
- Positive and negative traits are extracted from each positive and negative feedback, respectively.
- Training data now consists of flagged positive and negative traits. This data is trained naive Bayes classifier .
Then we use sentiment.polarity class method TextBlob get the polarity of the tweet from -1 to 1.
Then we classify the polarity as:
if analysis.sentiment.polarity" 0: return ’positive’ elif analysis.sentiment.polarity == 0: return’ neutral’ else: return ’negative’
- Finally, parsed tweets are returned. We can then do various kinds of statistical analysis of the tweets. For example, in the above program, we tried to find the percentage of positive, negative, and neutral tweets about a request.
- http://www.ijcaonline.org/research/volume125/number3/dandrea-2015-ijca -905866.pdf
- https://textblob.readthedocs.io /en/dev/quickstart.html#sentiment-analysis
This article courtesy of Nikhil Kumar . If you like Python.Engineering and would like to contribute, you can also write an article using contrib.python.engineering, or email your article to [email protected] See my article appearing on the Python.Engineering homepage and help other geeks.
Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.