From cab782bce590e9aa93ba284ecadc53972ddc8fe0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=A5=E5=BE=AE?= Date: Sun, 21 Jun 2026 01:34:22 +0800 Subject: [PATCH] =?UTF-8?q?fallback:=20=E5=B0=8F=E6=9E=9CAPI=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E7=94=A8=E6=97=B6=E9=99=8D=E7=BA=A7=E5=88=B0=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E8=AF=8D=E5=88=86=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xiaoguo_news_processor.py | 46 +++++++++++++++++++++++++++++---------- 1 file changed, 35 insertions(+), 11 deletions(-) diff --git a/xiaoguo_news_processor.py b/xiaoguo_news_processor.py index 8d361f3..a29bff2 100644 --- a/xiaoguo_news_processor.py +++ b/xiaoguo_news_processor.py @@ -138,6 +138,29 @@ def translate_sentiment(s): return m.get(s.lower() if isinstance(s, str) else "", s) +def fallback_classify(batch): + """关键词降级分类(小果API不可用时)""" + positive_kw = ['突破', '增长', '利好', '加单', '订单', '放量', '新高', '获批', '量产', + '超预期', '投产', '融资', '增持', '回购', '降息', '减税', '补贴', + '国产替代', '自主可控', '准入'] + negative_kw = ['管制', '限制', '制裁', '利空', '减持', '抛售', '下跌', '跌停', + '风险', '违约', '调查', '暂停', '取消', '下滑', '亏损', '裁员', + '诉讼', '退市', '做空', '关税', '禁令'] + + for a in batch: + text = a['title'] + (a.get('content') or '') + pos = sum(1 for kw in positive_kw if kw in text) + neg = sum(1 for kw in negative_kw if kw in text) + if pos > neg: + a['sentiment'] = '利好' + elif neg > pos: + a['sentiment'] = '利空' + else: + a['sentiment'] = '中性' + a['summary'] = a['title'][:80] + return batch + + def main(): conn = get_conn() signals = conn.execute( @@ -199,17 +222,18 @@ def main(): results = call_xiaoguo(batch) if not results: - print(" 小果分析失败", flush=True) - conn.close() - return - - # 合并结果(用索引位置匹配) - for i, r in enumerate(results): - if i < len(batch): - batch[i]["sentiment"] = translate_sentiment(r.get("sentiment", r.get("情感", ""))) - batch[i]["summary"] = r.get("summary", r.get("摘要", "")) - else: - break + print(" 小果API不可用,降级到关键词分类", flush=True) + fallback_classify(batch) + results = None # batch already has sentiment/summary set + + if results and isinstance(results, list): + # 小果LLM返回结果,按索引匹配 + for i, r in enumerate(results): + if i < len(batch): + batch[i]["sentiment"] = translate_sentiment(r.get("sentiment", r.get("情感", ""))) + batch[i]["summary"] = r.get("summary", r.get("摘要", "")) + else: + break # 汇总情感 sentiments = [a.get("sentiment", "中性") for a in batch if a.get("sentiment")]