def on_data(self, data): fullData = json.loads(data) try: #Controleer aantal tweets in db en max aantal tweets if DatabaseNieuw.getAantalTweets() != aantalTweets: #data maken en filteren timestamp = fullData['timestamp_ms'] tweet = fullData['text'] userData = fullData['user'] user = userData['screen_name'] #Mentions en Retweets uitsluiten if tweet.startswith("RT @") or tweet.startswith("@"): return True if '"' in tweet: tweet.replace('"', '') if '"' in user: user.replace('"', '') #plaats data in de db DatabaseNieuw.setTweetsDatabase(tweet, user, timestamp) else: #Max aantal tweets is bereikt, doe analyse en stop daarna. AnalyseNieuw.startAnalyse() return False except KeyError: return True
def createTimechart(): plt.figure(figsize=(30,10)) tijdLijst = [] scoreLijst = [] data = DatabaseNieuw.getTimeChart() for rij in data: tijdLijst.append(datetime.datetime.fromtimestamp(rij[0]/1000.0)) scoreLijst.append(rij[1]) plt.plot(tijdLijst, scoreLijst) plt.show()
def createPiechart(): plt.title('ISCP - Daniël Vercouteren: Pie Chart') dataLabels = "Negatief", "Neutraal", "Positief" dataSize = DatabaseNieuw.getAnalysis() colors = "firebrick", "darkorange", "forestgreen" explode = (0, 0, 0) plt.pie(dataSize, explode=explode, labels=dataLabels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=90) plt.axis('equal') plt.show()
def createBarchart(): plt.xlabel('Sentiment') plt.ylabel('Aantal') plt.title('ISCP - Daniël Vercouteren: Bar Chart') #X is negatief, neutraal, positief #Y is het aantal data = ['Negatief', 'Neutraal', 'Positief'] x = np.arange(len(data)) y = DatabaseNieuw.getAnalysis() width = 0.8 plt.bar(x, y, width, color = "darkblue") plt.xticks(x + 0.4, data) plt.tight_layout() plt.show()
def scoring(tweetScore, tweetNummer): print("Dit is tweet #" + str(tweetNummer)) sys.stdout.write("Deze tweet is: ") if tweetScore > 0: print("positief") DatabaseNieuw.setAnalysis(tweetNummer, '2') elif tweetScore < 0: print("negatief") DatabaseNieuw.setAnalysis(tweetNummer, '0') else: print("neutraal") DatabaseNieuw.setAnalysis(tweetNummer, '1')
def startAnalyse(): tweetNummer = 0 tweetLijst = DatabaseNieuw.getTweets() for tweet in tweetLijst: tweetNummer += 1 analyse(tweet, tweetNummer)