-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathclickbait_classifier.py
executable file
·48 lines (45 loc) · 1.52 KB
/
clickbait_classifier.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
from sklearn import preprocessing,svm
from sklearn.pipeline import Pipeline
import nltk
import numpy
import subprocess
import time
import utility
############### Classifier ############################
print "creating classifier..."
no_samples = 10000
positive = numpy.loadtxt("vectors/positive.csv", delimiter=',')
negetive = numpy.loadtxt("vectors/negative.csv", delimiter=',')
X = numpy.concatenate((positive, negetive), axis=0)
p = numpy.ones((no_samples, 1))
n = numpy.full((no_samples, 1), -1, dtype=numpy.int64)
Y = numpy.concatenate((p,n), axis=0)
y = Y.ravel()
#scale
scaler = preprocessing.StandardScaler().fit(X)
#classifying
svm_module = svm.SVC()
classifier = Pipeline(steps= [('svm', svm_module)]) #[('scale', scaler), ('svm', svm_module)])
classifier.fit(X, y)
print "Classifer created"
##############################################################
##################### Clickbait Classifier Service ###########################
def isclickbait(document):
try:
title_vector = numpy.array(utility.create_vector(document)).reshape(1,-1)
# t = scaler.transform(title_vector)
prediction = classifier.predict(title_vector)
if prediction[0] == 1:
print "The headline is a Clickbait!"
else:
print "The headline is not a Clickbait!"
except Exception,e:
print "except:", e
if __name__ == '__main__':
input_text = "W"
while input_text != "Q":
input_text = raw_input("Enter a news headline or 'Q/q' to exit: ")
if input_text == "Q" or input_text == 'q':
break
isclickbait(input_text)
print "Exited Succesfully"