-
Notifications
You must be signed in to change notification settings - Fork 6
/
baseline.py
242 lines (209 loc) · 7.95 KB
/
baseline.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# You need to install scikit-learn:
# sudo pip install scikit-learn
#
# Dataset: Polarity dataset v2.0
# http://www.cs.cornell.edu/people/pabo/movie-review-data/
#
# Full discussion:
# https://marcobonzanini.wordpress.com/2015/01/19/sentiment-analysis-with-python-and-scikit-learn
import sys
import os
import time
import os
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
import string
import nltk
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn import svm
from sklearn.metrics import classification_report
import random
import unicodedata
import pandas as pd
from sklearn.ensemble import RandomForestClassifier
NLTK_STOPWORDS = set(stopwords.words('english'))
# In[2]:
def usage():
print('Usage:')
print('python %s <data_dir>' % sys.argv[0])
def lowercase( s):
return s.lower()
def tokenize( s):
token_list = nltk.word_tokenize(s)
return token_list
def remove_punctuation( s):
return s.translate(None, string.punctuation)
def remove_numbers( s):
return s.translate(None, string.digits)
def remove_stopwords( token_list):
exclude_stopwords = lambda token : token not in NLTK_STOPWORDS
return filter(exclude_stopwords, token_list)
def stemming_token_list( token_list):
STEMMER = PorterStemmer()
#print token_list.decode('utf-8')
return [STEMMER.stem(tok.decode('utf-8')) for tok in token_list]
def restring_tokens( token_list):
return ' '.join(token_list)
# Function to clean the reviews using the Pre-Processing functions written above
def cleanDataset( line):
cleanData = ''
line = lowercase(line)
printable = set(string.printable)
line = filter(lambda x: x in printable, line)
#line = unicodedata.normalize('NFKD', line).encode('ascii','ignore')
line = remove_punctuation(line)
line = remove_numbers(line)
token_list = tokenize(line)
token_list = remove_stopwords(token_list)
token_list = stemming_token_list(token_list)
for words in token_list:
cleanData+=words+' '
return cleanData
if __name__ == '__main__':
print 'Entered'
if len(sys.argv) > 2:
usage()
sys.exit(1)
print 'Entered 1'
data_dir = 'txt_sentoken'
classes = ['pos', 'neg']
# Read the data
train_data = []
train_labels = []
test_data = []
test_labels = []
list = []
Data = pd.read_csv('Data/iseardataset.csv',header=None)
#Data = pd.read_csv('text_emotion.csv',header=None)
#Data = pd.read_csv('preprocessed_yelp.csv',header=None)
#print Data[2]
#print len(Data[1])
for i in range (len(Data[0])):
## if i < 10:
## print Data[2][i]+' '+Data[0][i]
#line = Data[2][i]+'|'+Data[0][i]
line = Data[0][i]+'|'+Data[1][i]
#line = Data[1][i]+'|'+Data[3][i]
list.append(line)
## f = open('combined.txt','w')
## f1 = open('pos','r')
## c = 0
## for i in f1:
## i = cleanDataset(i)
## line = 'pos|'+i
## f.write(line)
## list.append(line)
## f.write('\n')
## f1 = open('neg','r')
## c = 0
## for i in f1:
## i = cleanDataset(i)
## line = 'neg|'+i
## f.write(line)
## list.append(line)
## f.write('\n')
## f.close()
random.shuffle(list)
c = 0
for i in range(int(len(list)*0.7)):
if c < 10:
#print list[i][4:]
#print list[i]
c = c+ 1
index = list[i].index('|')
train_data.append(list[i][index+1:])
train_labels.append(list[i][:index])
for i in range(int(len(list)*0.7)+1, len(list)):
index = list[i].index('|')
test_data.append(list[i][index+1:])
test_labels.append(list[i][:index])
## for curr_class in classes:
## dirname = os.path.join(data_dir, curr_class)
## for fname in os.listdir(dirname):
## with open(os.path.join(dirname, fname), 'r') as f:
## content = f.read()
## if fname.startswith('cv9'):
## test_data.append(content)
## test_labels.append(curr_class)
## else:
## train_data.append(content)
## train_labels.append(curr_class)
# Create feature vectors
vectorizer = TfidfVectorizer(min_df=5,
max_df = 0.8,
sublinear_tf=True,
use_idf=True)
train_vectors = vectorizer.fit_transform(train_data)
test_vectors = vectorizer.transform(test_data)
# Perform classification with SVM, kernel=rbf
classifier_rbf = svm.SVC()
t0 = time.time()
classifier_rbf.fit(train_vectors, train_labels)
t1 = time.time()
prediction_rbf = classifier_rbf.predict(test_vectors)
t2 = time.time()
time_rbf_train = t1-t0
time_rbf_predict = t2-t1
print len(prediction_rbf),' ', len(test_labels)
c = 0
for i in range(len(test_labels)):
if prediction_rbf[i]==test_labels[i]:
c += 1
print prediction_rbf[i],' ', test_labels[i]
print 'ACCURACY RBF= ',float((c*1.0)/len(test_labels))
# Perform classification with SVM, kernel=linear
classifier_linear = svm.SVC(kernel='linear')
t0 = time.time()
classifier_linear.fit(train_vectors, train_labels)
t1 = time.time()
prediction_linear = classifier_linear.predict(test_vectors)
t2 = time.time()
time_linear_train = t1-t0
time_linear_predict = t2-t1
print len(prediction_linear),' ', len(test_labels)
c = 0
for i in range(len(test_labels)):
if prediction_linear[i]==test_labels[i]:
c += 1
print prediction_linear[i],' ', test_labels[i]
print 'ACCURACY LINEAR= ',float((c*1.0)/len(test_labels))
# Perform classification with SVM, kernel=linear
classifier_liblinear = svm.LinearSVC()
t0 = time.time()
classifier_liblinear.fit(train_vectors, train_labels)
t1 = time.time()
prediction_liblinear = classifier_liblinear.predict(test_vectors)
t2 = time.time()
time_liblinear_train = t1-t0
time_liblinear_predict = t2-t1
print len(prediction_liblinear),' ', len(test_labels)
c = 0
for i in range(len(test_labels)):
if prediction_liblinear[i]==test_labels[i]:
c += 1
print prediction_liblinear[i],' ', test_labels[i]
print 'ACCURACY LIBLINEAR= ',float((c*1.0)/len(test_labels))
# Print results in a nice table
print('Results for SVC(kernel=rbf)')
print('Training time: %fs; Prediction time: %fs' % (time_rbf_train, time_rbf_predict))
print(classification_report(test_labels, prediction_rbf))
print('Results for SVC(kernel=linear)')
print('Training time: %fs; Prediction time: %fs' % (time_linear_train, time_linear_predict))
print(classification_report(test_labels, prediction_linear))
print('Results for LinearSVC()')
print('Training time: %fs; Prediction time: %fs' % (time_liblinear_train, time_liblinear_predict))
print(classification_report(test_labels, prediction_liblinear))
print ''
print 'Bhai ab aagye Random Forest Classifier finally'
print train_vectors.shape, test_vectors.shape
classifier = RandomForestClassifier().fit(train_vectors.toarray(), train_labels)
print '......'
prediction_labels = classifier.predict(test_vectors.toarray())
c = 0
for i in range(len(test_labels)):
if prediction_labels[i]==test_labels[i]:
c += 1
print prediction_labels[i],' ', test_labels[i]
print 'ACCURACY Random Forest= ',float((c*1.0)/len(test_labels))
print('Results for Random Forest Classifier')
print(classification_report(test_labels, prediction_labels))