In [22]:
!pip install textblob
In [23]:
from __future__ import unicode_literals, print_function
import textblob
import pandas as pd
import numpy as np
In [24]:
textblob.TextBlob("Hello World I hate you").sentiment.polarity
Out[24]:
In [25]:
textblob.TextBlob("Hello World I love you").sentiment.polarity
Out[25]:
In [26]:
#textfile = "../hackpadtext_Wed.txt"
textfile = "../hackpadtext_Thu.txt"
#textfile = "../hackpadtext_Thu_active.txt"
In [27]:
rawdata = pd.read_csv(textfile, header=None, names=["text"], sep="\n", encoding="utf-8")
In [28]:
rawdata["polarity"] = np.zeros_like(np.array(rawdata.columns["0"]))
In [29]:
# analyse each data/hack idea
feelings = []
for i in rawdata.index:
data = rawdata.loc[i].values[0]
polarity = textblob.TextBlob(data).sentiment.polarity
rawdata.loc[i,"polarity"] = polarity
feelings.append(polarity)
How happy are we on average?
In [30]:
average_feels = sum(feelings)/len(feelings)
print(average_feels)
In [31]:
if average_feels>0:
print("Yay, we're happy! wooooooooooo!")
else:
print("oh no not happy jan")
Who sounds sad?
In [32]:
# search for sad hacks
rawdata[rawdata["polarity"]<0]
Out[32]:
In [33]:
# search for happy hacks
#rawdata[rawdata["polarity"]>0]
In [34]:
# Top Five Happy Hacks!
rawdata.sort_values("polarity")[::-1][:5]
Out[34]:
Wait... most of those sound like comments, not hacks!
In [35]:
rawdata["mask"] = np.zeros_like(np.array(rawdata.columns["0"]))
In [36]:
# select only
for i in rawdata.index:
if len(rawdata.loc[i,"text"].split(" "))>20:
rawdata.loc[i,"mask"] = True
else:
rawdata.loc[i,"mask"] = False
New dataset only includes hacks, not comments
In [37]:
hackdata = rawdata[rawdata["mask"]]
In [38]:
#Top Five Sad Actually-Hacks (probably)
hackdata.sort_values("polarity")[:5]
Out[38]:
In [39]:
# Top Five Happy Actually-Hacks (probably)
hackdata.sort_values("polarity")[::-1][:5]
Out[39]:
Repeat analysis from earlier
In [40]:
moarfeelings = []
for i in hackdata.index:
data = hackdata.loc[i].values[0]
polarity = textblob.TextBlob(data).sentiment.polarity
moarfeelings.append(polarity)
In [41]:
average_feels = sum(moarfeelings)/len(moarfeelings)
print(average_feels)
In [42]:
if average_feels>0:
print("YAY, WE'RE ACTUALLY HAPPY! wooooooooooo!")
else:
print("oh no we're actually sad")
In [ ]: