-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassification.py
More file actions
335 lines (227 loc) · 8.63 KB
/
classification.py
File metadata and controls
335 lines (227 loc) · 8.63 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import glob
import nltk
import grpc
import pandas as pd
from sklearn import naive_bayes, metrics
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics import confusion_matrix
import numpy as np
# In[ ]:
#gRPC gereksinimleri
import zemberek_grpc.language_id_pb2 as z_langid
import zemberek_grpc.language_id_pb2_grpc as z_langid_g
import zemberek_grpc.normalization_pb2 as z_normalization
import zemberek_grpc.normalization_pb2_grpc as z_normalization_g
import zemberek_grpc.preprocess_pb2 as z_preprocess
import zemberek_grpc.preprocess_pb2_grpc as z_preprocess_g
import zemberek_grpc.morphology_pb2 as z_morphology
import zemberek_grpc.morphology_pb2_grpc as z_morphology_g
# In[ ]:
#gRPC server bağlantısı
channel = grpc.insecure_channel('localhost:6789')
# In[ ]:
#java class atamaları
langid_stub = z_langid_g.LanguageIdServiceStub(channel)
normalization_stub = z_normalization_g.NormalizationServiceStub(channel)
preprocess_stub = z_preprocess_g.PreprocessingServiceStub(channel)
morphology_stub = z_morphology_g.MorphologyServiceStub(channel)
# In[ ]:
#train data dosya yolları
print("Eğitim dosyaları okunuyor...")
train_ekonomi_files = glob.glob("./train/ekonomi/*.txt")
train_magazin_files = glob.glob("./train/magazin/*.txt")
train_saglik_files = glob.glob("./train/saglik/*.txt")
train_spor_files = glob.glob("./train/spor/*.txt")
#test data dosya yolları
print("Test dosyaları okunuyor...")
test_ekonomi_files = glob.glob("./test/ekonomi/*.txt")
test_magazin_files = glob.glob("./test/magazin/*.txt")
test_saglik_files = glob.glob("./test/saglik/*.txt")
test_spor_files = glob.glob("./test/spor/*.txt")
# In[ ]:
# stop words
print("Stop words okunuyor...")
stops = open("./stop-words-turkish.txt").readlines()
temp = []
for i in stops:
#yeni satır karakteri silinir
k = str(i).replace("\r\n","").replace("\n","")
temp.append(k)
stops = temp
# In[ ]:
train_files_arr = [train_ekonomi_files, train_magazin_files, train_saglik_files, train_spor_files]
test_files_arr = [test_ekonomi_files, test_magazin_files, test_saglik_files, test_spor_files]
# In[ ]:
#verilen texti normalleştiren fonksiyon
def normalize(text):
#gRPC servera text gönderilir bir response alınır
res = normalization_stub.Normalize(z_normalization.NormalizationRequest(input=text))
#response error kontrolu
if res.normalized_input:
return res.normalized_input
else:
print('Problem normalizing input : ' + res.error)
# In[ ]:
#dosyaları etiketlerine göre düzenlenmesi
train_texts = []
train_labels=[]
train_file_names = []
print("Eğitim textleri normalize ediliyor...")
for files in train_files_arr:
for f in files:
with open(f) as text:
#dosya adını alma
train_file_names.append(str(f).split('\\'))
#dosya etiketini alma
if "ekonomi" in str(f):
train_labels.append("ekonomi")
if "magazin" in str(f):
train_labels.append("magazin")
if "saglik" in str(f):
train_labels.append("saglik")
if "spor" in str(f):
train_labels.append("spor")
#dosyanın textini normaleştirme işlemini yapıp döndüyü dosya texti olarak kaydetme
t = normalize(text.read())
train_texts.append(t)
# In[ ]:
#dosyaları etiketlerine göre düzenlenmesi
test_texts, test_labels, test_file_names=[],[],[]
print("Test textleri normalize ediliyor...")
for files in test_files_arr:
for f in files:
with open(f) as text:
#dosya adını alma
test_file_names.append(str(f).split("\\"))
#dosya etiketini alma
if "ekonomi" in str(f):
test_labels.append("ekonomi")
if "magazin" in str(f):
test_labels.append("magazin")
if "saglik" in str(f):
test_labels.append("saglik")
if "spor" in str(f):
test_labels.append("spor")
#dosyanın textini normaleştirme işlemini yapıp döndüyü dosya texti olarak kaydetme
t = normalize(text.read())
test_texts.append(t)
# In[ ]:
#train dataları ile dataframe oluşturma
trainDF = pd.DataFrame()
trainDF["label"] = train_labels
trainDF["file"] = [line[1] for line in train_file_names]
trainDF["text"] = train_texts
# In[ ]:
#test dataları ile dataframe oluşturma
testDF = pd.DataFrame()
testDF["label"] = test_labels
testDF["file"] = [line[1] for line in test_file_names]
testDF["text"] = test_texts
# In[ ]:
#verilen text dizisini tokenize edip verilen dataframenin sonuna ekler
def tokenize(text_arr,dataframe):
token_str = ""
tokens = []
for text in text_arr:
#gRPC servera text gönderilir bir response alınır
res = preprocess_stub.Tokenize(z_preprocess.TokenizationRequest(input=text))
for i in res.tokens:
#noktalama işaretleri, sayılar ve tarihler harici olanlar alnır
if i.type != "Punctuation" and i.type != "Number" and i.type != "Date" and i.type != "URL":
token_str += i.token+" "
tokens.append(token_str)
token_str = ""
dataframe["tokenized"] = tokens
# In[ ]:
# tokenleştirilen text için kök bulma işlemini yapar
def stemming(dataframe):
stemmed = []
stem_str = ""
for text in dataframe["tokenized"]:
#text tokenlerine ayrılır
for token in text.split(" "):
#boş satır geçilir
if token is "":
continue
#gRPC servera text gönderilir bir response alınır
res = morphology_stub.AnalyzeSentence(z_morphology.SentenceAnalysisRequest(input=str(token)))
stem_str += res.results[0].best.dictionaryItem.lemma.lower()+ " "
stemmed.append(stem_str)
stem_str = ""
dataframe["stemmed"] = stemmed
# In[ ]:
print("Textler tokenlere ayrılıyor...")
tokenize(test_texts,testDF)
tokenize(train_texts,trainDF)
# In[ ]:
#stemming kısmı uzun sürmekte
print("Textlerin kökleri bulunuyor...")
stemming(testDF)
stemming(trainDF)
# In[ ]:
#kökleri bulunmuş text içindeki stop wordsler çıkarılır
def remove_stops(dataframe):
no_stops = []
no_stop_str=""
for text in dataframe["stemmed"]:
for word in text.split(" "):
if word not in stops:
no_stop_str+=word+" "
no_stops.append(no_stop_str)
no_stop_str = ""
dataframe["no_stop"] = no_stops
# In[ ]:
print("Stop wordler çıkarılıyor...")
remove_stops(testDF)
remove_stops(trainDF)
# In[ ]:
# ngram level tf-idf
# ngram 1 - 2 - 3
# tf-idf ilk 5000 feature
print("Tf-Idf tablosu oluşturuluyor...")
tfidf_vect_ngram = TfidfVectorizer(ngram_range=(1,3), max_features=5000)
tfidf_vect_ngram.fit(trainDF['no_stop'])
xtrain_tfidf_ngram = tfidf_vect_ngram.transform(trainDF["no_stop"])
xvalid_tfidf_ngram = tfidf_vect_ngram.transform(testDF["no_stop"])
# In[ ]:
def train_model(classifier, feature_vector_train, label, feature_vector_valid):
print("Model eğitiliyor...")
# classifierın içine train datasını atar
classifier.fit(feature_vector_train, label)
# classları tahminler
predictions = classifier.predict(feature_vector_valid)
return metrics.accuracy_score(predictions, testDF["label"]),predictions
# In[ ]:
accuracy, predictions = train_model(naive_bayes.MultinomialNB(),xtrain_tfidf_ngram,trainDF["label"],xvalid_tfidf_ngram)
print ("Tutarlılık ", accuracy)
# In[ ]:
# tf-idf tablosu oluşturma
a = pd.DataFrame(xtrain_tfidf_ngram.toarray(),index=[line[1] for line in train_file_names],columns=tfidf_vect_ngram.get_feature_names())
b = pd.DataFrame(xvalid_tfidf_ngram.toarray(),index=[line[1].replace(" ","_") for line in test_file_names],columns=tfidf_vect_ngram.get_feature_names())
a["Sınıf"] = train_labels
b["Sınıf"] = test_labels
a=a.append(b)
# In[ ]:
#oluşan tf-idf tablosunu dosyaya yazma
print("Tf-Idf dosyaya yazıldı...")
a.to_csv("./tf-idf.csv")
# In[ ]:
# metrikleri hesaplama
print("Metrikler hesaplanıyor...")
cm = confusion_matrix(test_labels, predictions)
recall = np.diag(cm) / np.sum(cm, axis = 1)
recall = np.append(recall,np.mean(recall))
precision = np.diag(cm) / np.sum(cm, axis = 0)
precision=np.append(precision,np.mean(precision))
f_score = 2*(precision*recall)/(precision+recall)
# In[ ]:
#metrikleri dosyaya yazma
print("Metrikler dosyaya yazıldı...")
pc=pd.DataFrame(index=["Precision","Recall","F-Score"],columns=["ekonomi","magazin","sağlık","spor","ortalama"])
pc.iloc[0],pc.iloc[1],pc.iloc[2] = precision,recall,f_score
pc.to_csv("./performans_olcum.csv")
# In[ ]:
print(pc)