fallback: 小果API不可用时降级到关键词分类
This commit is contained in:
+35
-11
@@ -138,6 +138,29 @@ def translate_sentiment(s):
|
|||||||
return m.get(s.lower() if isinstance(s, str) else "", 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():
|
def main():
|
||||||
conn = get_conn()
|
conn = get_conn()
|
||||||
signals = conn.execute(
|
signals = conn.execute(
|
||||||
@@ -199,17 +222,18 @@ def main():
|
|||||||
|
|
||||||
results = call_xiaoguo(batch)
|
results = call_xiaoguo(batch)
|
||||||
if not results:
|
if not results:
|
||||||
print(" 小果分析失败", flush=True)
|
print(" 小果API不可用,降级到关键词分类", flush=True)
|
||||||
conn.close()
|
fallback_classify(batch)
|
||||||
return
|
results = None # batch already has sentiment/summary set
|
||||||
|
|
||||||
# 合并结果(用索引位置匹配)
|
if results and isinstance(results, list):
|
||||||
for i, r in enumerate(results):
|
# 小果LLM返回结果,按索引匹配
|
||||||
if i < len(batch):
|
for i, r in enumerate(results):
|
||||||
batch[i]["sentiment"] = translate_sentiment(r.get("sentiment", r.get("情感", "")))
|
if i < len(batch):
|
||||||
batch[i]["summary"] = r.get("summary", r.get("摘要", ""))
|
batch[i]["sentiment"] = translate_sentiment(r.get("sentiment", r.get("情感", "")))
|
||||||
else:
|
batch[i]["summary"] = r.get("summary", r.get("摘要", ""))
|
||||||
break
|
else:
|
||||||
|
break
|
||||||
|
|
||||||
# 汇总情感
|
# 汇总情感
|
||||||
sentiments = [a.get("sentiment", "中性") for a in batch if a.get("sentiment")]
|
sentiments = [a.get("sentiment", "中性") for a in batch if a.get("sentiment")]
|
||||||
|
|||||||
Reference in New Issue
Block a user