Ejemplo n.º 1
0
def primary(input_hashtag):
    secrets = Oauth_Secrets()  #secrets imported from secrets.py
    auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
    auth.set_access_token(secrets.access_token, secrets.access_token_secret)

    api = tweepy.API(auth)

    N = 50  #Number of Tweets
    Tweets = tweepy.Cursor(api.search, q=input_hashtag).items(N)
    neg = 0.0
    pos = 0.0
    neg_count = 0
    neutral_count = 0
    pos_count = 0
    for tweet in Tweets:
        # print tweet.text
        blob = TextBlob(tweet.text)
        print(blob)
        if blob.sentiment.polarity < 0:  #Negative
            neg += blob.sentiment.polarity
            neg_count += 1
        elif blob.sentiment.polarity == 0:  #Neutral
            neutral_count += 1
        else:  #Positive
            pos += blob.sentiment.polarity
            pos_count += 1
    # print "Total tweets",N
    # print "Positive ",float(pos_count/N)*100,"%"
    # print "Negative ",float(neg_count/N)*100,"%"
    # print "Neutral ",float(neutral_count/N)*100,"%"
    return [['Sentiment', 'no. of tweets'], ['Positive', pos_count],
            ['Neutral', neutral_count], ['Negative', neg_count]]
Ejemplo n.º 2
0
def primary(hashtag):
    secrets = Oauth_Secrets()
    auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
    auth.set_access_token(secrets.access_token, secrets.access_token_secret)

    api = tweepy.API(auth)

    N = 100
    tweets = tweepy.Cursor(api.search, q=hashtag).items(N)
    neg = 0.0
    pos = 0.0
    neg_count = 0
    pos_count = 0
    neutral_count = 0
    for tweet in tweets:
        blob = TextBlob(tweet.text)
        if blob.sentiment.polarity < 0:
            neg += blob.sentiment.polarity
            neg_count += 1
        elif blob.sentiment.polarity > 0:
            pos += blob.sentiment.polarity
            pos_count += 1
        else:
            neutral_count += 1

    return [['Sentiment', 'no of tweets'], ['Positive', pos_count],
            ['Negative', neg_count], ['Neutral', neutral_count]]
Ejemplo n.º 3
0
def analyze(input_word):
    secrets = Oauth_Secrets()
    auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
    auth.set_access_token(secrets.access_token, secrets.access_token_secret)

    api = tweepy.API(auth)

    Num_tweets = 100
    Tweets = tweepy.Cursor(api.search, q=input_word).items(Num_tweets)
    negative, positive = (0.0, 0.0)
    neg_count, pos_count = (0, 0)
    neutral_count = 0

    for tweet in Tweets:
        # print tweet.text
        blob = TextBlob(tweet.text)
        if blob.sentiment.polarity < 0:  #Negative tweets
            negative += blob.sentiment.polarity
            neg_count += 1
        elif blob.sentiment.polarity == 0:  #Neutral tweets
            neutral_count += 1
        else:  #Positive tweets
            positive += blob.sentiment.polarity
            pos_count += 1
    return [['Category', 'Tweets crawled'], ['Positive', pos_count],
            ['Neutral', neutral_count], ['Negative', neg_count]]
Ejemplo n.º 4
0
 def __init__(self, text):
     self.text = text
     secrets = Oauth_Secrets()
     self.auth = tweepy.OAuthHandler(secrets.consumer_key,
                                     secrets.consumer_secret)
     self.auth.set_access_token(secrets.access_token,
                                secrets.access_token_secret)
     self.myStreamListener = MyStreamListener()
     self.trending = []
     """ Virtually private constructor. """
     if StreamTweets.__instance is not None:
         raise Exception(
             "This class is a singleton! Call the instance methods")
     else:
         StreamTweets.__instance = self
