-
Notifications
You must be signed in to change notification settings - Fork 0
/
word2vec_sentiments.py
67 lines (50 loc) · 1.83 KB
/
word2vec_sentiments.py
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
65
66
67
from __future__ import division
import sys
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import scale
sys.path.append('models/word2vec_twitter_model')
from word2vecReader import Word2Vec
model_path = 'models/word2vec_twitter_model/word2vec_twitter_model.bin'
print 'Loading the model...'
model = Word2Vec.load_word2vec_format(model_path, binary=True)
def preprocess(text):
special_chars = """.,?!:;(){}[]#"""
for c in special_chars:
text = text.replace(c, ' %s '%c)
words = text.lower().split()
return words
def get_vector(text, model=model, size=400):
words = preprocess(text)
vec = np.zeros(size)
count = 0.
for word in words:
try:
vec += model[word]
count += 1.
except KeyError:
continue
if count != 0:
vec /= count
return vec
X_pos = open('data/positive-all','r').readlines()
X_neu = open('data/neutral-all','r').readlines()
X_neg = open('data/negative-all','r').readlines()
X_pos_vec = np.array(map(get_vector, X_pos))
X_neu_vec = np.array(map(get_vector, X_neu))
X_neg_vec = np.array(map(get_vector, X_neg))
y_pos_vec = np.ones(len(X_pos_vec))
y_neu_vec = np.zeros(len(X_neu_vec))
y_neg_vec = np.full(len(X_neg_vec),-1)
X_all = np.concatenate((X_pos_vec, X_neu_vec, X_neg_vec))
y_all = np.concatenate((y_pos_vec, y_neu_vec, y_neg_vec))
X_all = scale(X_all)
X_all.dump('models/X_all_w2v')
y_all.dump('models/y_all_w2v')
X_train, X_test, y_train, y_test = train_test_split(X_all, y_all, test_size=0.4, random_state=42)
logit = LogisticRegression(C=0.5)
clf = logit.fit(X_train, y_train)
pred = clf.predict(X_test)
print classification_report(y_test, pred, target_names=['1.','0.','-1.'])