Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

import tweepy

auth = tweepy.OAuthHandler("tUgkNrAfz45yvABii9yKofJQE",
"ptNgnz78tgayfluvWrCySLwnBdhHYQatJMopocWRZh1Vnre0P0" )
auth.set_access_token("1219626370684407809-Ljz224D2oLApMi2TfwWX4ySFHlfikG",
"UiHPG0BcSLLMKdBuaiRCmkRQdWtBTzNZEEiym7C67JRK9")
api = tweepy.API(auth)
public_tweets = api.home_timeline()

Above code is the most basic syntax


for accessing the twitter api using
tweepy
We have presented our framework in which
we have explained a process from the
collection, sentiment analysis, and
classification of Twitter opinions. We
considered tweets that were posted by users
in the form of hashtags to express their
opinions about current political trends. We
then stored the retrieved tweets in the
database, translated Urdu tweets, and pre-
processed the dataset. After pre-processing,
the remaining data was split into the training
set with 1690 tweets and the test set with
400 tweets. Polarity and subjectivity were
calculated using three different libraries,
SentiWordNet , W-WSD and TextBlob. We
applied the NaïveBayes and SVM classifier on
trainingset in Weka and built a classification
model. The model was tested on the training
data set to obtain the accuracy result of each
classifier.
Thefollowingsubsectionsdetailthetoolsandtec
hniquesthataidedusinsentimentanalysis.
Furthermore,a clear view of the sentiment-
analysis framework is illustrated below in
Figure1a,b

# Function to extract tweets


def get_tweets(username):
          
        # Authorization to consumer key and consumer secret
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
        # Access to user's access key and access secret
        auth.set_access_token(access_key, access_secret)
  
        # Calling api
        api = tweepy.API(auth)
  
        # 200 tweets to be extracted
        number_of_tweets=200
        tweets = api.user_timeline(screen_name=username)
  
        # Empty Array
        tmp=[] 
  
        # create array of tweet information: username, 
        # tweet id, date/time, text
        tweets_for_csv = [tweet.text for tweet in tweets] # CSV file
created 
        for j in tweets_for_csv:
  
            # Appending tweets to the empty array tmp
            tmp.append(j) 
  
        # Printing the tweets
        print(tmp)
  
  
# Driver code
if __name__ == '__main__':
  
    # Here goes the twitter handle for the user
    # whose tweets are to be extracted.
    get_tweets("twitter-handle")

You might also like