forked from Robert-Wett/RedditSentimentAnalysis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSentimentTest.py
More file actions
64 lines (56 loc) · 2.1 KB
/
Copy pathSentimentTest.py
File metadata and controls
64 lines (56 loc) · 2.1 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
53
54
55
56
57
58
59
60
61
62
63
64
from __future__ import print_function
import sys, json, requests
from textblob import TextBlob
from colorama import *
init()
def areyouhappy(username=None, depth=None):
if username is None:
username = "presidentobama"
if depth is None:
depth = 10
else: depth = int(depth)
url = r"http://www.reddit.com/user/%s/comments.json?limit=100" % username
totalsent=0
postcount=0
comment_max = 500
label = "*"*22
pos=[]
neg=[]
r = requests.get(url)
data = json.loads(r.text)
posts = data['data']['children']
for idx, c in enumerate(posts):
c = c['data']
try:
# TODO: Check 'body_html' for <pre> tags to skip code...
if len(c['body']) < comment_max:
sentence = TextBlob(c['body'])
if sentence.sentiment[0] > 0:
pos.append((sentence.string, sentence.sentiment[0]))
else:
neg.append((sentence.string, sentence.sentiment[0]))
totalsent += sentence.sentiment[0]
postcount += 1
except:
pass
psort = sorted(pos, key=lambda x: x[-1])
pos_sort = sorted(psort, reverse=True)
nsort = sorted(neg, key=lambda x: x[-1])
neg_sort = sorted(nsort)
print(Fore.GREEN + label,"Top Positive Comments:", label, sep="\n")
for idx, x in enumerate(pos_sort):
if idx < depth:
print(Fore.RESET + u'{0}: "{1}"'.format(idx+1, pos_sort[idx][0]).encode('utf-8', 'ignore'), end="\n\n")
print(Fore.RED + label,"Most Negative Comments:", label, sep="\n")
for idx, x in enumerate(neg_sort):
if idx < depth:
print(Fore.RESET + u'{0}: "{1}"'.format(idx+1, neg_sort[idx][0]).encode('utf-8', 'ignore'), end="\n\n")
overall = totalsent/postcount
if overall > 0:
print("\n\n\nYou're happy overall though, your overall positivity is {0}".format(overall))
else:
print("\n\n\nYou're a candidate for the playahatas ball - your overall negativity is {0}".format(overall))
try:
areyouhappy(username=sys.argv[1])
except:
areyouhappy()