-
Notifications
You must be signed in to change notification settings - Fork 2
/
nlp.py
234 lines (190 loc) · 8.09 KB
/
nlp.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
from time import time
import matplotlib.pyplot as plt
from sklearn.feature_selection import SelectKBest, chi2
from sklearn.linear_model import RidgeClassifier
from sklearn.svm import LinearSVC
from sklearn.linear_model import SGDClassifier
from sklearn.linear_model import Perceptron
from sklearn.linear_model import PassiveAggressiveClassifier
from sklearn.naive_bayes import BernoulliNB, MultinomialNB
from sklearn.neighbors import KNeighborsClassifier
from sklearn.neighbors import NearestCentroid
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils.extmath import density
from sklearn import metrics
import numpy as np
import pandas as pd
from sklearn import model_selection
from sklearn.ensemble import RandomForestClassifier
from sklearn.pipeline import Pipeline
from sklearn.feature_extraction.text import TfidfVectorizer
import string
from nltk.corpus import stopwords as sw
from nltk.corpus import wordnet as wn
from sklearn.preprocessing import Normalizer
from nltk import wordpunct_tokenize
from nltk.stem import WordNetLemmatizer
from nltk import sent_tokenize
from nltk import pos_tag
from sklearn.base import BaseEstimator,TransformerMixin
class NLTKPreprocesor(BaseEstimator,TransformerMixin):
def __init__(self,stopwords = None,punct = None,lower = True,strip=True):
self.lower = lower
self.strip = strip
self.stopwords = stopwords or set(sw.words('english'))
self.punct = punct or set(string.punctuation)
self.lemmatizer = WordNetLemmatizer()
def fit(self,X,y=None):
return self
def inverse_transform(self,X):
pass
def transform(self,X):
return [list(self.tokenize(doc)) for doc in X]
def tokenize(self,document):
for sent in sent_tokenize(document):
for token,tag in pos_tag(wordpunct_tokenize(sent)):
token = token.lower() if self.lower else token
token = token.strip() if self.strip else token
token = token.strip('_') if self.strip else token
token = token.strip('*') if self.strip else token
token = token.strip('#') if self.strip else token
if token in self.stopwords:
continue
if all(char in self.punct for char in token):
continue
if len(token) <= 0:
continue
lemma = self.lemmatize(token,tag)
yield lemma
def lemmatize(self,token,tag):
tag ={
'N' : wn.NOUN,
'V' : wn.VERB,
'R' : wn.ADV,
'J' : wn.ADJ
}.get(tag[0],wn.NOUN)
return self.lemmatizer.lemmatize(token,tag)
def prepare_data(X,X_t):
preProcess = Pipeline([
('NLTKpreprocess', NLTKPreprocesor()),
('vectorizer', TfidfVectorizer(
max_df=0.90,
max_features=5000,
encoding='latin1',
tokenizer=lambda x: x,
preprocessor=None, lowercase=False))
])
train_text_features = np.asanyarray(preProcess.fit_transform(X['text'].values).todense())
test_text_features = np.asanyarray(preProcess.transform(X_t['text'].values).todense())
x_train = np.column_stack((train_text_features,
X['linked_tweets'].values, X['related_links'].values))
x_test = np.column_stack((test_text_features,
X_t['linked_tweets'].values, X_t['related_links'].values))
return x_train,x_test,preProcess
def trim(s):
"""Trim string to fit on terminal (assuming 80-column display)"""
return s if len(s) <= 80 else s[:77] + "..."
def benchmark(clf,X_train,y_train,X_test,y_test,feature_names=None):
print('_' * 80)
print("Training: ")
print(clf)
t0 = time()
clf.fit(X_train, y_train)
train_time = time() - t0
print("train time: %0.3fs" % train_time)
t0 = time()
pred = clf.predict(X_test)
test_time = time() - t0
print("test time: %0.3fs" % test_time)
score = metrics.accuracy_score(y_test, pred)
print("accuracy: %0.3f" % score)
if hasattr(clf, 'coef_'):
print("dimensionality: %d" % clf.coef_.shape[1])
print("density: %f" % density(clf.coef_))
if feature_names is not None:
print("top 10 keywords per class:")
top10 = np.argsort(clf.coef_)[-10:]
print(trim("%s: %s" % ('fake new has :', " ".join(feature_names[top10]))))
print()
print("classification report:")
print(metrics.classification_report(y_test, pred))
print("confusion matrix:")
print(metrics.confusion_matrix(y_test, pred))
print()
clf_descr = str(clf).split('(')[0]
return clf_descr, score, train_time, test_time
def main(X,X_t,y,y_t):
print(X.shape)
x_train,x_test,preprocessor = prepare_data(X,X_t)
results = []
for clf, name in (
(RidgeClassifier(tol=1e-2, solver="lsqr"), "Ridge Classifier"),
(Perceptron(n_iter=50), "Perceptron"),
(PassiveAggressiveClassifier(n_iter=50), "Passive-Aggressive"),
(KNeighborsClassifier(n_neighbors=10), "kNN"),
(RandomForestClassifier(n_estimators=300), "Random forest")):
print('=' * 80)
print(name)
results.append(benchmark(clf,x_train,y,x_test,y_t,None))
for penalty in ["l2", "l1"]:
print('=' * 80)
print("%s penalty" % penalty.upper())
# Train Liblinear model
results.append(benchmark(LinearSVC(loss='l2', penalty=penalty,
dual=False, tol=1e-3),x_train,y,x_test,y_t,None))
# Train SGD model
results.append(benchmark(SGDClassifier(alpha=.0001, n_iter=50,
penalty=penalty),x_train,y,x_test,y_t,None))
# Train SGD with Elastic Net penalty
print('=' * 80)
print("Elastic-Net penalty")
results.append(benchmark(SGDClassifier(alpha=.0001, n_iter=50,
penalty="elasticnet"),x_train,y,x_test,y_t,None))
# Train NearestCentroid without threshold
print('=' * 80)
print("NearestCentroid (aka Rocchio classifier)")
results.append(benchmark(NearestCentroid(),x_train,y,x_test,y_t,None))
# Train sparse Naive Bayes classifiers
print('=' * 80)
print("Naive Bayes")
results.append(benchmark(MultinomialNB(alpha=.01),x_train,y,x_test,y_t,None))
results.append(benchmark(BernoulliNB(alpha=.01),x_train,y,x_test,y_t,None))
print('=' * 80)
print("LinearSVC with L1-based feature selection")
# The smaller C, the stronger the regularization.
# The more regularization, the more sparsity.
results.append(benchmark(Pipeline([
('feature_selection', LinearSVC(penalty="l1", dual=False, tol=1e-3)),
('classification', LinearSVC())
]),x_train,y,x_test,y_t,None))
# make some plots
indices = np.arange(len(results))
results = [[x[i] for x in results] for i in range(4)]
clf_names, score, training_time, test_time = results
training_time = np.array(training_time) / np.max(training_time)
test_time = np.array(test_time) / np.max(test_time)
plt.figure(figsize=(12, 8))
plt.title("Score")
plt.barh(indices, score, .2, label="score", color='navy')
plt.barh(indices + .3, training_time, .2, label="training time",
color='c')
plt.barh(indices + .6, test_time, .2, label="test time", color='darkorange')
plt.yticks(())
plt.legend(loc='best')
plt.subplots_adjust(left=.25)
plt.subplots_adjust(top=.95)
plt.subplots_adjust(bottom=.05)
for i, c in zip(indices, clf_names):
plt.text(-.3, i, c)
plt.show()
if __name__ == '__main__':
fakes = pd.read_csv('clean_fake_data.csv',encoding='latin1',index_col=0)
fakes = fakes.dropna()
y_fakes = np.asarray([1]*fakes.shape[0],dtype=np.int)
reals = pd.read_csv('clean_true_data.csv',encoding='latin1')
reals = reals.dropna()
y_real = np.asarray([-1] * reals.shape[0], dtype=np.int)
y = np.concatenate([y_fakes,y_real])
data = pd.concat([fakes,reals])
train,test,y,y_t = model_selection.train_test_split(data,y,test_size=0.2)
main(train,test,y,y_t)