-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkospi_sentiment_batch.py
308 lines (212 loc) · 9.74 KB
/
kospi_sentiment_batch.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
#!/usr/bin/env python
# coding: utf-8
# In[276]:
# import FinanceDataReader as fdr
import pandas as pd
import numpy as np
from tqdm import tqdm
import warnings
import datetime
import pymssql
import math
import time
import re
class kospi200_API:
def __init__(self):
self.startday = str(datetime.datetime.now() - datetime.timedelta(days=3))[:10]
self.yesterday = str(datetime.datetime.now() - datetime.timedelta(days=1))[:10]
# self.startday = startday
# self.yesterday = yesterday
self.kospi200 = pd.read_csv('C:/Users/남건우/Desktop/batch/data/kospi_200.csv')
for i in range(200):
p = (6 - len(str(self.kospi200['종목코드'][i]))) * str(0)
self.kospi200.loc[i,'종목코드'] = p+str(self.kospi200['종목코드'][i])
print(self.startday, self.yesterday)
def news_load(self):
## 테이블 생성
conn = pymssql.connect(host=r"10.101.34.221", user='sa', password='vmfkdla2006', database='BWPRIME', charset='UTF-8')
cursor = conn.cursor()
sql = f"SELECT * FROM NEWS_LIST_NAVER WHERE NEWS_DATE BETWEEN '{self.startday} 00:00:00' AND '{self.yesterday} 23:59:59.997'"
cursor.execute(sql)
rows = cursor.fetchall()
conn.commit()
conn.close()
col = ['IDX', 'CATEGORY','NEWS_DATE', 'TITLE', 'clean_content',
'URL_DETAIL', 'final_title', 'final_content',
'positive_score_mean', 'negative_score_mean', 'positive_score_sum',
'negative_score_sum', 'news_sentiment','news_label','good','bad','source']
df=pd.DataFrame(np.array(rows), columns=col)
title = []
content = []
source = []
final_title = []
final_content = []
error = []
for i in range(len(df)):
try:
title.append(df.iloc[i, 3].encode('iso-8859-1').decode('euc-kr'))
except:
title.append(0)
error.append(i)
for i in range(len(df)):
try:
content.append(df.iloc[i, 4].encode('iso-8859-1').decode('euc-kr'))
except:
content.append(0)
error.append(i)
for i in range(len(df)):
try:
source.append(df.iloc[i, 1].encode('iso-8859-1').decode('euc-kr'))
except:
source.append(0)
error.append(i)
for i in range(len(df)):
try:
final_title.append(df.iloc[i, 6].encode('iso-8859-1').decode('euc-kr'))
except:
final_title.append(0)
error.append(i)
for i in range(len(df)):
try:
final_content.append(df.iloc[i, 7].encode('iso-8859-1').decode('euc-kr'))
except:
final_content.append(0)
error.append(i)
len(title), len(content), len(source), len(df)
error = list(set(error))
df['TITLE'] = title
df['clean_content'] = content
df['CATEGORY'] = source
df['final_title'] = final_title
df['final_content'] = final_content
df = df.drop(error).reset_index(drop=True)
df = df.sort_values('NEWS_DATE',ascending=False).reset_index(drop=True)
df = df[df['clean_content'] != 'blank']
from sklearn.preprocessing import MinMaxScaler
# pos_mm = MinMaxScaler()
# neg_mm = MinMaxScaler()
label_mm = MinMaxScaler()
# df['sc_positive_score_mean'] = pos_mm.fit_transform(df[['positive_score_mean']]) * 100
# df['sc_negative_score_mean'] = neg_mm.fit_transform(abs(df[['negative_score_mean']])) * 100
label_mm.fit(df[['news_label']])
df['sc_news_label_score'] = label_mm.transform(df[['news_label']]) * 100
return df, label_mm
def corp_daily_senti(self, df):
# final_corp = pd.DataFrame(columns = ['date', 'name', 'positive','negative', 'total_score'])
final_corp = pd.DataFrame(columns = ['date', 'name', 'sentiment'])
for i in tqdm(self.kospi200['종목명']):
corp_news = df[df['final_title'].str.contains(i)].reset_index(drop=True)
if len(corp_news) > 2:
ls = []
# ls.append([self.yesterday, i, corp_news['sc_positive_score_mean'].mean() , corp_news['sc_negative_score_mean'].mean(), corp_news['sc_news_label_score'].mean()])
ls.append([self.yesterday, i, corp_news['sc_news_label_score'].mean()])
# corp_df = pd.DataFrame(ls, columns = ['date', 'name', 'positive','negative','total_score'])
corp_df = pd.DataFrame(ls, columns = ['date', 'name', 'sentiment'])
final_corp = final_corp.append(corp_df)
final_corp = final_corp.reset_index(drop=True)
final_corp.dropna(inplace=True)
return final_corp
def corp_daily_news(self, df, corp_list):
final_news = pd.DataFrame(columns = ['date','name','IDX','TITLE','final_title','clean_content','URL_DETAIL','news_sentiment','source'])
for i in tqdm(corp_list):
corp_neg_news = df[(df['final_title'].str.contains(i)) & (df['news_sentiment'] == -1)].reset_index(drop=True).sort_values(['NEWS_DATE','sc_news_label_score'], ascending=True)
corp_pos_news = df[(df['final_title'].str.contains(i)) & (df['news_sentiment'] == 1)].reset_index(drop=True).sort_values(['NEWS_DATE','sc_news_label_score'], ascending=False)
edaily_pos = corp_pos_news[corp_pos_news['URL_DETAIL'].str.contains('www.edaily.co.kr')]
edaily_neg = corp_neg_news[corp_neg_news['URL_DETAIL'].str.contains('www.edaily.co.kr')]
corp_pos_news = corp_pos_news[~(corp_pos_news['URL_DETAIL'].str.contains('www.edaily.co.kr'))]
corp_neg_news = corp_neg_news[~(corp_neg_news['URL_DETAIL'].str.contains('www.edaily.co.kr'))]
corp_pos_news['URL_DETAIL'] = None
corp_neg_news['URL_DETAIL'] = None
final_pos = pd.concat([edaily_pos, corp_pos_news[:3]])
final_neg = pd.concat([edaily_neg, corp_neg_news[:3]])
corp_news = pd.concat([final_pos, final_neg]).reset_index(drop=True)
corp_news['date'] = self.yesterday
corp_news['name'] = i
corp_news = corp_news[['date','name','IDX','TITLE','final_title','clean_content','URL_DETAIL','news_sentiment','source']]
final_news = final_news.append(corp_news)
final_news.drop_duplicates('final_title', inplace=True)
final_news = final_news.reset_index(drop=True)
return final_news
# In[306]:
s = kospi200_API()
# In[307]:
df, label_mm = s.news_load()
# In[267]:
corp_daily_senti = s.corp_daily_senti(df)
corp_daily_senti['label'] = np.where(corp_daily_senti.sentiment >= (label_mm.transform(np.array(0.15).reshape(1,-1)) *100)[0][0], 1, np.where(corp_daily_senti.sentiment <= (label_mm.transform(np.array(-0.05).reshape(1,-1)) *100)[0][0],-1,0))
corp_daily_senti
# In[308]:
corp_daily_news = s.corp_daily_news(df,corp_daily_senti['name'])
# ls = []
# for idx in range(len(corp_daily_news)):
# t = []
# for word in corp_daily_news['final_title'][idx].split():
# try:
# matching = [s for s in corp_daily_news['TITLE'][idx].split() if word in s][0]
# matching = re.sub('[=+,#-/”“▶▲●◆■◀\?.:^$’@*\"※~&%ㆍ!』\\‘|\(\)\[\]\<\>`\'…》☞△&·‥""]', ' ', matching).split()
# matching = [s for s in matching if word in s][0]
# if matching not in t:
# t.append(matching)
# except:
# pass
# ls.append(t)
#
# # corp_daily_news['new_title'] = ''
# for i in range(len(corp_daily_news)):
# corp_daily_news['final_title'][i] = ' '.join(ls[i])
for i in range(len(corp_daily_news)):
s = corp_daily_news['source'][i]
t = corp_daily_news['TITLE'][i]
corp_daily_news['final_title'][i] = f'[{s}] {t}'
corp_daily_news = corp_daily_news.drop(columns='source')
corp_daily_news = corp_daily_news.reset_index(drop=True)
# In[250]:
conn = pymssql.connect(host=r"10.101.34.221", user='sa', password='vmfkdla2006', database='BWPRIME', charset='UTF-8')
cursor = conn.cursor()
for i in corp_daily_senti.values:
try:
insert_query ="INSERT INTO CORP_SENTIMENT (DATE, NAME, SENTIMENT, LABEL) VALUES (%s, %s, %s, %s)"
sql_data = tuple(i)
cursor.execute(insert_query, sql_data)
conn.commit()
except:
pass
conn.close()
# In[309]:
conn = pymssql.connect(host=r"10.101.34.221", user='sa', password='vmfkdla2006', database='BWPRIME', charset='UTF-8')
cursor = conn.cursor()
for i in corp_daily_news.values:
try:
insert_query = "INSERT INTO CORP_SENTIMENT_NEWS (DATE, NAME, IDX, TITLE, FINAL_TITLE, FINAL_CONTENT, URL_DETAIL, LABEL) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
sql_data = tuple(i)
cursor.execute(insert_query, sql_data)
conn.commit()
except:
pass
conn.close()
# In[ ]:
# In[272]:
#
# conn = pymssql.connect(host=r"10.101.34.221", user='sa', password='vmfkdla2006', database='BWPRIME', charset='UTF-8')
# cursor = conn.cursor()
#
# sql = 'CREATE TABLE CORP_SENTIMENT_NEWS(DATE DATE, NAME varchar(32), IDX varchar(32), TITLE TEXT, FINAL_TITLE TEXT, FINAL_CONTENT TEXT, URL_DETAIL TEXT, LABEL int, CONSTRAINT PK_CORP_SENTIMENT_NEWS PRIMARY KEY (DATE,NAME,IDX))'
#
# cursor.execute(sql)
#
# conn.commit()
# conn.close()
#
#
#
#
#
# conn = pymssql.connect(host=r"10.101.34.221", user='sa', password='vmfkdla2006', database='BWPRIME', charset='UTF-8')
# cursor = conn.cursor()
#
# sql = 'CREATE TABLE CORP_SENTIMENT(DATE DATE, NAME varchar(32), SENTIMENT float, LABEL int)'
#
# cursor.execute(sql)
#
# conn.commit()
# conn.close()