-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVM.py
More file actions
342 lines (301 loc) · 13.1 KB
/
SVM.py
File metadata and controls
342 lines (301 loc) · 13.1 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
335
336
337
338
339
340
341
import pandas as pd
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer, TfidfTransformer
from sklearn.linear_model import SGDClassifier
from sklearn.exceptions import NotFittedError
import pickle
import joblib
import pymongo
from pymongo import MongoClient
import json
from common.cmm import showTime, SAMP_DATA_DIR
from common import prs
import os
import h5py
import csv
from elasticsearch import Elasticsearch
import schedule
import time
import os.path
import traceback
import sys
import esFunc
import re
import schedule
import time
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.base import JobLookupError
from konlpy.tag import Komoran
if os.name == "nt":# 윈도우 운영체제
from eunjeon import Mecab
else:# 현재 리눅스 서버 및 맥은 konlpy으로 미캡 모듈 import
from konlpy.tag import Mecab
#### 토큰화해주는 함수 ####
def dataPrePrcs(contents):
no_kor_num=0
tagger = Mecab()
print('mecab 형태소 분석기를 실행합니다.')
#f.write("Mecab을 실행합니다.")
print('한글 외의 글자를 삭제합니다.')
#f.write("한글 외의 글자를 삭제합니다.")
hangul = re.compile('[^ ㄱ-ㅣ가-힣]+')
for j in range(len(contents)):
if re.match('[^ ㄱ-ㅣ가-힣]+',str(contents[j])):
no_kor_num+=1
contents = [hangul.sub('',str(contents[cn])) for cn in range(len(contents))]
print('한글 외의 글자를 가진',no_kor_num,'개의 문서 삭제를 완료했습니다.')
#f.write("한글 외의 글자 삭제를 완료했습니다.")
print('각 문서의 명사를 추출합니다.')
#f.write("각 문서의 명사를 추출합니다.")
#from tqdm import tqdm_notebook
from tqdm import tqdm
tokenized_doc = []
for cnt in tqdm(range(len(contents))):
#print(cnt, '번 문서의 명사를 추출합니다.')
nouns = tagger.nouns(contents[cnt])
#print(len(nouns), '토큰들을 통합합니다.')
tokenized_doc.append(nouns)
print('각 문서의 명사추출을 완료했습니다.')
#f.write("각 문서의 명사를 추출을 완료했습니다.")
# 한글자 단어들 지우기!
print('한 글자 단어를 삭제합니다.')
#f.write("한 글자 단어를 삭제합니다.")
num_doc = len(tokenized_doc)
one_word=0
for i in range(num_doc):
tokenized_doc[i] = [word for word in tokenized_doc[i] if len(word) > 1]
#print('한 글자 단어 ', one_word ,'개를 삭제를 완료했습니다.')
print("한 글자 단어를 삭제를 완료했습니다.")
#f.write("한 글자 단어를 삭제를 완료했습니다.")
return tokenized_doc
def SVMTrain():
import time
print('train data를 불러옵니다.')
#f.write("train data를 불러옵니다")
tvc=TfidfVectorizer()
##### 모델링 ######
#train data load
data=pd.read_csv("/home/dapi1/TIBigdataMiddleware/train_data/single_20110224-20210224.csv")
data.columns.to_list()
data = data.drop_duplicates()
del data['Unnamed: 0']
print('train data를 불러오는데 성공하였습니다.')
print('키워드 데이터의 가중치를 구합니다.')
start=time.time()
tvc_data=tvc.fit_transform(data["키워드"])
print('키워드 데이터의 가중치를 구하는데 성공하였습니다.')
#SVM모델 학습
svm_model=SGDClassifier(loss='hinge', penalty='l2', alpha=0.00001, random_state=42,n_iter_no_change=5)
model= svm_model.fit(tvc_data, data['주제'])
filename='model/SVM.h5'
dataname='model/tvc.pickle'
with open(filename, 'wb') as filehandle:
pickle.dump(model,filehandle, protocol=pickle.HIGHEST_PROTOCOL)
with open(dataname, 'wb') as datahandle:
pickle.dump(tvc,datahandle, protocol=pickle.HIGHEST_PROTOCOL)
TT=time.time()-start
print(TT,"시간 만에 모델훈련을 완료하였습니다.")
print("훈련한 모델을 저장하였습니다.")
return "훈련한 모델을 저장하였습니다."
def SVMTest(tokenized_doc):#북한데이터에 saved model 적용
print("저장된 모델을 불러옵니다. ")
#f.write("저장된 모델을 불러옵니다.")
filename='model/SVM.h5'
dataname='model/tvc.pickle'
# 저장된 모델을 읽어오기
with open(filename, 'rb') as filehandle:
model = pickle.load(filehandle)
with open(dataname, 'rb') as datahandle:
tvc = pickle.load(datahandle)
print("저장된 모델을 불러오는 데 성공했습니다. ")
#f.write("저장된 모델을 불러오는 데 성공했습니다.")
print("주제예측을 시작합니다.")
#f.write("주제예측을 시작합니다.")
result=list()
#북한데이터 tokenizer & predict
for i in range(len(tokenized_doc)):
if(len(tokenized_doc[i])>0):
tvc_lst=(tvc.transform(tokenized_doc[i]))#tfidfvectorizer
result.append(list(model.predict(tvc_lst))[0])#predict
else:
result.append("")#predict
print(len(result),"개의 데이터의 주제예측을 성공적으로 완료하였습니다.")
#f.write("주제예측을 성공적으로 완료하였습니다.")
return result
def Pre_date(date):
print('Elasticsearch server에 접속을 시도합니다.')
index = 'nkdb_new_210526'
es = Elasticsearch(
hosts='https://kubic-repo.handong.edu:19200',
http_auth=( 'elastic','2021HandongEPP!NTH201#!#!'),
#scheme="https",
#port=19200,
verify_certs=False
)
print('Elasticsearch server에 성공적으로 접속하였습니다.')
#이전&현재 데이터
print('Elasticsearch server에 데이터를 요청합니다.')
print("date2: ", date)
#f.write('Elasticsearch server에 데이터를 요청합니다.')
resp=es.search(
index=index,
body={
"size":100,
"query":{
"range" :{
"timestamp":{
#"type":"date",
"lte":date,#이하,
"format": "yyyy-MM-dd"
}
},
},
}, scroll='1s'
)
#old_scroll_id = resp["_scroll_id"]
sid = resp["_scroll_id"]
fetched =len(resp['hits']['hits'])
print("fetched: ",fetched)
#fetched =len(resp['hits']['hits'])
hash_key=[]
titles=[]
contents=[]
times=[]
tokenized_doc=[]
corpus=[]
corpus2=[]
count=0
print('0번부터 99번까지 100개의 데이터를 처리합니다.')
#f.write('0번부터 99번까지 100개의 데이터를 처리합니다.')
for i in range(fetched):
if "file_extracted_content" in resp['hits']['hits'][i]["_source"].keys():
hash_key.append((resp['hits']['hits'][i]["_source"]["hash_key"]))
titles.append((resp['hits']['hits'][i]["_source"]["post_title"]))
times.append((resp['hits']['hits'][i]["_source"]["timestamp"]))
contents.append((resp['hits']['hits'][i]["_source"]["file_extracted_content"]))
from os import path
resp_count = 0
while len(resp['hits']['hits']):
resp_count+=1
print(resp_count*100,'번부터', resp_count*100+99, '번까지 100개의 데이터를 처리합니다.')
#f.write(resp_count*100,'번부터', resp_count*100+99, '번까지 100개의 데이터를 처리합니다.')
resp=es.scroll(scroll_id=sid, scroll='1s')
fetched=len(resp['hits']['hits'])
for doc in resp['hits']['hits']:
if "file_extracted_content" in doc["_source"].keys():
hash_key.append((doc["_source"]["hash_key"]))
titles.append((doc["_source"]["post_title"]))
times.append((doc["_source"]["timestamp"]))
contents.append(doc["_source"]["file_extracted_content"])
#형태소 분석기
print('형태소 분석기를 실행합니다. ')
#f.write('형태소 분석기를 실행합니다. ')
tokenized_doc=dataPrePrcs(contents)
print('형태소 분석기를 실행을 완료하였습니다. ')
#f.write('형태소 분석기를 실행을 완료하였습니다. ')
return hash_key, titles, tokenized_doc, contents, times
def Post_date(date):
print('Elasticsearch server에 접속을 시도합니다.')
index = 'nkdb_new_210526'
es = Elasticsearch(
hosts='https://kubic-repo.handong.edu:19200',
http_auth=( 'elastic','2021HandongEPP!NTH201#!#!'),
#scheme="https",
#port=19200,
verify_certs=False
)
print('Elasticsearch server에 성공적으로 접속하였습니다.')
#이전&현재 데이터
print('Elasticsearch server에 데이터를 요청합니다.')
#f.write('Elasticsearch server에 데이터를 요청합니다.')
resp=es.search(
index=index,
body={
"size":100,
"query":{
"range" :{
"timestamp":{
"gt":str(date)#이하
,"format" : "yyyy-MM-dd"
}
},
},
}, scroll='1s'
)
#old_scroll_id = resp["_scroll_id"]
sid = resp["_scroll_id"]
fetched =len(resp['hits']['hits'])
titles=[]
contents=[]
times=[]
hash_key=[]
tokenized_doc=[]
corpus=[]
corpus2=[]
count=0
print('0번부터 99번까지 100개의 데이터를 처리합니다.')
#f.write('0번부터 99번까지 100개의 데이터를 처리합니다.')
for i in range(fetched):
if "file_extracted_content" in resp['hits']['hits'][i]["_source"].keys():
hash_key.append((resp['hits']['hits'][i]["_source"]["hash_key"]))
titles.append((resp['hits']['hits'][i]["_source"]["post_title"]))
times.append((resp['hits']['hits'][i]["_source"]["timestamp"]))
contents.append((resp['hits']['hits'][i]["_source"]["file_extracted_content"]))
from os import path
resp_count = 0
while len(resp['hits']['hits']):
resp_count+=1
print(resp_count*100,'번부터', resp_count*100+99, '번까지 100개의 데이터를 처리합니다.')
#f.write(resp_count*100,'번부터', resp_count*100+99, '번까지 100개의 데이터를 처리합니다.')
resp=es.scroll(scroll_id=sid, scroll='1s')
fetched=len(resp['hits']['hits'])
for doc in resp['hits']['hits']:
if "file_extracted_content" in doc["_source"].keys():
hash_key.append((doc["_source"]["hash_key"]))
titles.append((doc["_source"]["post_title"]))
times.append((doc["_source"]["timestamp"]))
contents.append(doc["_source"]["file_extracted_content"])
#형태소 분석기
print('형태소 분석기를 실행합니다. ')
#f.write('형태소 분석기를 실행합니다. ')
tokenized_doc=dataPrePrcs(contents)
print('형태소 분석기를 실행을 완료하였습니다. ')
#f.write('형태소 분석기를 실행을 완료하였습니다. ')
return hash_key, titles, tokenized_doc, contents, times
def MoEs(date):
#Mongo
print("FE MongoDB에 연결을 시도합니다.")
client=MongoClient(host='localhost',port=27017) ##################### frontend MongoDB와 접속할 수 있는 권한을 열어주게 된다면 이부분의 host수정하기!!!!
print('MongoDB에 연결을 성공했습니다.')
#f.write("MongoDB에 연결을 성공했습니다..")
db=client.topic_analysis
collection_num=db.multi_label_svm.count()
date=date
print("\n")
print(collection_num)
if collection_num==0:#최초 시작
print('svmDB에 ',collection_num,'개의 데이터가 있습니다. ')
(hash_key, doc_title, tokenized_doc, contents, times)=Pre_date(date)
print('MongoDB의 ', date, '이전의 데이터의 주제를 분석합니다.')
#f.write("MongoDB의 모든 데이터의 주제를 분석합니다.")
result=SVMTest(tokenized_doc)
else:
print('svmDB에 ',collection_num,'개의 데이터가 있습니다. ')
(hash_key, doc_title, tokenized_doc, contents, times)=Post_date(date)
print('MongoDB에 새로유입된 ',len(tokenized_doc),'개의 데이터의 주제를 분석합니다.')
#f.write("MongoDB에 새로유입된 데이터의 주제를 분석합니다.")
result=SVMTest(tokenized_doc)#갱신
print('MongoDB의 svm collection에 분석한', len(result),'개의 주제를 저장합니다.')
#f.write("MongoDB의 svm collection에 분석한 주제를 저장합니다")
for i in range(len(hash_key)):
doc={
"hash_key" : hash_key[i],
"doc_title" : doc_title[i],
"topic" : result[i],
"timestamp": times[i]
}
db.multi_label_svm.insert_one(doc)
showTime()
print('MongoDB의 svm collection에 분석한', len(result),'개의 주제를 저장을 완료했습니다.')
#f.write("MongoDB의 svm collection에 분석한 주제를 저장을 완료했습니다")
return result