-
Notifications
You must be signed in to change notification settings - Fork 0
/
Meissy.py
348 lines (201 loc) · 8.73 KB
/
Meissy.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
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
import re
import konlpy
from konlpy.tag import Okt
import numpy as np
import matplotlib.pyplot as plt
import urllib.request
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.utils import to_categorical
from sklearn.model_selection import train_test_split
from soyspacing.countbase import CountSpace
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Embedding, LSTM, Bidirectional, TimeDistributed
from tensorflow.keras.optimizers import Adam
#dictionary 만들기
def make_predefined_dictionaries() :
stopwords = open("/home/ubuntu/python_test/library/stopwords.txt","r")
#stopwords = open("/content/saebyeok/stopwords.txt","r")
stopwords = stopwords.readlines()
dic_stopword = [stopword.replace('\n','') for stopword in stopwords if stopword.startswith('#') == False and len(stopword.strip()) > 0]
names = open("/home/ubuntu/python_test/library/name.train.txt","r")
#names = open("/content/saebyeok/name.train.txt","r")
names = names.readlines()
dic_names = [name.replace('\n','').split()[0] for name in names if name.startswith('#') == False and len(name.strip()) > 0]
chains = open("/home/ubuntu/python_test/library/chain.train.txt","r")
#chains = open("/content/saebyeok/chain.train.txt","r")
chains = chains.readlines()
dic_chains = [chain.replace('\n','').split()[0] for chain in chains if chain.startswith('#') == False and len(chain.strip()) > 0]
dic_phone_numbers = open("/home/ubuntu/python_test/library/phone.train.txt","r")
#dic_phone_numbers = open("/content/saebyeok/phone.train.txt","r")
dic_phone_numbers = dic_phone_numbers.readlines()
dic_phone_numbers = [phone_number.replace('\n','').split()[0] for phone_number in dic_phone_numbers if phone_number.startswith('#') == False and len(phone_number.strip()) > 0]
return [dic_stopword, dic_names, dic_chains, dic_phone_numbers]
#전처리
def spacing(text) :
spacing_model = CountSpace()
spacing_model.load_model("/home/ubuntu/python_test/model/spacing",json_format=False)
corrected_sentence, tag = spacing_model.correct(text)
return corrected_sentence
def clean(text) :
pattern = '([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)'
text = re.sub(pattern=pattern, repl='', string=text)
pattern= '(http|ftp|https)://(?:[-\w.]|(?:%[\da-fA-F]{2}))+'
text = re.sub(pattern=pattern, repl='', string=text)
pattern = '([ㄱ-ㅎㅏ-ㅣ]+)'
text = re.sub(pattern=pattern, repl='', string=text)
pattern = '<[^>]*>'
text = re.sub(pattern=pattern, repl='', string=text)
pattern = '[^\w\s\n]'
text = re.sub(pattern=pattern, repl='', string=text)
return text
def get_morphs(text) :
dic_stopword, dic_names, dic_chains, dic_phone_numbers = make_predefined_dictionaries()
text = clean(text)
text = spacing(text)
okt = Okt()
nouns = okt.morphs(text)
#걸러내기 : 한글자 이하 / 욕설, 성적인 언급 등 제외
preprocessed_words=[]
for noun in nouns :
if len(noun) > 1 and noun not in dic_stopword :
preprocessed_words.append(noun)
return preprocessed_words
def rule_based(nouns) :
dic_stopword, dic_names, dic_chains, dic_phone_numbers = make_predefined_dictionaries()
rule_result = []
remove_target = []
#주어진 명사가 두글자에서 세글자사이라면 이름을 체크
for noun in nouns :
if len(noun) <= 3 :
r = re.compile(".*{0}.*".format(noun))
matched_name = list(filter(r.match,dic_names))
if len(matched_name) > 0 :
rule_result.append({'entity' : 'B_PS', 'value' : matched_name[0]})
remove_target.append(noun)
for target in remove_target :
nouns.remove(target)
#체인으로 체크
remove_target = []
for noun in nouns :
r = re.compile(".*{0}.*".format(noun.upper()))
matched_chain = list(filter(r.match,dic_chains))
if len(matched_chain) > 0 :
rule_result.append({'entity' : 'B_OG', 'value' : matched_chain[0]})
remove_target.append(noun)
for target in remove_target :
nouns.remove(target)
#전화번호와 년도가 잘 되지 않는 군
#숫자로만 이루어져 있으면 전화번호로 분류
remove_target = []
for noun in nouns :
if noun.isnumeric():
rule_result.append({'entity' : 'B_PN', 'value' : noun})
remove_target.append(noun)
for target in remove_target :
nouns.remove(target)
#월, 일, 년이 들어가 있으면 날짜로 분
remove_target = []
for noun in nouns :
if re.match('.*[월|일|년].*', noun):
rule_result.append({'entity' : 'B_DT', 'value' : noun})
remove_target.append(noun)
for target in remove_target :
nouns.remove(target)
return rule_result
#모델 학습
def train() :
tagged_sentences =[]
sentence = []
sentences = open("/home/ubuntu/python_test/library/ner_dataset.txt","r")
#sentences = open("/content/saebyeok/ner_dataset.txt","r")
sentences = sentences.readlines()
for s in sentences :
if s.startswith(';') or s.startswith('$') or s.startswith('#') or len(s.strip())==0 :
if len(sentence) > 0 :
tagged_sentences.append(sentence)
sentence = []
continue
s.replace('\n','')
s = s.split('\t')
if len(s) != 4 :
continue
sentence.append([s[1].lower(),s[3].replace('\n','')])
sentences, ner_tags = [],[]
for tagged_sentence in tagged_sentences :
sen, tag_info = zip(*tagged_sentence)
sentences.append(list(sen))
ner_tags.append(list(tag_info))
vocab_size = len(sentences)
src_tokenizer = Tokenizer(num_words = vocab_size, oov_token='OOV')
src_tokenizer.fit_on_texts(sentences)
tar_tokenizer = Tokenizer()
tar_tokenizer.fit_on_texts(ner_tags)
tag_size = len(tar_tokenizer.word_index) + 1
X_train = src_tokenizer.texts_to_sequences(sentences)
y_train = tar_tokenizer.texts_to_sequences(ner_tags)
index_to_word = src_tokenizer.index_word
index_to_ner = tar_tokenizer.index_word
decoded = []
for index in X_train[0] :
decoded.append(index_to_word[index])
max_len=70
X_train = pad_sequences(X_train, padding='post', maxlen=max_len)
y_train = pad_sequences(y_train, padding='post', maxlen=max_len)
X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=.2, random_state=777)
y_train = to_categorical(y_train, num_classes = tag_size)
y_test = to_categorical(y_test, num_classes = tag_size)
embedding_dim = 128
hidden_units = 128
model = Sequential()
model.add(Embedding(input_dim=vocab_size, output_dim=embedding_dim, input_length=max_len, mask_zero=True))
model.add(Bidirectional(LSTM(hidden_units, return_sequences=True)))
model.add(TimeDistributed(Dense(tag_size, activation='softmax')))
model.compile(loss='categorical_crossentropy', optimizer=Adam(0.001), metrics=['accuracy'])
model.fit(X_train,y_train,batch_size=128,epochs=8,validation_data=(X_test,y_test))
model.save('/home/ubuntu/python_test/meissy0.02')
#model.save('/content/saebyeok/meissy0.02')
def predict(text) :
from tensorflow import keras
index_to_ner = {1: 'o', 2: 'i', 3: 'b_dt', 4: 'b_og', 5: 'b_ps', 6: 'b_lc', 7: 'b_ti'}
rule_based_result = []
predict_result = []
#rule based
nouns = get_morphs(text)
rule_based_result = rule_based(nouns)
if len(nouns) == 0 :
return rule_based_result
tagged_sentences =[]
sentence = []
sentences = open("/home/ubuntu/python_test/library/ner_dataset.txt","r")
#sentences = open("/content/saebyeok/ner_dataset.txt","r")
sentences = sentences.readlines()
for s in sentences :
if s.startswith(';') or s.startswith('$') or s.startswith('#') or len(s.strip())==0 :
if len(sentence) > 0 :
tagged_sentences.append(sentence)
sentence = []
continue
s.replace('\n','')
s = s.split('\t')
if len(s) != 4 :
continue
sentence.append([s[1].lower(),s[3].replace('\n','')])
sentences = []
for tagged_sentence in tagged_sentences :
sen, tag_info = zip(*tagged_sentence)
sentences.append(list(sen))
vocab_size = len(sentences)
src_tokenizer = Tokenizer(num_words = vocab_size, oov_token='OOV')
src_tokenizer.fit_on_texts(sentences)
predict_target = src_tokenizer.texts_to_sequences([nouns])
predict_target = pad_sequences( predict_target, padding='post', maxlen=70)
loded_model = keras.models.load_model('/home/ubuntu/python_test/meissy0.02')
y_predicted = loded_model.predict(np.array([predict_target[0]]))
y_predicted = np.argmax(y_predicted,axis=-1)
index = 0
for word, pred in zip( predict_target[0], y_predicted[0]) :
if word != 0 :
predict_result.append({"entity" : index_to_ner[pred].upper(), "value" : nouns[index]})
index = index + 1
return predict_result + rule_based_result