Socialify

Folder ..

Viewing SentimentAnalyser.py
53 lines (42 loc) • 1.3 KB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from cassandra.cluster import Cluster
import re
import pandas as pd


compound = []
pos = []
neu = []
neg = []

def sentence_score(rs):
    review_score = SentimentIntensityAnalyzer()
    return review_score.polarity_scores(rs)['compound']
    # compound.append(review_score.polarity_scores(rs)['compound'])
    # neg.append(review_score.polarity_scores(rs)['neg'])
    # neu.append(review_score.polarity_scores(rs)['neu'])
    # pos.append(review_score.polarity_scores(rs)['pos'])


cluster = Cluster(['127.0.0.1'], port=9042)
session = cluster.connect()
session.set_keyspace('twitter')
session.execute("USE twitter")

# Select all tweets from cassandra database
query = "SELECT * FROM twitterdata"
rows = session.execute(query)
tweets = []

# Iterate through all tweets
for row in rows:
    try:
        tweets.append({
            'tweet_id': row.tweet_id,
            'tweet': row.tweet,
            'score': sentence_score(row.tweet)
        })
    except:
        print(row.tweet)


for tweet in tweets:
    if tweet.get('score') > 0.5:
        tweet['sentiment'] = 'positive'
    elif tweet.get('score') < -0.5:
        tweet['sentiment'] = 'negative'
    else:
        tweet['sentiment'] = 'neutral'

df = pd.DataFrame(tweets)
df.to_csv('tweets.csv', index=False)