-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
113 lines (106 loc) ยท 4.63 KB
/
Copy pathapp.py
File metadata and controls
113 lines (106 loc) ยท 4.63 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
from flask import Flask, request, jsonify
from konlpy.tag import Mecab
from collections import Counter
from crawling.crawling import each_crawling
from db.connect import get_db
from db.check_insert_stock import check_insert_stock
from db.insert_keywords import insert_keywords
from db.get_title_content import get_title_content
from db.insert_isGood import insert_isGood
from flask_cors import CORS
from bs4 import BeautifulSoup
from transformers import pipeline
import json
import os
import time
os.environ["TOKENIZERS_PARALLELISM"] = "false"
app = Flask(__name__)
CORS(app, resources={r'*':{'origins':'http://43.202.201.221:3003'}})
db = get_db()
kor_clf_sentiment = pipeline("sentiment-analysis", "snunlp/KR-FinBert-SC")
stopwords = ['์ต๊ทผ', '์์ฆ', '์ง๋๋ฌ', '์ด๋ฌ', '์ง๋ํด', '์ฌํด', '์ด๋ ', '์ง๋ํด', '์ ๋ ', '์ด์ ', '์ดํ',
'๊ทน๋ด','๋ฐ๋ฉด','๊ธฐ์ค', '๊ด๋ จ','์ํฉ','๋๋น','์ด์','์ดํ','์ฅ์ค','์ดํ','๊ฐ์ธ','๊ธฐ๊ด','ํฌ์ธํธ',
'์ฃผ๊ฐ','๊ฑฐ๋์ผ','์ง์','๊ธฐ๋ก','ํ์ฉ','์์']
@app.route('/api/news', methods=['POST'])
def crawling_keyword():
# request body : {"name" : "์ผ์ฑ์ ์", "code" : "005930"}
# name = request.json['name'] # [code]
code = request.json['code']
tags = None
time0 = time.time()
# # if check_insert_stock("์์", code):
# time2 = time.time()
# print("appใ
ใ
ใ
: ", time2-time0)
news_text = each_crawling(code) # code์ ๋ํ ๋ด์ค ๊ฐ์ ธ์ด
if news_text == []:
response_data = {"message": "๋ด์ค๋ฆฌ์คํธ๊ฐ ์์ด์ใ
ใ
."}
return jsonify(response_data), 404
time1 = time.time()
print("app time1 - time0 : ", time1 - time0)
# return news_text
# KoNLpy + Mecab : ํํ์ ๋ถ์
# ํํ์ ๋ถ์๊ธฐ๋ก ๋ช
์ฌ๋ง ์ถ์ถ,1๊ธ์๋ ์๋ฏธ์๋ค๊ณ ๋ณด๊ณ ์ญ์
path = "/opt/homebrew/lib/mecab/dic/mecab-ko-dic"
try:
mecab = Mecab()
except:
mecab = Mecab(path)
nouns = mecab.nouns(' '.join(news_text))
nouns = [n for n in nouns if len(n) > 1]
# ์ข
๋ชฉ ์ด๋ฆ๊ณผ ์ผ๋ถ๋ฅผ ๋ถ์ฉ์ด๋ก ์ถ๊ฐ
stopwords_exp = stopwords + get_stopwords_for_stock(code)
print(stopwords_exp)
# ๋จ์ด ๊ฐ์ ์ธ๊ธฐ, ๊ฐ์ฅ ๋ง์ด ๋ฑ์ฅํ N๊ฐ ๊ตฌํ๊ธฐ(Counter.most_common())
count = Counter(nouns)
tags = count.most_common(40)
# ๋ถ์ฉ์ด ์ ๊ฑฐ
tags = [(word, cnt) for word, cnt in tags if word not in stopwords_exp]
result = json.dumps([{"word":tag[0], "cnt":tag[1]} for tag in tags], ensure_ascii=False)
time2 = time.time()
print("app time2 - time1 : ",time2-time1)
if insert_keywords(result, code):
response_data = {"message": "์์ฒญ์ด ์ฑ๊ณต์ ์ผ๋ก ์ฒ๋ฆฌ๋์์ต๋๋ค."}
return jsonify(response_data), 200
response_data = {"message": "์์ฒญ์ด ์คํจํ์ต๋๋ค."}
return jsonify(response_data), 404
def get_stopwords_for_stock(code):
# ์ฌ๊ธฐ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋๋ ๋ค๋ฅธ ์์ค๋ก๋ถํฐ ์ข
๋ชฉ ์ด๋ฆ๊ณผ ์ผ๋ถ๋ฅผ ๊ฐ์ ธ์์ ๋ถ์ฉ์ด๋ก ์ฒ๋ฆฌํฉ๋๋ค.
stopwords_for_stock = []
cursor = db.cursor()
query = "SELECT name FROM Stock WHERE stockCode = %s"
cursor.execute(query, (code, ))
stock_name = cursor.fetchone()
if stock_name:
stock_name = stock_name['name'] # dict์์ ์ข
๋ชฉ ์ด๋ฆ ๋ฌธ์์ด๋ก ๋ณํ
# ์ข
๋ชฉ ์ด๋ฆ์ ์ผ๋ถ๋ ๋ถ์ฉ์ด๋ก ์ฒ๋ฆฌ
for i in range(len(stock_name)):
part_of_name = stock_name[:i+1] # ์ข
๋ชฉ ์ด๋ฆ์ ์ฒ์๋ถํฐ i+1๊น์ง์ ๋ถ๋ถ
if len(part_of_name) > 1: # ํ ๊ธ์ ์ด์์ธ ๊ฒฝ์ฐ์๋ง ์ถ๊ฐ
stopwords_for_stock.append(part_of_name)
print(stopwords_for_stock)
return stopwords_for_stock
@app.route('/api/krFinBert', methods=['POST'])
def content_sentiment():
newsId = request.json['newsId']
text = get_title_content(newsId)
if text['isGood'] is not None:
return str(text['isGood'])
text["content"] = [text for text in BeautifulSoup(text["content"], 'html.parser').stripped_strings]
score = 0 # positive
res_title = 2 * kor_clf_sentiment(text['title'])
score += calculate_score(res_title)
for text in text["content"]:
res_content = kor_clf_sentiment(text)
score += calculate_score(res_content)
isGood = 1 if score >= 0 else 0
insert_isGood(isGood, newsId)
return str(isGood)
def calculate_score(result):
if(result[0]['label'] == 'neutral'): return 0
elif(result[0]['label'] == 'positive') : return 1
else: return -1
if __name__ == '__main__':
# from waitress import serve
# from app import app
# serve(app, host="0.0.0.0", port=5000)
app.run(host='0.0.0.0')