- by Buu Thong Tran -
You may scratch your head, trying to know how your brand is perceived in the market. Conventional approach no longer works for you. Now, it is time for you to try new things. In this tutorial, I will teach you how to use python and Twitter as a tool to understand what people think about your brand.
pip is already installed on your operating system, you can use pip to install neccessary libraries and import them. In your command prompt, type
pip install tweepy textbloband
python -m textblob.download_corpora
import tweepy
from textblob import TextBlob
You need to create a Twitter app, then get its consumer key, consumer secret key, access token, access token secret to call API.
consumer_key = 'cRPQUFuKBvvWKCCIuawJvIy7h'
consumer_secret = 'eG2LsdIuBa2o53QADUAVfsz2IPA4X150hihWIx4I5wIxfB7SAz'
access_token = '841368199-gx50VJVJSgnHVw8ScGmEWtgEy6gtLGJf0a5j70oN'
access_token_secret = '5zrzFU9d9z2Vt9RvWvU40TNN0EM7DypgPXhz5O4Es6sj1'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
Now, this is the most interesting part; you will search for what people talked about your brand on Twitter, and score them by using TextBlob’s sentiment analysis. Let’s say your brand is “Microsoft” and your competitor is “Apple”.
me = 'Microsoft'
competitor = 'Apple'
# Query tweets that mention me
public_tweets_me = api.search(me)
my_score = 0
for tweet in public_tweets_me:
#print tweet.text
analysis = TextBlob(tweet.text)
#print analysis.sentiment
# Polarity ranges between -1 and 1. A tweet that has a positive sentiment will give a positive score.
# Subjectivity ranges between 0 and 1. A tweet that has a high subjectivity means its opinion is not based on fact.
# Therefore, its polarity has low impact on the total score of your brand.
my_score += analysis.sentiment.polarity / 10**(analysis.sentiment.subjectivity)
print '\n', me, my_score/len(public_tweets_me), '\n'
# Query tweets that mention my competitor
public_tweets_competitor = api.search(competitor)
competitor_score = 0
for tweet in public_tweets_competitor:
#print tweet.text
analysis = TextBlob(tweet.text)
#print analysis.sentiment
competitor_score += analysis.sentiment.polarity / 10**(analysis.sentiment.subjectivity)
print '\n', competitor, score/len(public_tweets_competitor)
Microsoft 0.0842526390674
Apple 0.0110057297059
The scores above are calculated based on 15 most recent tweets that mention their brands. The more data you get from Twitter, the more you understand how your brand is perceived, compared to other brands, in the market. I recommend that you conduct real time analysis by using this technique so that you can manage your brand more effectively.
TextBlob: Simplified Text Processing