Skip to content

cabes/market-sentiment-analysis

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

市场情绪分析系统

项目简介

资本市场情绪收集和分析系统 - 用于用户资产投机参考的MVP版本。该系统能够:

  • 📰 自动抓取新闻源数据(Yahoo Finance, Seeking Alpha)
  • 🤖 使用NLP模型进行情绪分析
  • 📊 提供情绪统计和趋势分析
  • 🔍 支持按股票代码查询情绪数据

技术栈

  • 后端: Python 3.11 + FastAPI
  • 数据库: PostgreSQL 15
  • 缓存: Redis 7
  • NLP: Transformers (RoBERTa)
  • 前端: HTML + JavaScript (纯静态)
  • 容器: Docker + Docker Compose

快速开始

前置要求

  • Docker 20.10+
  • Docker Compose 2.0+

启动系统

# 1. 克隆项目
git clone <repository-url>
cd market-sentiment-analysis

# 2. 复制环境配置
cp .env.example .env

# 3. 启动所有服务
./dev.sh start

访问应用

使用说明

基本操作

# 抓取新闻
./dev.sh fetch-news

# 分析情绪
./dev.sh process-news

# 查看日志
./dev.sh logs

# 停止服务
./dev.sh stop

API使用示例

1. 分析文本情绪

curl -X POST http://localhost:8000/api/v1/analyze \
  -H "Content-Type: application/json" \
  -d '{"text": "Apple stock is doing great today!"}'

2. 查询新闻

# 获取最新新闻
curl http://localhost:8000/api/v1/news?limit=10

# 按股票代码筛选
curl http://localhost:8000/api/v1/news?symbol=AAPL

# 按情绪筛选
curl http://localhost:8000/api/v1/news?sentiment=positive

3. 查询情绪统计

# 获取某股票的30天情绪统计
curl http://localhost:8000/api/v1/stats/AAPL?days=30

# 计算特定日期的情绪统计
curl -X POST http://localhost:8000/api/v1/stats/AAPL/calculate

项目结构

market-sentiment-analysis/
├── app/                    # 应用代码
│   ├── __init__.py
│   ├── config.py          # 配置管理
│   ├── models.py          # 数据库模型
│   ├── database.py        # 数据库连接
│   ├── schemas.py         # API数据模型
│   ├── crud.py            # 数据库操作
│   ├── sentiment.py       # 情绪分析模块
│   ├── scraper.py         # 新闻爬虫
│   └── main.py            # FastAPI应用入口
├── alembic/               # 数据库迁移
│   └── versions/
├── frontend/              # 前端文件
│   └── index.html
├── tests/                 # 测试文件
├── docker-compose.yml     # Docker编排配置
├── Dockerfile            # Docker镜像配置
├── requirements.txt       # Python依赖
└── dev.sh                # 开发控制脚本

API 端点

端点 方法 说明
/api/v1/analyze POST 分析文本情绪
/api/v1/news GET 获取新闻列表
/api/v1/news/{id} GET 获取单条新闻
/api/v1/news POST 创建新闻条目
/api/v1/scraper/fetch POST 抓取新闻
/api/v1/scraper/process POST 分析情绪
/api/v1/stats/{symbol} GET 获取情绪统计
/api/v1/stats/{symbol}/calculate POST 计算情绪统计

数据模型

NewsArticle(新闻文章)

  • id: 文章ID
  • title: 标题
  • content: 内容
  • url: 链接
  • source: 来源
  • published_at: 发布时间
  • sentiment: 情绪标签
  • sentiment_score: 情绪评分

StockMention(股票提及)

  • id: 提及ID
  • article_id: 文章ID
  • symbol: 股票代码
  • mention_sentiment: 情绪

SentimentStats(情绪统计)

  • id: 统计ID
  • symbol: 股票代码
  • date: 日期
  • avg_sentiment_score: 平均情绪分
  • trend_24h: 24小时趋势
  • trend_7d: 7天趋势

开发指南

本地开发

# 1. 启动依赖服务
docker-compose up -d postgres redis

# 2. 安装Python依赖
pip install -r requirements.txt

# 3. 设置环境变量
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/market_sentiment"
export REDIS_URL="redis://localhost:6379/0"

# 4. 运行迁移
alembic upgrade head

# 5. 启动开发服务器
uvicorn app.main:app --reload --port 8000

运行测试

# 安装测试依赖
pip install -r requirements.txt[dev]

# 运行所有测试
pytest

# 运行单个测试
pytest tests/test_api.py

# 运行带覆盖率
pytest --cov=app --cov-report=html

数据库迁移

# 创建新迁移
alembic revision --autogenerate -m "description"

# 升级数据库
alembic upgrade head

# 降级数据库
alembic downgrade -1

# 查看迁移历史
alembic history

扩展功能

添加新闻源

  1. app/scraper.py 中创建新的scraper类
  2. 继承 NewsScraper 基类
  3. 实现 fetch_articles() 方法
  4. ScraperFactory._scrapers 中注册

添加情绪分析模型

  1. app/sentiment.py 中配置新模型
  2. 修改 SENTIMENT_MODEL 环境变量

添加API端点

  1. app/schemas.py 中定义数据模型
  2. app/main.py 中添加路由处理函数
  3. app/crud.py 中实现数据库操作

常见问题

模型加载慢

首次运行时,transformers会自动下载模型(约500MB)。下载完成后会被缓存,后续启动会更快。

情绪分析不准确

当前使用的是通用RoBERTa模型。对于金融领域,可以考虑使用专门的金融情绪分析模型:

  • nlptown/bert-base-multilingual-uncased-sentiment
  • ProsusAI/finbert

新闻抓取失败

新闻网站可能有反爬机制。可以考虑:

  • 添加请求延迟
  • 使用代理IP
  • 添加User-Agent轮换

许可证

MIT License

贡献

欢迎提交Issue和Pull Request!

联系方式

About

Market Sentiment Analysis System - 市场情绪分析系统

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors