2017.11.25) Konlpy를 활용한 야민정음 해석기 part.4

2017. 11. 25. 14:02프로젝트(완료)/야민정음 해석기

gui프로그래밍과

점수 체계를 더욱 발전시켜서 정확도를 높혔다.


코드

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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
import konlpy
import sys
from PyQt5.QtWidgets import *
 
def yaminList(p0):
    rep_arr_small = [
 
        ["대""머"],
        ["머""대"],
 
        ["파""과"],
        ["과""파"],
 
        ["댕""멍"],
        ["멍""댕"],
 
        ["피""괴"],
        ["괴""피"],
 
        ["커""귀"],
        ["귀""커"],
        ["멋""댓"],
        ["댓""멋"],
        ["괏""팟"],
        ["팟""괏"],
 
 
        ["늒""뉴"],
        ["앟""왕"],
        ["팡""광"],
        ["비""네"],
        ["ㄹ""근"],
        ["흑""후"],
        ["맣""싸"],
        ["늑""누"],
        ["띠""며"],
        ["며""띠"],
        ["빅""넥"],
        ["윽""우"],
        ["판""관"],
        ["믁""무"],
        ["㕦""못"],
        ["거""지"],
        ["븍""부"],
        ["지""거"],
        ["거""지"],
        ["싀""식"],
        ["튽""장"],
        ["넨""빈"],
        ["띵""명"],
        ["극""구"],
        ["긘""리"],
        ["뫼""민"],
        ["익""의"],
        ["읶""위"],
        ["돠""단"],
        ["뫄""만"],
        ["E""든"],
        ["네""비"],
        ["화""한"],
        ["틴""된"],
        ["즉""주"],
        ["화""한"],
        ["슥""수"],
        ["티""되"],
        ["뢰""린"],
        ["외""인"],
        ["믂""뮤"],
        ["윾""유"],
        ["봐""반"],
    ]
    rep_arr = rep_arr_small * 10
    rep_arr2 = [
        ["01""이"],
        ["7ㅣ""기"],
        ["SE""또"],
    ]
 
    for i in range(0len(rep_arr)):
       for j in range(0len(p0)):
            temp = p0[j]
            for k in range(0len(p0[j])):
                if temp[k] == rep_arr[i][0]:
                    temp = temp[:k] + rep_arr[i][1+ temp[k + 1:]
                    isex = False
                    for f in range(0len(p0)):
                        if p0[f] == temp:
                            isex = True
                    if not isex:
                        p0.append(temp)
 
    for i in range(0len(rep_arr2)):
        for j in range(0len(p0)):
            temp = p0[j]
            for k in range(0len(temp) - 1):
                try:
                    if (temp[k] + temp[k + 1]) == rep_arr2[i][0]:
                        temp = temp[:k] + rep_arr2[i][1+ temp[k + 2:]
                        isex = False
                        for f in range(0len(p0)):
                            if p0[f] == temp:
                                isex = True
                        if not isex:
                            p0.append(temp)
                except:
                    pass
 
    return p0
 
 
def yamin(p0):
    rtn = []
    rtn.append(p0)
    rtn = yaminList(rtn)
 
    return rtn
 
 
def nounNext(p0):
    if not (p0.find('SC'== -1or \
            not (p0.find('MAJ'== -1or \
            not (p0.find('JKS'== -1or \
            not (p0.find('JKC'== -1or \
            not (p0.find('JKG'== -1or \
            not (p0.find('JKO'== -1or \
            not (p0.find('JKB'== -1or \
            not (p0.find('JKV'== -1or \
            not (p0.find('JKQ'== -1or \
            not (p0.find('JC'== -1or \
            not (p0.find('JC'== -1or \
            not (p0.find('XSN'== -1):
        return 4
    return 0
 
 
def adjNext(p0):
    if not (p0.find('ETM'== -1or \
            not (p0.find('EP'== -1or \
            not (p0.find('EC'== -1):
        return 4
    return 0
 
 
def verbNext(p0):
    if not (p0.find('EP'== -1or \
            not (p0.find('ETM'== -1or \
            not (p0.find('NNB'== -1):
        return 4
    return 0
 
 
def gwanNext(p0):
    if not (p0.find('NNG'== -1or \
            not (p0.find('NNP'== -1or \
            not (p0.find('NNB'== -1or \
            not (p0.find('NR'== -1or \
            not (p0.find('NP'== -1):
        return 4
    return 0
 
 
def VCPNext(p0):
    if not (p0.find('EF'== -1):
        return 4
    return 0
 
 
def XRNext(p0):
    if not (p0.find('XSV'== -1or \
            not (p0.find('XSA'== -1):
        return 4
    return 0
 
 
def longscore(p0):
    if len(p0) == 1:
        return 0
    elif len(p0) == 2:
        return 4
    elif len(p0) == 3:
        return 16
    elif len(p0) == 4:
        return 64
    else:
        return 256
 
 
 
def comfile(p0):
    words = []
    score = []
    for arr in p0:
        words.append(konlpy.tag.Mecab().pos(arr))
        score.append(0)
    for i in range(0len(p0)):
        print(words[i])
        for j in range(0len(words[i])):
            for k in words[i][j][1].split('+'):
 
                if words[i][j][0== '슥':
                    score[i] -= 4
 
                if words[i][j][0== '머':
                    score[i] -= 1
 
                if words[i][j][0== '지':
                    score[i] += 4
                    try:
                        if words[i][j + 1][0== '게':
                            score[i] += 4
                    except:
                        pass
                try:
                    if words[i][j][0== '이':
                        if words[i][j + 1][1== "SN":
                            score[i] -= 4
                except:
                    pass
                try:
                    if words[i][j][0+ words[i][j + 1][0== '이며':
                        score[i] += 6
 
                except:
                    pass
                if words[i][j][0== '멍멍이':
                    score[i] += 8
                if k == 'NNG':
                    if words[i][j][0== '현지':
                        score[i] += 4
                    try:
                        score[i] += nounNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += 2 * longscore(words[i][j][0])
 
                if k == 'NNP':
                    try:
                        score[i] += nounNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += longscore(words[i][j][0])
 
                if k == 'NNB':
                    try:
                        if words[i][j][0== "거":
                            if words[i][j + 1][0== "에서":
                                score[i] += 4
                            if words[i][j + 1][0== "라":
                                score[i] += 4
                    except:
                        pass
                    if words[i][j][0== "건지":
                        score[i] += 6
                    try:
                        score[i] += nounNext(words[i][j + 1][1])
                    except:
                        pass
 
                if k == 'NR':
                    try:
                        score[i] += nounNext(words[i][j + 1][1])
                    except:
                        pass
 
                if k == 'NP':
                    if words[i][j][0== "누구":
                        score[i] += 4
                    try:
                        score[i] += nounNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += longscore(words[i][j][0])
 
                if k == 'VV':
                    try:
                        score[i] += verbNext(words[i][j][1].split('+')[1])
                        score[i] += verbNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += 2 * longscore(words[i][j][0])
 
                if k == 'VA':
                    if words[i][j][0== "귀엽":
                        score[i] += 8
                    try:
                        score[i] += adjNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += longscore(words[i][j][0])
 
                if k == 'VCP':
                    try:
                        score[i] += VCPNext(words[i][j + 1][1])
                    except:
                        pass
 
                if k == 'VCN':
                    try:
                        score[i] += VCPNext(words[i][j + 1][1])
                    except:
                        pass
 
                if k == 'MM':
                    try:
                        score[i] += gwanNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += longscore(words[i][j][0])
 
                if k == 'MAG':
                    score[i] += longscore(words[i][j][0])
                    if words[i][j][0== '단지':
                        score[i] += 6
                if k == 'EC':
                    if words[i][j][0== '지만':
                        score[i] += 10
                    if words[i][j][0== '라며':
                        score[i] += 4
                    if words[i][j][0== '인지':
                        score[i] += 4
                    if words[i][j][0== '네요':
                        score[i] += 10
                if k == 'XSN':
                    try:
                        score[i] += nounNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += 2 * longscore(words[i][j][0])
 
                if k == 'XSV':
                    if words[i][j][0== '되':
                        score[i] += 4
                    try:
                        score[i] += verbNext(words[i][j][1].split('+')[1])
                        score[i] += verbNext(words[i][j + 1][1])
                    except:
                        pass
 
                if k == 'XSA':
                    try:
                        score[i] += adjNext(words[i][j + 1][1])
                    except:
                        pass
 
                if k == 'XR':
                    try:
                        score[i] += XRNext(words[i][j + 1][1])
                    except:
                        pass
                    score[i] += longscore(words[i][j][0])
                if k == 'SH':
                    score[i] -= 64
 
                if k == 'SL':
                    score[i] -= longscore(words[i][j][0])
                    score[i] -= 4
                if k == 'XPN':
                    if words[i][j][0== "명":
                        score[i] += 4
                if k == 'SN':
                    try:
                        if words[i][j + 1][0== '이':
                            score[i] -= 4
                    except:
                        pass
                    score[i] -= 6
 
                if k == 'UNKNOWN':
                    score[i] -= longscore(words[i][j][0])
                    score[i] -= 4
 
    highscore = score[0]
 
    for i in range(0len(p0)):
        if highscore <= score[i]:
            highscore = score[i]
 
    high = []
    for i in range(0len(p0)):
        if highscore == score[i]:
            high.append(i)
    rtn = []
    for i in high:
        rtn.append(p0[i])
 
    return rtn
 
 
 
 
class MyWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setupUI()
 
    def setupUI(self):
        self.setGeometry(640360640360)
        self.setWindowTitle("아민정음 해독기")
        global lineEdit
        lineEdit = QLineEdit("", self)
        lineEdit.setGeometry(20,20,520,20)
        lineEdit.returnPressed.connect(self.btn1_clicked)
 
        btn1 = QPushButton("입력", self)
        btn1.setGeometry(560206020)
        btn1.clicked.connect(self.btn1_clicked)
        global viewresult
        viewresult = QLabel("결과: ", self)
        viewresult.setGeometry(20,40,600,300)
 
    def btn1_clicked(self):
        global viewresult
        global lineEdit
        viewr = "결과: "
        result = comfile(yamin(lineEdit.text()))
        for arr in result:
            viewr += "\n"+arr
 
        viewresult.setText(viewr)
 
 
 
if __name__ == "__main__":
    app = QApplication(sys.argv)
    mywindow = MyWindow()
    mywindow.show()
    app.exec_()
 
 
 
 

cs


실행 화면