Ejemplo n.º 5
0
def primary(input_hashtag):
    secrets = Oauth_Secrets()  #secrets imported from secrets.py
    auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
    auth.set_access_token(secrets.access_token, secrets.access_token_secret)

    api = tweepy.API(auth)

    N = 1000  #Number of Tweets
    Tweets = tweepy.Cursor(api.search, q=input_hashtag).items(N)
    # Tweets=tweepy.Cursor(api.search,q=input_hashtag + " -filter:retweets",rpp=5,lang="en", tweet_mode='extended').items(50)
    # tweets_list = []
    # for tweet in Tweets:
    #     temp = {}
    #     temp["text"] = tweet.full_text
    #     temp["username"] = tweet.user.screen_name
    #     tweets_list.append(temp)
    # print("tweets::::::",tweets_list)
    neg = 0.0
    pos = 0.0
    pos_list = []
    nev_list = []
    neut_list = []
    neg_count = 0
    neutral_count = 0
    pos_count = 0
    # data = pd.DataFrame(data=[[tweet.text,tweet.created_at,tweet.user.screen_name,tweet.user.location,tweet.user.id,tweet.user.created_at,tweet.user.description] for tweet in Tweets],
    #                 columns=['Tweets','date','user','location','id','join_date','profile_description'])
    for tweet in Tweets:
        tweet_data_dict = {}
        tweet_data_dict = {
            'text': tweet.text,
            'created_at': tweet.created_at,
            'screen_name': tweet.user.screen_name,
            'user_location': tweet.user.location,
            'user_id': tweet.user.id,
            'user_created_at': tweet.user.created_at,
            'tweet_user_description': tweet.user.description
        }

        # print(tweet.)
        blob = TextBlob(tweet.text)
        if blob.sentiment.polarity < 0:  #Negative
            neg += blob.sentiment.polarity
            neg_count += 1
            nev_list.append(tweet_data_dict)

        elif blob.sentiment.polarity == 0:  #Neutral
            neutral_count += 1
            neut_list.append(tweet_data_dict)
        else:  #Positive
            pos += blob.sentiment.polarity
            pos_count += 1
            pos_list.append(tweet_data_dict)

    print('pos_list', len(pos_list), 'nev_list', len(nev_list), 'neut_list',
          len(neut_list))
    # print(nev_list)
    return [['Sentiment', 'no. of tweets'], ['Positive', pos_count],
            ['Neutral', neutral_count], ['Negative', neg_count], {
                'Positive_list': pos_list
            }, {
                'Negative_list': nev_list
            }, {
                'Neutral_list': neut_list
            }]
Ejemplo n.º 6
0
def getdata(input_hashtag):

    # input_hashtag = 'obama'
    secrets = Oauth_Secrets()
    auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
    auth.set_access_token(secrets.access_token, secrets.access_token_secret)

    api = tweepy.API(auth)

    N = 100  # number of tweets
    # Tweets = api.user_timeline(id=input_hashtag, count=N)
    Tweets = tweepy.Cursor(api.search, q=input_hashtag, lang="en").items(N)
    # Tweets = api.geo_search(query='Kenya', granularity="country")
    # print(Tweets.text[0])
    negative = 0.0
    positive = 0.0
    negative_count = 0
    neutral_count = 0
    postive_count = 0
    tweets_pos = []
    tweets_neg = []
    tweets_nut = []
    general_location = []
    time_negative = {}
    time_neutral = {}
    time_positive = {}
    # if len(Tweets) < 1:
    #     print("no tweets for now")
    # else:
    # print(Tweets)
    for tweet in Tweets:
        # print(tweet.created_at)
        # print(tweet.user.location)
        # print("placeid:%s" % tweet)
        # print(tweet.id_str, tweet.coordinates, tweet.geo, tweet.geocode)
        # print(tweet.place.country)
        general_location.append(tweet.user.location)
        blob = TextBlob(tweet.text)
        if blob.sentiment.polarity < 0:
            negative += blob.sentiment.polarity
            negative_count += 1
            tweets_neg.append(tweet.text)
            time_negative[tweet.created_at] = tweet.text
        elif blob.sentiment.polarity == 0:
            neutral_count += 1
            tweets_nut.append(tweet.text)
            time_neutral[tweet.created_at] = tweet.text
        else:
            positive += blob.sentiment.polarity
            postive_count += 1
            tweets_pos.append(tweet.text)
            time_positive[tweet.created_at] = tweet.text

    # post = ("Positive ", float(postive_count/N)*100, "%")

    data = {
        'Sample': N,
        'Topic': input_hashtag,
        'Positive': postive_count,
        'Neutral': neutral_count,
        'Negative': negative_count,
        'Nagative_tweets': tweets_neg,
        'Neutral_tweets': tweets_nut,
        'Postive_tweets': tweets_pos,
        'general_location': general_location,
        'time_negative': time_negative,
        'time_neutral': time_neutral,
        'time_positive': time_positive
    }
    # print(post)
    # print(data)

    return data
Ejemplo n.º 7
0
def getdata(input_hashtag):

    # input_hashtag = 'obama'
    secrets = Oauth_Secrets()
    auth = tweepy.OAuthHandler(secrets.consumer_key, secrets.consumer_secret)
    auth.set_access_token(secrets.access_token, secrets.access_token_secret)

    api = tweepy.API(auth)


