-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbojung.py
273 lines (255 loc) · 9.62 KB
/
bojung.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
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
import hgtk
import textdistance
import os
import csv
import pandas as pd
import time
import correcting as cor
# 격조사, 접속조사 (무조건 단어 뒤에 붙어어있음)
josa1_high = ['에서', '에게서', '께서', '를', '에게', '는', '랑', '께서', '이에게', '이하고','처럼']
# low: 길이별로
josa1_low1 = ['의', '가', '을', '은', '와', '과', '나', '이', '며']
josa1_low2 = ['에다', '하고', '이가', '이는', '이랑', '이나', '이와', '이며']
# 보조사
# 무조건보조사
josa2_high = ['까지', '만큼', '이라도', '커녕', '부터', '이나마']
josa2 = ['만', '도', '마저', '나마', '마다', '라도', '치고']
# 서술격조사 : 마침표 찍는용
josa3_s0 = ['다', '요']
josa3_s = ['하다', '에요', '죠', '지요']
#이게 있으면 무조건 서술어
josa3_sb = ['ㅇㅣᴥㄷㅏ', 'ㅂᴥㄴㅣᴥㄷㅏ', 'ㄹᴥㄲㅏ', 'ㅆᴥㄷㅏ', 'ㅆᴥㅅㅗ', 'ㄴᴥㄷㅏ', 'ㄶᴥㄷㅏ', 'ㅆᴥㅇㅓᴥㅇㅛ', 'ㅆᴥㅈㅛ']
def bojung_csv(type_num,loc,a):
#print("bojung csv - ",type_num)
# num =session type
inittime = time.time()
a_split = a[2].split() # a_split에는 띄어쓰기별 단어 저장됨
n = []
stopfile = loc+'stopword_post.csv'#'./stopword_post.csv'
stopdata = pd.read_csv(stopfile, encoding='cp949')
stopset = list(stopdata.stopword)
for word in a_split:
d = []
f = open(loc+'newExample'+str(type_num)+'.csv','r', encoding='utf-8')
rdr = csv.reader(f)
new_word = word
j = ''
geok_flag = 0
flag = 0
# stopset 검사
if word in stopset:
#print("원본그대로 stopword")
flag=1
n.append(word)
if flag:
continue
# 조사 여부 검사
# 서술격조사(확률높은) 확인
for josa in josa3_sb:
if josa in hgtk.text.decompose(word):
flag=1
n.append(hgtk.text.compose(word)+'.')
break
if flag:
continue
# 격조사(확률높은) 확인
for josa in josa1_high:
if josa in word:
#print('확률높은격조사')
idx = word.index(josa)
new_word = word[:idx]
j = word[idx:]
geok_flag = 1
break
# print('격조사1: ', time.time()-inittime)
if not geok_flag:
if len(word)>1:
for josa in josa1_low1:
if word[-1]==josa:
new_word = word[:-1]
j = josa
geok_flag = 1
break
# print('격조사2: ', time.time()-inittime)
if not geok_flag:
if len(word)>2:
for josa in josa1_low1:
if word[-2]==josa:
new_word = word[:-2]
j = josa
geok_flag = 1
break
# 조사제거후 stopset검사
if new_word in stopset:
#print('조사제거후 stopset')
flag=1
n.append(word)
if flag:
continue
if geok_flag:
wordlen=len(new_word)
else:
wordlen = len(word)
word = hgtk.text.decompose(new_word)
# csv 파일 한줄씩 접근해서 작은값 넣기
for line in rdr:
if abs(wordlen-len(line[0]))<3:
if word[0] == line[1][0]:
tmp = textdistance.levenshtein(word, line[1])
num = word.count('ᴥ')
if tmp < num:
d.append((tmp, line[0]))
# 최솟값에 해당하는 단어를 n에 넣음
if d:
#print(min(d)[0])
#print(min(d)[1])
########################################################
if(min(d)[0]>1 and min(d)[0]<=2):
line=hgtk.text.decompose(min(d)[1])
result,new=cor.checkYH(word,line)
new=hgtk.text.decompose(new)
result,new=cor.checkae(new,line)
new=hgtk.text.decompose(new)
result,new=cor.checkou(new,line)
new=hgtk.text.decompose(new)
result,new=cor.checkEndStart(new,line)
new=hgtk.text.decompose(new)
result,new=cor.checkNiuen(new,line)
n.append(new+j)
############################################################3
else:
word = min(d)[1]+j
n.append(word)
else:
n.append(hgtk.text.compose(word)+j)
#print(n)
# print('append: ', time.time()-inittime)
#print('소요시간: ', time.time()-inittime)
return (a[0],a[1],' '.join(n))
def bojung(dataset,loc,a):
# num =session type
inittime = time.time()
a_split = a[2].split() # a_split에는 띄어쓰기별 단어 저장됨
n = []
stopfile = loc+'stopword_post.csv'#'./stopword_post.csv'
stopdata = pd.read_csv(stopfile, encoding='cp949')
stopset = list(stopdata.stopword)
for word in a_split:
d = []
#f = open(loc+'newExample'+str(num)+'.csv','r', encoding='utf-8')
#rdr = csv.reader(f)
new_word = word
j = ''
geok_flag = 0
flag = 0
# stopset 검사
if word in stopset:
#print("원본그대로 stopword")
flag=1
n.append(word)
if flag:
continue
# 조사 여부 검사
# 서술격조사(확률높은) 확인
for josa in josa3_sb:
if josa in hgtk.text.decompose(word):
flag=1
n.append(hgtk.text.compose(word)+'.')
break
if flag:
continue
# 격조사(확률높은) 확인
for josa in josa1_high:
if josa in word:
#print('확률높은격조사')
idx = word.index(josa)
new_word = word[:idx]
j = word[idx:]
geok_flag = 1
break
# print('격조사1: ', time.time()-inittime)
if not geok_flag:
if len(word)>1:
for josa in josa1_low1:
if word[-1]==josa:
new_word = word[:-1]
j = josa
geok_flag = 1
break
# print('격조사2: ', time.time()-inittime)
if not geok_flag:
if len(word)>2:
for josa in josa1_low1:
if word[-2]==josa:
new_word = word[:-2]
j = josa
geok_flag = 1
break
# 조사제거후 stopset검사
if new_word in stopset:
#print('조사제거후 stopset')
flag=1
n.append(word)
if flag:
continue
if geok_flag:
wordlen=len(new_word)
else:
wordlen = len(word)
word = hgtk.text.decompose(new_word)
# csv 파일 한줄씩 접근해서 작은값 넣기
for line in dataset:
#print(line)
line_word = hgtk.text.decompose(line)
if abs(wordlen-len(line))<3:
tmp = textdistance.levenshtein(word,line_word)
num = word.count('ᴥ')
#print("word",word)
#print("tmp",tmp)
#print("num",num)
if tmp < num:
d.append((tmp, line))
# 최솟값에 해당하는 단어를 n에 넣음
if d:
#print(min(d)[0])
#print("correct:",min(d)[1])
########################################################
if(min(d)[0]>1 and min(d)[0]<=2):
line=hgtk.text.decompose(min(d)[1])
result,new=cor.checkYH(word,line)
new=hgtk.text.decompose(new)
result,new=cor.checkae(new,line)
new=hgtk.text.decompose(new)
result,new=cor.checkou(new,line)
new=hgtk.text.decompose(new)
result,new=cor.checkEndStart(new,line)
new=hgtk.text.decompose(new)
result,new=cor.checkNiuen(new,line)
n.append(new+j)
############################################################3
else:
word = min(d)[1]+j
n.append(word)
else:
n.append(hgtk.text.compose(word)+j)
#print(n)
# print('append: ', time.time()-inittime)
#print('소요시간: ', time.time()-inittime)
return (a[0],a[1],' '.join(n))
# db에서 데이터를 튜플 형태로 받아왔다고 가정
#a=("14:24:20","lyj","구로피우스는 모더니즘을 대표하는 독일의 건축가이다 바우하우스의 창립자이다")
#a=("14:24:23","kyh","스패인 알카싸르에서 이술람 양식을 엿볼 수 있습니다")
#a=("14:24:26","byh","색상완은 가시강선의 스팩트럼을 고리형태로 연결하여 색을 배열한 것을 말합니다")
"""
a4=("14:24:30","osh","태피스트리는 고대 이집스를 비롯하여 중세 초기의 페르샤를 중심으로 화려한 작품이 많이 나왔는데")
a5=("14:24:32","osh","가브리앨은 미가엘처럼 유대교와 기독고, 그리고 이슬람교에서 중요한 위치를 차지한다")
print('Initial : ', end='')
print(a4)
print('Corrected : ', end='')
print(bojung(a4))
print("\n")
print('Initial : ', end='')
print(a5)
print('Corrected : ', end='')
print(bojung(a5))
"""