iT邦幫忙

2025 iThome 鐵人賽

DAY 27
0
自我挑戰組

30天用Python打造你的數位金融實力:從零開始的FinTech入門筆記系列 第 27

生成式 AI 思維在金融:用 Python 做財經摘要 + 情緒偵測

  • 分享至 

  • xImage
  •  

為什麼有用?

  • 金融資訊量大,投資/風控人員需快速抓重點。
  • 生成式 AI(summary / Q&A / drafting)很棒,但入門門檻高;先用「提取式摘要+簡單情緒分析」就能達到類似輔助決策的效果。
  • 完整流程:把新聞 / 財報文字 → 自動產出重點句(摘要)→ 判斷情緒(正/中/負)→ 產出簡短結論,可自動發報告或觸發警示。

做法概念

  1. 提取式摘要:把文章切成句子,根據詞頻給每句分數,選出前 N 句做為摘要(不會自己「生成」新句,不會亂說話)。
  2. 情緒偵測(Rule-based):用一個小的正/負字典計分,算出情緒傾向(正/中/負)。

下面是完全離線、只要 Python 標準庫+matplotlib/pandas(選用)就能跑的範例程式碼。

Python 範例

import re
from collections import Counter

# 範例文章(可替換成新聞全文)
text = """
台灣晶片大廠於昨日公布財報,營收與淨利均優於市場預期。公司表示,受惠於 AI 加速需求,製程良率改善帶動毛利率提升。管理層同時警告,全球供應鏈仍有不確定性,但短期訂單能見度良好。
市場反應正向,股價盤中上漲逾 6%。
"""

# --- 1) 簡單句子切割 ---
def split_sentences(text):
    sentences = re.split(r'(?<=[。!?\?])\s*', text.strip())
    return [s for s in sentences if s]

# --- 2) 字詞頻率計算(非常基礎) ---
STOPWORDS = set([
    '的','了','與','和','在','是','有','也','與','就','但','而','其','能','短期'
])  # 可擴充

def tokenize_words(s):
    words = re.findall(r'\w+|[一-龥]+', s)  # 英文或中文詞塊(簡單版)
    return [w.lower() for w in words if w.lower() not in STOPWORDS and len(w) > 1]

def sentence_score(sent, freq):
    words = tokenize_words(sent)
    if not words:
        return 0
    return sum(freq.get(w,0) for w in words) / len(words)

# --- 3) 建摘要(取 top_n 句,保持原順序) ---
def extractive_summary(text, top_n=2):
    sents = split_sentences(text)
    words = []
    for s in sents:
        words += tokenize_words(s)
    freq = Counter(words)
    scores = [(i, s, sentence_score(s, freq)) for i, s in enumerate(sents)]
    top = sorted(scores, key=lambda x: x[2], reverse=True)[:top_n]
    top_idx = sorted([t[0] for t in top])
    summary = ' '.join(sents[i] for i in top_idx)
    return summary

# --- 4) 簡單情緒判斷(金融詞典示意) ---
POS = {"優於","提升","上漲","利多","成長","改善","利好","增加"}
NEG = {"不確定","下滑","下跌","虧損","警告","減少","利空","擔憂"}

def sentiment_score(text):
    t = text.lower()
    pos_count = sum(t.count(w) for w in POS)
    neg_count = sum(t.count(w) for w in NEG)
    score = pos_count - neg_count
    if score > 0:
        return "正向", score
    elif score < 0:
        return "負向", score
    else:
        return "中性", score

# --- 執行示例 ---
summary = extractive_summary(text, top_n=2)
sentiment, s = sentiment_score(text)
print("=== 摘要 ===")
print(summary)
print("\n=== 情緒判定 ===")
print(sentiment, "(分數:", s, ")")

執行結果說明

  • 摘要:幫你把長文抽出 1–2 句重點(例如:財報優於預期、AI 需求推升毛利)。
  • 情緒:把整篇標成「正向 / 中性 / 負向」,並給出簡單分數,方便你自動化篩選要人工深入閱讀的新聞。

延伸

  • 若你想用真實的 LLM(生成式摘要、問答),可在有 API 時把上面 extractive 摘要作為「提示前處理」,再把重點句或全文交給 LLM 做「精簡撰寫」或「QA」,但要注意 LLM 有時會「憑空產生(hallucinate)」,尤其在財經事實上要特別小心驗證。
  • 可把此流程接到「每日上午自動抓新聞 → 摘要+情緒 → 寄報告」的自動化管線(結合 Day25 的自動報表)。

上一篇
永續金融入門:用 Python 模擬 ESG 投資績效
下一篇
用 Python 做資料遮罩、卡號 Luhn 驗證與匿名化 ID
系列文
30天用Python打造你的數位金融實力:從零開始的FinTech入門筆記28
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言