def tableAppend(request): username = sharedLib.getUserName(request) data={} data['user']=username data['DataFrame']=DataFrame data['gmtTime']=withGMT(username) return render(request, 'flow/usecase_templates/tweetDisplayTable.html', data)
def getPieChart(request): username = sharedLib.getUserName(request) sentiments = final_df['Sentiments'].astype(str).value_counts() fig = {"data": [{"labels": sentiments.index ,"values": sentiments.values,"type": "pie"}]} py.plot(fig, filename=sharedLib.getFilePath('twitterSentiment', username)+'pie', auto_open=False) data={} data['user']=username data['pieImage']='pieChart' return render(request,'flow/usecase_templates/twitterPieChart.html', data)
def displayHashTag(request): username = sharedLib.getUserName(request) #api=twitterAuth() worldwide_trends=api.trends_place(1) trandingHashtag=[tweets['name'] for tweets in worldwide_trends[0]['trends']] filteredHashTag=[] for hashTag in trandingHashtag: if(hashTag.startswith('#') and bool(re.match('[a-zA-Z0-9_]+$',hashTag.split('#')[-1]))): filteredHashTag.append(hashTag) elif(bool(re.match('[a-zA-Z0-9_]+$',hashTag))): filteredHashTag.append(hashTag) data={'user':username,'filteredHashTag':filteredHashTag} return JsonResponse(data)
def tweetSentiment(request): print("inside tweetSentiment*********") username = sharedLib.getUserName(request) hashtag=None selectedhashtag = request.GET.get('hashtag') userEntered=request.GET.get('enterHash') print("selectedhashtag= ",selectedhashtag) print("userEntered = ",userEntered) if(userEntered!=''): hashtag=userEntered else: hashtag=selectedhashtag #api=twitterAuth() df = pd.DataFrame() text = [] user=[] tweetTime=[] # if(hashtag.startswith('#')): # hashtag='https://api.twitter.com/1.1/search/tweets.json?q=%23'+hashtag.split('#')[-1] # print("hashtag.startswith('#')= ",hashtag.startswith('#')) # print("hashtag ********** =",hashtag) for tweet in tweepy.Cursor(api.search, q=hashtag,lang='en').items(200): text.append(tweet.text) user.append(tweet.user.screen_name) tweetTime.append(tweet.created_at) if(len(text)!=0): try: df['text']=text df['user']=user df['createdAt']=tweetTime words = set(nltk.corpus.words.words()) stopword_set = set(stopwords.words("english")) refinedTweets=[] for tweet in df['text']: refinedWords=[] for word in tweet.split(' '): word=re.sub(r'[|!|.|:|+|?|,|-|*|%|/]','',word) if(bool(re.match('[a-zA-Z_]+$',word)) and not word.startswith('RT') and word in words and not word in stopword_set and len(word)!=1): refinedWords.append(word) refinedTweets.append(' '.join(refinedWords)) df['refinedTweets']=refinedTweets global final_df final_df = pd.DataFrame() final_user=[] final_text=[] final_time=[] text_sentiment=[] for i in range(df.shape[0]): if(len(df['refinedTweets'][i])!=0): score,sentiment=sentiment_textblob(df['refinedTweets'][i]) final_user.append(df['user'][i]) final_text.append(df['text'][i]) final_time.append(df['createdAt'][i]) text_sentiment.append(sentiment) final_df['User']=final_user final_df['Tweets']=final_text final_df['CreatedAt']=final_time final_df['Sentiments']=text_sentiment global DataFrame DataFrame=[] DataFrame.append(list(final_df.columns)) for i in range(final_df.shape[0]): DataFrame.append(list(final_df.iloc[i])) data={} data['user']=username #data['DataFrame']=DataFrame data['gmtTime']=withGMT(username) data['error']='false' #return render(request, 'flow/usecase_templates/tweetTable.html', data) return JsonResponse(data) except: data={} data['user']=username data['gmtTime']=withGMT(username) data['error']='True' data['errorMsg']=str(sys.exc_info()) return JsonResponse(data) elif(len(text)==0): data={} data['user']=username data['gmtTime']=withGMT(username) data['error']='True' data['errorMsg']="The given <b>"+hashtag+"</b> Hashtag or word contains only Non-English Tweets so, can't do analysis." #return render(request, 'flow/usecase_templates/twitter.html', data) return JsonResponse(data)
def twitterInit(request): username = sharedLib.getUserName(request) # print(sys.exc_info()[1]) data={'user':username,'gmtTime':withGMT(username)} return render(request, 'flow/usecase_templates/twitter.html', data)
def pieChart(request): username = sharedLib.getUserName(request) graphPath=sharedLib.getFilePath('twitterSentiment', username)+'pie.html' print("graphPath= ",graphPath) return render(request,graphPath)