-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.py
143 lines (96 loc) · 3.58 KB
/
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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
from time import time
import sys
import pandas as pd
import nltk
from collections import Counter
from nltk.corpus import stopwords
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import LinearSVC
# from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, AdaBoostClassifier
from sklearn.naive_bayes import MultinomialNB
from sklearn.feature_extraction.text import TfidfVectorizer
from TurkishStemmer import TurkishStemmer
names = [
# "QDA",
"Nearest Neighbors",
"LinearSVC",
"RBF SVM",
"Decision Tree",
"Random Forest",
"Neural Net",
"AdaBoost",
"Naive Bayes"
]
classifiers = [
# QuadraticDiscriminantAnalysis(),
KNeighborsClassifier(),
LinearSVC(multi_class="crammer_singer"),
LinearSVC(C=1),
# GaussianProcessClassifier(1.0 * RBF(1.0)),
DecisionTreeClassifier(max_depth=5),
RandomForestClassifier(max_depth=5, n_estimators=10),
MLPClassifier(alpha=1),
AdaBoostClassifier(),
MultinomialNB(),
]
coefficients = [.51, 0, .59, .47, 0, .59, .50, .59]
s =sum(coefficients)
coefficients = [x/s for x in coefficients]
stemmer=TurkishStemmer()
def stem_tokens(tokens, stemmer):
stemmed = []
for item in tokens:
stemmed.append(stemmer.stem(item.encode('utf-8')))
return stemmed
def tokenize(text):
tokens = nltk.word_tokenize(text)
stems = stem_tokens(tokens, stemmer)
return stems
def multiple_classfier_prediction(X, y,query_tweets):
# results = [0]*len(query_tweets)
results = [Counter() for _ in range(len(query_tweets))]
for i, (name, clf) in enumerate(zip(names, classifiers)):
time_begin = time()
try:
clf.fit(X, y)
tweet_vector = vectorizer.transform(query_tweets)
temp_results = clf.predict(tweet_vector)
print(temp_results, name, 'in', time() - time_begin, 'seconds')
for j in range(len(results)):
results[j][temp_results[j]] += coefficients[i]
# results[j] += temp_results[j] * coefficients[i]
except TypeError as e:
print(name, e)
print
return results
if __name__ == '__main__':
try:
train_file = sys.argv[1]
inputtxt = sys.argv[2]
outputtxt = sys.argv[3]
except:
train_file = "combined-train.txt"
inputtxt = "input.txt"
outputtxt = 'output.txt'
with open(train_file, "r") as ts:
lines = ts.readlines()
_nrows = len(lines)
df=pd.read_csv(train_file,sep='\t',names=['liked','id','text'],engine='python',nrows=_nrows)
stopwords=stopwords.words('turkish')
vectorizer=TfidfVectorizer(tokenizer=tokenize,use_idf=True,lowercase=True,strip_accents='ascii',stop_words=stopwords)
y=df.liked
X=vectorizer.fit_transform(df.text)
with open(inputtxt, "r") as input_file:
query_tweets = input_file.readlines()
results = multiple_classfier_prediction(X, y,query_tweets)
print("results: ")
# for res in results:
# print(res.most_common(3))
with open(outputtxt,'w') as output_file:
for result, query_tweet in zip(results,query_tweets):
output_file.write(str(result.most_common(1)[0][0]) + '\t\t\t' + query_tweet)