Prashant Rohilla
3 min readJun 22, 2021

--

TWITTER SENTIMENT ANALYSIS OF DOGECOIN USING PYTHON IN 30 Line CODE

#first of all import all the necessary packages and if package not available simply download it by typing pip install (package name).then import them using below commands.

#Tweepy is an open source Python package that gives you a very convenient way to access the Twitter API with Python.(simply can access the live tweets)

import tweepy as tw

#here pandas used for importing data in form of data frames

import pandas as pd

#matplotlib as you all know for plotting

import matplotlib.pyplot as plt

# the below keys you will get by creating developer account on twitter (they will require some information what you will do with this account just type any project name and information just so you can get these tokens and keys, there is word limit you have to necessarily type).

#(Don't copy this because these are dummy keys)

Access_Token=”1255715971026653192-UX6SKiNzLm35yp2XvTnickuh0a1R1A”
Access_Token_Secret=”ygoYvHOISgXBFoVP3LTY5DVkr9KFXmi5znBjklL2ZV50g”
API_Key=”55SMQC3ZZcwD4sX7DAS8TY8iv”
API_Secret_Key=”bQ8XzAJDI1LSCmJeBpZFWF6BY1l3r784KKhl4O2TqQsuSpMYMc”

# by giving keys with these commands we are getting access to tweets by our developer account

author = tw.OAuthHandler(API_Key,API_Secret_Key)
auth.set_access_token(Access_Token,Access_Token_Secret)
api = tw.API(auth, wait_on_rate_limit=True
)

# this is the sentiment analysis of dogecoin

currency = “Dogecoin”
# to remove the retweets

search = f’#{currency}-filter:retweets’
# give date from which you want to start getting tweets about dogecoin and the last date upto which you want

start = “2021–06–14”
end = “2021–06–17”

#this command will search the the tweets and set the dates and also how many tweets we want to do analysis on(in my case it is 1000)
query = tw.Cursor(api.search,q = search,since=start,until = end).items(1000)

#this command will take only the tweet text and time at which it is tweeted by applying for loop to each tweet
tweets = [{“Tweets”:tweet.text,”Timestamp”:tweet.created_at}for tweet in query]

#can view the tweets
print(tweets)

# convert the data into data frame by pandas and give the column name Tweets

df = pd.DataFrame(tweets, columns =[“Tweets”])

#visualize the first 10 tweets
df.head(10)

# the below command will replace the unnecessary text with blank space so that there will be no problem in analysis

# first import re then apply for loop on each row using iterrows() and then replace using re.sub()

import re
for _, row in df.iterrows():
row[“Tweets”] = re.sub(“http\S+”,””,row[“Tweets”])
row[“Tweets”] = re.sub(“#\S+”,””,row[“Tweets”])
row[“Tweets”] = re.sub(“@\S”,””,row[“Tweets”])
row[“Tweets”] = re.sub(“\\n”,””,row[“Tweets”])

#first import TextBlob which is a Python (2 and 3) library for processing textual data. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis. we will calculate the polarity by using it which gives us a value on basis of what we decide the whether the tweet is positive or negative

import textblob
df[“polarity”] = df[“Tweets”].map(lambda tweet:textblob.TextBlob(tweet).sentiment.polarity)

# create a new column (Result) which contains + and -((+) if the value we #get in polarity is greater than 0 and (-) if the value is less than zero)

df[“Result”] = df[“polarity”].map(lambda pol: “+” if pol>0 else “-”)

# counting the no of positive and negative using count()

positive = df[df.Result == “+”].count()[“Tweets”]
negative = df[df.Result == “-”].count()[“Tweets”]

# at last plot the result by plt to visualize how many tweets are negative and how many are positive

x = [“positive”,”negative”]
plt.bar(x,[positive,negative],color=[“green”,”red”])
plt.title(“sentiment of dogecoin”)
plt.ylabel(“count”)
plt.show()

--

--

Prashant Rohilla

student of msc data science ,completed graduation in computer science from university of delhi