-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
229 lines (167 loc) · 7.25 KB
/
gui.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
import jpype
from jpype import JClass
import pandas as pd
import numpy as np
import string
# Zembrek'i çalıştıralım
jar = r"C:/Users/Dilemre/Documents/GitHub/bitirme/zemberek-full.jar" # Zemberek'in yolu
jvmpath = r"C:/Program Files/Java/jdk-17.0.2/bin/server/jvm.dll" # JVM'nin yolu
if not jpype.isJVMStarted():
jpype.startJVM(jvmpath=jvmpath, classpath=jar)
try:
TurkishMorphology = JClass('zemberek.morphology.TurkishMorphology')
TurkishSentenceExtractor = JClass('zemberek.tokenization.TurkishSentenceExtractor')
except:
print("wrong path")
morphology = TurkishMorphology.createWithDefaults()
extractor = TurkishSentenceExtractor.DEFAULT
###
stopwords = [x.strip() for x in open('stop-words.txt','r', encoding="UTF8").read().split('\n')]
def dataCleaning(text):
"""Satır boşluklarını kaldır, metini küçük harfe çevir, noktalama işaretlerini kaldır"""
text = text.replace("\n", " ")
text = text.lower()
text = "".join([i for i in text if (i.isalnum() or i == " ")])
return " ".join(text.split())
def removeStopwords(text):
"""Zemberek'ten aldığımız stopword kelimelerini kaldırır"""
for word in text:
if word in stopwords or word in string.whitespace:
text.remove(word)
return text
def wordTokenize(text):
"""Önişlenmiş metini kelimelere ayırır ve stopword'leri kaldırır"""
text = text.split(" ")
text = removeStopwords(text)
return text
def sentTokenize(text):
"""Önişlenmemiş metini cümlelerine ayırır, bunişlem sırasında önişleme yapar ve stopword'leri kaldırır """
sent_list = []
text = text.replace("\"", "")
results: TurkishSentenceExtractor = extractor.fromDocument(jpype.JString(text))
for result in results:
result = dataCleaning(str(result))
result = removeStopwords(result.split(" "))
if len(result) == 0:
continue
else:
sent_list.append(" ".join(result))
return sent_list
#### Burada UNK kelimeleri UNK olarak alıcak şekilde düzenlenmeli - hatta unk kelime sayısı da nitelik olarak eklenmeli
def lemmas(word_list):
"""Kelime token'larından kök tokenları oluşturur"""
lemma = []
for word in word_list:
result = str(morphology.analyzeAndDisambiguate(word).bestAnalysis()[0].getLemmas()[0])
if result == "UNK":
lemma.append(result)
else:
lemma.append(result)
return lemma
def wtDist(wt):
"""Kelimelerin dağılımları"""
wt_dist = dict()
wt_dist.fromkeys(set(wt))
for i in set(wt):
wt_dist[i] = wt.count(i)
return wt_dist
def wtLenDist(wt):
"""Kelimelerin harf olarak uzunluk dağılımlarını çıkarır"""
wt_len = [len(str(word)) for word in wt]
wt_len_dist = dict()
wt_len_dist.fromkeys(range(1, 29))
for i in range(0, 29):
wt_len_dist[i] = wt_len.count(i)
return wt_len_dist
def stLenDist(st):
"Cümlelerin kelime olarak uzunluk dağılımlarını çıkarır"
st_len = [len(wordTokenize(sent)) for sent in st]
st_len_dist = dict()
st_len_dist.fromkeys(range(1, 29))
for i in range(0, 29):
st_len_dist[i] = st_len.count(i)
return st_len_dist
def typeTokenRatio(wt):
"""Kelimenin toplam kelime sayısına oranı"""
return len(set(wt))/len((wt))
def avgWtLen(wt):
return sum(len(word) for word in wt)/len(wt)
def avgStLen(st):
return sum(len(wordTokenize(sent)) for sent in st)/len(st)
def puncNum(txt):
return len([x for x in txt if x in string.punctuation])
def numStopwords(clean_text):
return len([w for w in clean_text.split() if w in stopwords])
def numUpper(raw_text):
p = string.punctuation + "’" + "“" + "”"
raw_text = ''.join(' ' if c in p else c for c in raw_text)
return len([w for w in raw_text.split() if str(w).isupper()])
###
import pickle
with open('./model/mapping.pkl', 'rb') as f:
mapping = pickle.load(f)
with open('./model/scaler.pkl', 'rb') as f:
scaler = pickle.load(f)
with open('./model/count_vectorize.pkl', 'rb') as f:
count_vectorize = pickle.load(f)
with open('./model/tfidf_vector.pkl', 'rb') as f:
tfidf_vector = pickle.load(f)
with open('./model/wtDict_vector.pkl', 'rb') as f:
wtDict_vector = pickle.load(f)
with open('./model/stDict_vector.pkl', 'rb') as f:
stDict_vector = pickle.load(f)
with open('./model/SVM.pkl', 'rb') as f:
svm = pickle.load(f)
###
def processing(text):
data = pd.DataFrame(columns = ["text"])
data = data.append({"text" : text}, ignore_index=True)
data['clean_text'] = data['text'].apply(lambda x : dataCleaning(x))
data['word_token'] = data['clean_text'].apply(lambda x : wordTokenize(x))
data['sent_token'] = data['text'].apply(lambda x : sentTokenize(x))
data['lemma_token'] = data['word_token'].apply(lambda x : lemmas(x))
data['ltDist'] = data['lemma_token'].apply(lambda x : wtDist(x))
data['wtLenDist'] = data['word_token'].apply(lambda x : wtLenDist(x))
data['stLenDist'] = data['sent_token'].apply(lambda x : stLenDist(x))
data['ttr'] = data['word_token'].apply(lambda x : typeTokenRatio(x))
data['lttr'] = data['lemma_token'].apply(lambda x : typeTokenRatio(x))
data['avgWtLen'] = data['word_token'].apply(lambda x : avgWtLen(x))
data['avgStLen'] = data['sent_token'].apply(lambda x : avgStLen(x))
data['puncNum'] = data['text'].apply(lambda x : puncNum(x))
data['numStopwords'] = data['clean_text'].apply(lambda x : numStopwords(x))
data['numUpper'] = data['text'].apply(lambda x : numUpper(x))
# Vectorizing
normalizedLabels = scaler.transform(data.loc[:, 'ttr':'numUpper'])
x = np.array(data.lemma_token)
for i in range(0, len(data.lemma_token)):
x[i] = " ".join(data.lemma_token[i])
sparce_matrix = count_vectorize.transform(x).toarray()
sparce_matrix = (sparce_matrix - sparce_matrix.min())/(sparce_matrix.max() - sparce_matrix.min())
tfidf_matrix = tfidf_vector.transform(x).toarray()
wtLenDist_matrix = wtDict_vector.transform(data.wtLenDist).toarray()
stLenDist_matrix = stDict_vector.transform(data.stLenDist).toarray()
wtLenDist_matrix = (wtLenDist_matrix - wtLenDist_matrix.min())/(wtLenDist_matrix.max() - wtLenDist_matrix.min())
stLenDist_matrix = (stLenDist_matrix - stLenDist_matrix.min())/(stLenDist_matrix.max() - stLenDist_matrix.min())
attribution = (tfidf_matrix, sparce_matrix, wtLenDist_matrix, stLenDist_matrix, normalizedLabels)
attribution = np.concatenate(attribution, axis = 1)
pred_svm = svm.predict(attribution)
return pred_svm
###
import PySimpleGUI as sg
sg.theme('BluePurple')
layout = [ [sg.Text('Köşe yazısını giriniz.')],
[sg.Multiline(size=(50, 20))],
[sg.Button('Yazarı bul')],
[sg.Text('Yazar: '), sg.Text(size=(30,1), key='-OUTPUT-')]]
window = sg.Window('Yazar Bulma', layout)
while True:
event, values = window.read()
result = processing(values[0])
if event == 'Yazarı bul':
# Update the "output" text element to be the value of "input" element
window['-OUTPUT-'].update(mapping[result[0]])
if event == sg.WIN_CLOSED:
break
print('Author: ', mapping[result], result)
window.close()
###