forked from weichenzhao/CS544_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
learner_classifier.py
209 lines (164 loc) · 5.27 KB
/
learner_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
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
import sys
import json
import pickle
import string
import nltk
from nltk.stem.porter import PorterStemmer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import PCA
from sklearn.decomposition import SparsePCA
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.svm import LinearSVC
from sklearn.metrics import f1_score
tag_dict = list()
tags = list()
body = list()
code = list()
title = list()
seg_dict_test = dict()
tags_test = list()
body_test = list()
code_test = list()
title_test = list()
tagFile = open('input_100.pkl', 'rb')
data = pickle.load(tagFile)
tags = data[0]
body = data[1]
code = data[2]
title = data[3]
tags_test = data[4]
body_test = data[5]
code_test = data[6]
title_test = data[7]
tag_dict = data[8]
def tokenize(text):
no_punctuation = text.translate(string.punctuation)
return no_punctuation.split()
def tf_idf_body():
print("tf_idf body trainging:")
tfidf = TfidfVectorizer(stop_words='english', tokenizer=nltk.word_tokenize)
tfs = tfidf.fit_transform(body)
# tfs_feature = tfs.toarray()
features_name = tfidf.get_feature_names()
print("tf_idf body test:")
test_feature = tfidf.transform(body_test)
# print(tfs_feature)
# print(len(features_name))
# print(tags_test)
################## learn ####################
model = learn(tfs)
################## classify #################
print("############\ttags predicted by body (tfidf) \t############")
classify(model, test_feature, tags_test)
def tf_idf_code():
print("tf_idf code training:")
tfidf = TfidfVectorizer(stop_words=None, tokenizer=nltk.word_tokenize)
# print(code)
tfs = tfidf.fit_transform(code)
# tfs_feature = tfs.toarray()
features_name = tfidf.get_feature_names()
print("tf_idf code test:")
test_feature = tfidf.transform(code_test)
# print(tfs_feature)
# print(len(features_name))
# print(tags_test)
################## learn ####################
model = learn(tfs)
################## classify #################
print("############\ttags predicted by code(tfidf):\t############")
classify(model, test_feature, tags_test)
def BOW_body():
print("bag_of_words body train: ")
bow = CountVectorizer(stop_words='english', tokenizer=nltk.word_tokenize)
bag = bow.fit_transform(body)
features_name = bow.get_feature_names()
print(len(features_name))
print("bag of wrods test: ")
test_feature = bow.transform(body_test)
# print(bag)
model = learn(bag)
print("############\ttags predicted by body(bag of words):\t############")
classify(model, test_feature, tags_test)
def BOW_code():
print("bag_of_words code train: ")
bow = CountVectorizer(stop_words=None, tokenizer=nltk.word_tokenize)
bag = bow.fit_transform(code)
print("bag of wrods test: ")
test_feature = bow.transform(code_test)
model = learn(bag)
print("############\ttags predicted by code(bag of words):\t############")
classify(model, test_feature, tags_test)
def predict_title():
print("classifiy title: ")
tag_predict = list()
for line in title_test:
lowers = line.lower()
no_punctuation = lowers.translate(string.punctuation)
tokens = no_punctuation.split()
# print(tokens)
t = list()
for token in tokens:
if token in tag_dict:
# print(token)
t.append(tag_dict.index(token))
tag_predict.append(t)
print("############\ttags predicted by title:\t############")
print(tag_predict)
def title_tfidf():
print("tf_idf title training:")
tfidf = TfidfVectorizer(stop_words=None, tokenizer=nltk.word_tokenize)
# print(code)
tfs = tfidf.fit_transform(title)
# tfs_feature = tfs.toarray()
features_name = tfidf.get_feature_names()
print("tf_idf title test:")
test_feature = tfidf.transform(title_test)
# print(tfs_feature)
# print(len(features_name))
# print(tags_test)
################## learn ####################
model = learn(tfs)
################## classify #################
print("############\ttags predicted by title(tfidf):\t############")
classify(model, test_feature, tags_test)
def title_bow():
print("bag_of_words title train: ")
bow = CountVectorizer(stop_words=None, tokenizer=nltk.word_tokenize)
bag = bow.fit_transform(title)
print("bag of wrods test: ")
test_feature = bow.transform(title_test)
model = learn(bag)
print("############\ttags predicted by title(bag of words):\t############")
classify(model, test_feature, tags_test)
def learn(tfs_feature):
# print("PCA")
# print(tfs_feature)
# traindata_final = PCA(n_components=10000).fit_transform(tfs_feature)
# print(traindata_final)
print("learning: ")
model = OneVsRestClassifier(LinearSVC(random_state=0)).fit(tfs_feature, tags)
# model = OneVsRestClassifier(SGDClassifier()).fit(tfs_feature, tags)
return model
def classify(model, test_feature, tags_test):
print("predicting: ")
tag_predict = model.predict(test_feature)
print(tag_predict)
result = f1_score(tags_test, tag_predict, average='weighted')
print("f-score: ", result)
return tag_predict
##################################################### main ##################################################
def main(argv):
# tf_idf_body()
tf_idf_code()
# BOW_body()
# BOW_code()
# predict_title()
# title_tfidf()
# title_bow()
# test(testFile)
# learn()
if __name__ == '__main__':
main(sys.argv)