-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
197 lines (178 loc) · 5.03 KB
/
main.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
import os
from sklearn.svm import LinearSVC
from sklearn.feature_extraction.text import TfidfVectorizer
class L1LinearSVC(LinearSVC):
def fit(self, X, y):
# The smaller C, the stronger the regularization.
# The more regularization, the more sparsity.
self.transformer_ = LinearSVC(penalty="l1", dual=False, tol=1e-3)
X = self.transformer_.fit_transform(X, y)
return LinearSVC.fit(self, X, y)
def predict(self, X):
X = self.transformer_.transform(X)
return LinearSVC.predict(self, X)
print 'Adding stopwords'
path_stopwords = '../dataset/stopwords/'
file_stopwords = os.listdir(path_stopwords)
list_stopwords = []
for i in file_stopwords:
f = open(path_stopwords+i,'r')
content = f.read()
content = content.decode("utf-8-sig")
content = content.encode("utf-8")
content = content.split('\n')
content.remove('')
list_stopwords = list_stopwords + content
print 'Adding positive training data'
path_train = '../dataset/tweets_train/'
list_train = []
list_train_target = []
# Positive
file_train = os.listdir(path_train+'pos/')
for i in file_train:
f = open(path_train+'pos/'+i,'r')
content = f.read()
content = content.decode("utf-8-sig")
content = content.encode("utf-8")
content = content.strip()
list_train.append(content)
list_train_target.append(1)
# Negative
print 'Adding negative training data'
file_train = os.listdir(path_train+'neg/')
for i in file_train:
f = open(path_train+'neg/'+i,'r')
content = f.read()
content = content.decode("utf-8-sig")
content = content.encode("utf-8")
content = content.strip()
list_train.append(content)
list_train_target.append(0)
print 'Adding positive testing data'
path_test = '../dataset/tweets_test/'
list_test = []
list_test_target = []
# Positive
file_test = os.listdir(path_test+'pos/')
for i in file_test:
f = open(path_test+'pos/'+i,'r')
content = f.read()
content = content.decode("utf-8-sig")
content = content.encode("utf-8")
content = content.strip()
list_test.append(content)
list_test_target.append(1)
# Negative
print 'Adding negative testing data'
file_test = os.listdir(path_test+'neg/')
for i in file_test:
f = open(path_test+'neg/'+i,'r')
content = f.read()
content = content.decode("utf-8-sig")
content = content.encode("utf-8")
content = content.strip()
list_test.append(content)
list_test_target.append(0)
print 'Replace URL and mention'
for i in range(len(list_train)):
temp = list_train[i]
temp = temp.split(' ')
for j in range(len(temp)):
if temp[j].startswith('@'):
temp[j]='||T||'
elif (temp[j].lower()).startswith('http'):
temp[j]='||U||'
list_train[i] = ' '.join(temp)
for i in range(len(list_test)):
temp = list_test[i]
temp = temp.split(' ')
for j in range(len(temp)):
if temp[j].startswith('@'):
temp[j]='||T||'
elif (temp[j].lower()).startswith('http'):
temp[j]='||U||'
list_test[i] = ' '.join(temp)
print 'Extracting feature from training and testing data'
vectorizer = TfidfVectorizer(stop_words=list_stopwords,token_pattern='([^\\s]+)')
X_train = vectorizer.fit_transform(list_train)
X_test = vectorizer.transform(list_test)
y_train = list_train_target
y_test = list_test_target
print 'Training with linear kernel SVM'
svc_linear = L1LinearSVC()
svc_linear.fit(X_train,y_train)
print 'Predict test data'
pred = svc_linear.predict(X_test)
'''print 'Input testing data'
true = 0
false = 0
path_test = '../corpora/tweets_test/'
# Positive
file_test = os.listdir(path_test+'pos/')
for i in file_test:
f = open(path_test+'pos/'+i,'r')
content = f.read()
content = content.split('\n')[0]
ans = vectorizer.transform([content]).toarray()
ans = svc_linear.predict(ans)[0]
if ans==1:
true = true+1
else:
false = false+1
# Negative
file_test = os.listdir(path_test+'neg/')
for i in file_test:
f = open(path_test+'neg/'+i,'r')
content = f.read()
content = content.split('\n')[0]
ans = vectorizer.transform([content]).toarray()
ans = svc_linear.predict(ans)[0]
if ans==0:
true = true+1
else:
false = false+1'''
true = 0
false = 0
for i in range(len(pred)):
if pred[i]==y_test[i]:
true=true+1
else:
false=false+1
print 'Accuracy: ',true*100.0/(true+false)
'''print 'Remove stopwords and replace url/mention on tweets training'
for i in range(len(list_train)):
temp = list_train[i].split(' ')
temp1 = [w for w in temp if not w in list_stopwords]
j = 0
z = len(temp1)
while j<len(temp1):
if temp1[j].startswith('@'):
temp1.remove(temp1[j])
temp1.append('||T||')
elif temp1[j].startswith('http'):
temp1.remove(temp1[j])
temp1.append('||U||')
else:
j = j+1
list_train[i] = temp1
print 'Determine unigram attribute'
attrib = []
for i in range(len(list_train)):
for j in list_train[i]:
if j in attrib:
pass
else:
attrib.append(j)
print 'Generate unigram for tweets training'
unigram_train = []
i = 0
while i<len(list_train):
list = [0 for k in range(len(attrib))]
for j in list_train[i]:
idx = attrib.index(j)
list[idx] = 1
unigram_train.append(list)
i = i+4
print 'Train with SVM'
svc_rbf = svm.SVC(kernel='rbf',C=1)
svc_rbf.fit(unigram_train,list_train_target[::4])'''