# q='input_hashtag -filter:retweets'
    N = 1000  # number of tweets
    quiz = input_hashtag + ' -filter:retweets'
    print(quiz)
    # Tweets = api.user_timeline(id=input_hashtag, count=N)
    Tweets = tweepy.Cursor(api.search, q=quiz, tweet_mode='extended',
                           lang="en").items(N)
    # Tweets = api.geo_search(query='Kenya', granularity="country")
    # print(Tweets.text[0])
    negative = 0.0
    positive = 0.0
    negative_count = 0
    neutral_count = 0
    postive_count = 0
    tweets_pos = []
    tweets_neg = []
    tweets_nut = []
    # general_location = []
    # time_negative = {}
    # time_neutral = {}
    # time_positive = {}
    # if len(Tweets) < 1:
    #     print("no tweets for now")
    # else:
    # print(Tweets)
    # the key for profile image on tweet json is 'profile_image_url'
    for tweet in Tweets:
        if tweet.place:
            # print(tweet)
            print("place is:" + str(tweet.place))
            # location works for some
            print("user profile location is: " + tweet.user.location)
            print(tweet.user.screen_name)  # to show username
            print(tweet.user.profile_image_url)  # to show profile image
            # location works for some
            print('followers are: ' + str(tweet.user.followers_count))  # followers
            # number of user favourites
            print('user favourites are:' + str(tweet.user.favourites_count))
            # number of tweet retweets
            print('retweets are: ' + str(tweet.retweet_count))
            # number of tweet favourites
            print('favs are: ' + str(tweet.favorite_count))
            print(tweet.full_text)  # tweet itself
            # print('retweets are: ' + str(tweet.retweet_count))
            # print('favs are: ' + str(tweet.favorite_count))

            print('string is ' + str(tweet.id_str))  # tweet id

            # print(tweet)
            # print(tweet.text)
            # print(tweet.created_at)
            # print(tweet.user.location)
            # print("placeid:%s" % tweet)
            # print(tweet.id_str, tweet.coordinates, tweet.geo, tweet.geocode)
            # print(tweet.place.country)
            avatar = tweet.user.profile_image_url
            username = tweet.user.screen_name
            followers = tweet.user.followers_count
            retweets = tweet.retweet_count
            likes = tweet.favorite_count
            tweet_id = tweet.id_str
            # general_location.append(tweet.user.location)
            blob = TextBlob(tweet.full_text)
            if blob.sentiment.polarity < 0:
                tweet_full = {}
                negative += blob.sentiment.polarity
                negative_count += 1
                # tweets_neg.append(tweet.text)
                # order of dictionary matters as that is how items will be ordered on html regardless of what order you call them on html
                tweet_full['avatar'] = (avatar)
                tweet_full['username'] = (username)
                tweet_full['followers'] = (followers)
                tweet_full['tweet'] = (tweet.full_text)
                tweet_full['tweet_id'] = (tweet_id)
                tweet_full['retweets'] = (retweets)
                tweet_full['likes'] = (likes)

                tweets_neg.append(tweet_full)
                # time_negative[tweet.created_at] = tweet.text
            elif blob.sentiment.polarity == 0:
                tweet_full = {}

                neutral_count += 1
                # tweets_nut.append(tweet.text)
                tweet_full['avatar'] = (avatar)
                tweet_full['username'] = (username)
                tweet_full['followers'] = (followers)
                tweet_full['tweet'] = (tweet.full_text)
                tweet_full['tweet_id'] = (tweet_id)
                tweet_full['retweets'] = (retweets)
                tweet_full['likes'] = (likes)

                tweets_nut.append(tweet_full)

                # time_neutral[tweet.created_at] = tweet.text
            else:
                positive += blob.sentiment.polarity
                tweet_full = {}

                postive_count += 1
                # tweets_pos.append(tweet.text)
                tweet_full['avatar'] = (avatar)
                tweet_full['username'] = (username)
                tweet_full['followers'] = (followers)
                tweet_full['tweet'] = (tweet.full_text)
                tweet_full['tweet_id'] = (tweet_id)
                tweet_full['retweets'] = (retweets)
                tweet_full['likes'] = (likes)

                tweets_pos.append(tweet_full)
                # time_positive[tweet.created_at] = tweet.text
        else:
            print("tweet has no location")
    # post = ("Positive ", float(postive_count/N)*100, "%")

    data = {
        'Sample': N,
        'Topic': input_hashtag,
        'Positive': postive_count,
        'Neutral': neutral_count,
        'Negative': negative_count,
        'Negative_tweets': tweets_neg,
        'Neutral_tweets': tweets_nut,
        'Postive_tweets': tweets_pos,
        # 'general_location': general_location,
        # 'time_negative': time_negative,
        # 'time_neutral': time_neutral,
        # 'time_positive': time_positive

    }
    # print(post)
    # print(data)

    return data