- ProGeek, ProGeek 2021, Project, Python, Python Flask, Python-projects

Twitter Sentiment Analysis WebApp Using Flask

from flask import Blueprint, render_template, requestimport matplotlib.pyplot as pltimport os  import tweepyimport csvimport refrom textblob import TextBlobimport matplotlibmatplotlib.use(‘agg’)  second = Blueprint(“second”, __name__, static_folder=”static”,                   template_folder=”template”)  @second.route(“/sentiment_analyzer”)def sentiment_analyzer():    return render_template(“sentiment_analyzer.html”)    class SentimentAnalysis:      def __init__(self):        self.tweets = []        self.tweetText = []          def DownloadData(self, keyword, tweets):                  consumerKey = ‘//get from Tweepy’        consumerSecret = ‘//get from Tweepy’        accessToken = ‘//insert your access token here’        accessTokenSecret = ‘//Tweepy AccessToken secret here’        auth = tweepy.OAuthHandler(consumerKey, consumerSecret)        auth.set_access_token(accessToken, accessTokenSecret)        api = tweepy.API(auth, wait_on_rate_limit=True)                                  tweets = int(tweets)                  self.tweets = tweepy.Cursor(            api.search, q=keyword, lang=”en”).items(tweets)                  csvFile = open(‘result.csv’, ‘a’)                  csvWriter = csv.writer(csvFile)                  polarity = 0        positive = 0        wpositive = 0        spositive = 0        negative = 0        wnegative = 0        snegative = 0        neutral = 0                  for tweet in self.tweets:                                    self.tweetText.append(self.cleanTweet(tweet.text).encode(‘utf-8’))                                      analysis = TextBlob(tweet.text)                                                  polarity += analysis.sentiment.polarity                          if (analysis.sentiment.polarity == 0):                neutral += 1            elif (analysis.sentiment.polarity > 0 and analysis.sentiment.polarity 0.3 and analysis.sentiment.polarity 0.6 and analysis.sentiment.polarity -0.3 and analysis.sentiment.polarity -0.6 and analysis.sentiment.polarity -1 and analysis.sentiment.polarity 0 and polarity 0.3 and polarity 0.6 and polarity -0.3 and polarity -0.6 and polarity -1 and polarity