Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,49 @@ from plotly.subplots import make_subplots
|
|
| 12 |
import re
|
| 13 |
from bs4 import BeautifulSoup
|
| 14 |
import requests
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# 台股代號對應表 (移除台指期,因為它現在是獨立區塊)
|
| 17 |
TAIWAN_STOCKS = {
|
|
|
|
| 12 |
import re
|
| 13 |
from bs4 import BeautifulSoup
|
| 14 |
import requests
|
| 15 |
+
from Bert_predict import BertPredictor
|
| 16 |
+
|
| 17 |
+
bert_predictor = BertPredictor(max_news_per_keyword=2)
|
| 18 |
+
# --- 2. 測試 get_news_index() 方法 ---
|
| 19 |
+
print("\n" + "-"*50)
|
| 20 |
+
print(f"[步驟 2] 測試 get_news_index() 方法...")
|
| 21 |
+
print(f"將會讀取檔案 '{os.path.basename(bert_predictor.news_csv_path)}' 並計算分數平均值。")
|
| 22 |
+
|
| 23 |
+
avg_score = bert_predictor.get_news_index()
|
| 24 |
+
|
| 25 |
+
if avg_score is not None:
|
| 26 |
+
print(f"【測試結果】成功!")
|
| 27 |
+
print(f" └── 所有新聞的平均分數為: {avg_score:.4f}")
|
| 28 |
+
else:
|
| 29 |
+
print("【測試結果】失敗或無資料。")
|
| 30 |
+
print(f" └── 無法從 '{os.path.basename(predictor.news_csv_path)}' 計算出平均分數。")
|
| 31 |
+
|
| 32 |
+
# --- 3. 測試 get_news() 方法 ---
|
| 33 |
+
print("\n" + "-"*50)
|
| 34 |
+
print(f"[步驟 3] 測試 get_news() 方法...")
|
| 35 |
+
print(f"將會讀取檔案 '{os.path.basename(bert_predictor.news_csv_path)}' 並找出分數絕對值最高的三則新聞。")
|
| 36 |
+
|
| 37 |
+
top_news_content_list = bert_predictor.get_news()
|
| 38 |
+
|
| 39 |
+
if top_news_content_list is not None:
|
| 40 |
+
if top_news_content_list: # List 不為空
|
| 41 |
+
print("【測試結果】成功!")
|
| 42 |
+
print(" └── 分數絕對值最高的三則新聞內容如下:")
|
| 43 |
+
for i, content in enumerate(top_news_content_list):
|
| 44 |
+
# 為了美觀地顯示,限制內容長度
|
| 45 |
+
content_display = (content[:80] + '...') if len(content) > 80 else content
|
| 46 |
+
print(f" {i+1}. {content_display}")
|
| 47 |
+
else: # List 為空 []
|
| 48 |
+
print("【測試結果】成功,但無有效新聞可顯示。")
|
| 49 |
+
print(f" └── 檔案 '{os.path.basename(predictor.news_csv_path)}' 可能為空或不含有效分數。")
|
| 50 |
+
else: # 回傳值為 None,代表發生錯誤
|
| 51 |
+
print("【測試結果】失敗。")
|
| 52 |
+
print(f" └── 讀取檔案 '{os.path.basename(predictor.news_csv_path)}' 時發生錯誤。")
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
print("\n" + "="*50)
|
| 56 |
+
print("所有測試執行完畢。")
|
| 57 |
+
print("="*50)
|
| 58 |
|
| 59 |
# 台股代號對應表 (移除台指期,因為它現在是獨立區塊)
|
| 60 |
TAIWAN_STOCKS = {
|