Reputation: 1
from textblob import TextBlob import sys import tweepy import matplotlib.pyplot as plt import pandas as pd import numpy as np import os import nltk import pycountry import re import string #Sentiment Analysis def percentage(part,whole): return 100 * float(part)/float(whole) keyword = input("Please enter keyword or hashtag to search: ") noOfTweet = int(input ("Please enter how many tweets to analyze: ")) tweets = tweepy.Cursor(api.search_users, q=keyword).items(noOfTweet) positive = 0 negative = 0 neutral = 0 polarity = 0 tweet_list = [] neutral_list = [] negative_list = [] positive_list = [] for tweet in tweets:
#print(tweet.text)
tweet_list.append(tweet.data)
analysis = TextBlob(tweet.data)
score = SentimentIntensityAnalyzer().polarity_scores(tweet.text)
neg = score['neg']
neu = score['neu']
pos = score['pos']
comp = score['compound']
polarity += analysis.sentiment.polarity
negative += 1
if(pos > neg):
positive_list.append(tweet.text)
positive += 1
elif pos == neg:
neutral_list.append(tweet.text)
neutral += 1
positive = percentage(positive, noOfTweet)
negative = percentage(negative, noOfTweet)
neutral = percentage(neutral, noOfTweet)
polarity = percentage(polarity, noOfTweet)
positive = format(positive, '.1f')
negative = format(negative, '.1f')
neutral = format(neutral, '.1f')
Upvotes: 0
Views: 255
Reputation: 1843
You are using the api.search_users()
method, which returns User
objects and not Tweet
objects (see the documentation), so they don't have any text
attribute.
You should use the api.search_tweets()
method instead (see the documentation).
Upvotes: 0