使用 LLM 提取情绪因子:分析 NBA 新闻中的团队情绪

在这里插入图片描述

引言

体育新闻中充满激情、揣测与情绪。在 NBA 世界里,这些语言不仅影响球迷情绪,也左右着球队氛围、投注市场,甚至 Fantasy 游戏策略。

现在,我们可以利用大型语言模型(LLMs)从 NBA 新闻标题中提取结构化的情绪因子,如球队士气伤病担忧交易传闻强度。这为我们提供了可追踪、可比较、可操作的语言情绪指标。


1. 为什么要分析 NBA 新闻的情绪?

与金融市场类似,情绪也是体育分析的重要因子:

  • 球队是否具备逆风翻盘的信心?
  • 谣言是否在扰乱球员或更衣室?
  • 教练或高层是否释放出积极或保守的信号?

情绪分析的目的是:把模糊的语气变成明确的信号


2. 常见 NBA 情绪因子及评分方式

情绪因子描述分值类型示例
overall_sentiment新闻标题的总体情绪"Positive""Neutral""Negative""Negative"
team_morale球队氛围、 信心-2(低)~+2(高)+1
injury_concern对伤病的担忧程度0(无)~3(严重)2
trade_speculation交易传闻的强度及倾向-2(即将离队) ~ +2(利好消息)-1
key_entities涉及的球队或球员及其情绪评分JSON 列表见下方输出示例

3. 示例:从标题到结构化情绪因子

标题:
“湖人缺少戴维斯苦战,罗素交易传闻升温”

LLM 输出:

{
  "overall_sentiment": "Negative",
  "team_morale": -1,
  "injury_concern": 2,
  "trade_speculation": -2,
  "key_entities": [
    {"entity": "湖人", "sentiment_score": -1},
    {"entity": "安东尼·戴维斯", "sentiment_score": -2},
    {"entity": "拉塞尔", "sentiment_score": -1}
  ]
}
  1. 应用场景
    • Fantasy 球队管理: 用情绪因子辅助球员出场选择
    • 投注模型优化: 提前识别舆论拐点
    • 球队趋势分析: 构建“舆情仪表盘”跟踪士气、焦点、动荡等

  1. Python 实战:提取 NBA 新闻情绪因子

使用 NBA 官网新闻源,并用本地 Ollama 模型调用 LLM 分析。

import requests
from bs4 import BeautifulSoup
import json
import time

# 第一步:从 NBA 官网获取新闻标题
def get_nba_news(limit=5):
    url = "https://www.nba.com/news"
    headers = {"User-Agent": "Mozilla/5.0"}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, "html.parser")

    headlines = []
    for article in soup.select("a.Anchor_anchor__cSc3P")[:limit]:
        title = article.get_text(strip=True)
        link = "https://www.nba.com" + article["href"]
        if title and "/news/" in link:
            headlines.append((title, link))

    return headlines

# 第二步:调用 Ollama Chat API 获取情绪因子
def extract_sentiment_ollama_chat(text):
    messages = [
        {
            "role": "system",
            "content": "你是一个负责从NBA新闻标题中提取情绪因子的助手,请以JSON格式输出结果。"
        },
        {
            "role": "user",
            "content": f"""
请分析以下NBA新闻标题,并返回以下JSON结构:

Text: "{text}"

返回格式:
{{
  "overall_sentiment": "Positive" | "Neutral" | "Negative",
  "team_morale": -2 到 +2,
  "injury_concern": 0 到 3,
  "trade_speculation": -2 到 +2,
  "key_entities": [
    {{"entity": "球队或球员名称", "sentiment_score": -2 到 +2}}
  ]
}}

只返回合法 JSON 对象。
"""
        }
    ]

    response = requests.post(
        "http://localhost:11434/api/chat",
        json={
            "model": "llama3",
            "messages": messages,
            "stream": False
        }
    )

    try:
        raw = response.json()["message"]["content"]
        json_text = raw.strip().split("```")[-1] if "```" in raw else raw
        result = json.loads(json_text)
        return result
    except Exception as e:
        print("解析错误:", e)
        print("原始返回:", response.text)
        return None

# 第三步:运行主程序
def main():
    print("\n从 NBA 官方网站获取新闻标题,并使用 Ollama Chat 模型提取情绪因子:\n" + "=" * 60)
    headlines = get_nba_news(limit=5)
    for title, link in headlines:
        print(f"\n标题:{title}")
        print(f"链接:{link}")
        result = extract_sentiment_ollama_chat(title)
        print("提取的情绪因子:")
        print(json.dumps(result, indent=2, ensure_ascii=False))
        time.sleep(2)

if __name__ == "__main__":
    main()

结语:用语言建模读懂 NBA 舆情

情绪因子为体育分析带来了“语言信号维度”。
你可以在比分、赔率变动前,先读懂球队和媒体的态度、语气与倾向。

用 LLM 解构语言,下一场胜负的秘密,也许就藏在字里行间。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值