08.06.2015 Views

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

Building Machine Learning Systems with Python - Richert, Coelho

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Classification II – Sentiment Analysis<br />

for w,t in tagged:<br />

p, n = 0,0<br />

sent_pos_type = None<br />

if t.starts<strong>with</strong>("NN"):<br />

sent_pos_type = "n"<br />

nouns += 1<br />

elif t.starts<strong>with</strong>("JJ"):<br />

sent_pos_type = "a"<br />

adjectives += 1<br />

elif t.starts<strong>with</strong>("VB"):<br />

sent_pos_type = "v"<br />

verbs += 1<br />

elif t.starts<strong>with</strong>("RB"):<br />

sent_pos_type = "r"<br />

adverbs += 1<br />

if sent_pos_type is not None:<br />

sent_word = "%s/%s"%(sent_pos_type, w)<br />

if sent_word in sent_word_net:<br />

p,n = sent_word_net[sent_word]<br />

pos_vals.append(p)<br />

neg_vals.append(n)<br />

l = len(sent)<br />

avg_pos_val = np.mean(pos_vals)<br />

avg_neg_val = np.mean(neg_vals)<br />

return [1-avg_pos_val-avg_neg_val,<br />

avg_pos_val, avg_neg_val,<br />

nouns/l, adjectives/l, verbs/l, adverbs/l]<br />

def transform(self, documents):<br />

obj_val, pos_val, neg_val, nouns, adjectives, \<br />

verbs, adverbs = np.array([self._get_sentiments(d) \<br />

for d in documents]).T<br />

allcaps = []<br />

exclamation = []<br />

question = []<br />

hashtag = []<br />

mentioning = []<br />

[ 144 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!