Twatter - Twitter Sentiment Analysis System
Kafka-driven Twitter and financial news sentiment analyzer with advanced stock detection, validation, and real-time processing. Consumes tweets, filters advertisements, validates tickers, and publishes sentiment results to Kafka with comprehensive training data collection.
Status: completed · 2025-05-01
Overview
Twatter is a standalone Python application for monitoring Twitter posts and financial news for stock-related sentiment analysis. The system connects to Kafka topics to process Twitter data, analyzes tweets to determine stock/company mentions and sentiment, and publishes results with comprehensive validation and training data collection.
Technologies
Python, Kafka, Apache Arrow, Financial Modeling Prep (FMP) API, LLM Integration, Sentiment Analysis, Stock Detection, Advertisement Filtering, Account Reputation System, Ticker Validation, Training Data Collection, Functional Programming, Error Handling, Real-time Processing, Microservices Architecture, Deduplication, News Processing, Pattern Matching, Context-aware Analysis
- Tweet Processing Speed
- 1000+ tweets/min
- Stock Detection Accuracy
- 94.2%
- Sentiment Analysis Speed
- < 50ms
- Advertisement Filter Accuracy
- 98.5%
Twatter
A standalone Python application for monitoring Twitter posts and financial news for stock-related sentiment analysis.
Overview
Twatter connects to a Kafka topic to process Twitter data, analyzes tweets to determine if they mention stocks/companies and their sentiment, and publishes results to another Kafka topic. It also periodically pulls financial news and grades from Financial Modeling Prep (FMP) API.
Features
- Consumes Twitter posts from Kafka topic `twitter_posts`
- Analyzes tweet content for stock/company mentions and sentiment
- Publishes results to Kafka topic `twitter_alerts`
- Uses Financial Modeling Prep (FMP) API for company lookup (focusing on US exchanges)
- Pulls latest stock grades/news every 5 minutes
- Analyzes sentiment of financial news
- Calculates upgrade/downgrade scores based on analyst actions
- Publishes results to Kafka topic `finnews_alerts`
- Includes validation tools for training data collection
- Uses Apache Arrow for historical training data storage
- Implements functional programming principles for robust error handling
- Features enhanced stock detection with advertisement filtering
- Uses LLM for context-aware company identification
- Tracks account reputation to prioritize reliable sources
- Implements tiered account system for trusted posters
Architecture
The system is designed with the following components:
- Kafka Consumer: Processes incoming Twitter posts
- Enhanced Stock Detector: Identifies stock/company mentions with improved accuracy
- Fast Stock Detector: Provides quick filtering and advertisement detection
- LLM Integration: Uses language models for context-aware company identification
- Account Reputation Tracker: Maintains reputation scores for tweet sources
- Sentiment Analyzer: Determines sentiment score of tweets and news
- FMP API Client: Interfaces with Financial Modeling Prep API for ticker validation
- News Processor: Handles periodic news fetching and deduplication
- Kafka Producer: Publishes processed data to output topic
- Training Data Collector: Stores and manages data for model improvement
- Validation Interface: Tool for reviewing and correcting processed data
Data Flow
- Twitter posts are consumed from Kafka topic `twitter_posts`
- Account reputation is checked to determine processing priority
- Fast advertisement detection is performed to filter promotional content
- Posts are analyzed for stock/company mentions using cashtag extraction
- For trusted accounts or non-ad content without cashtags, LLM analysis is performed
- Identified tickers are validated against FMP API and common stocks list
- Sentiment analysis is performed on validated stock mentions
- Account reputation is updated based on post quality
- Results are published to Kafka topic `twitter_alerts`
- Every 5 minutes, latest financial news is pulled from FMP API
- News items are deduplicated and analyzed for sentiment
- News sentiment and upgrade/downgrade scores are calculated
- Results are published to Kafka topic `finnews_alerts`
Development Approach
The system follows functional programming principles to ensure robust error handling and maintainability. Components are designed to be modular and testable, with clear interfaces between them.
Enhanced Stock Detection
The enhanced stock detection system implements a multi-stage approach:
- Fast Advertisement Detection: Quickly identifies promotional content using pattern matching
- Account Reputation Tracking: Maintains a reputation system for tweet sources
- Trusted accounts: Automatically processed without advertisement checks
- Probationary accounts: Being monitored for quality content
- Standard accounts: Subject to full filtering process
- Cashtag Extraction: Identifies direct stock mentions with $ symbol
- LLM-based Company Detection: Uses language models to identify company mentions in natural language
- Ticker Validation: Verifies tickers against both a common stocks list and FMP API
Running Tests
To run the unit tests: ```bash source .venv/bin/activate && python -m tests.unit.test_fast_detector ```
To run the Kafka integration test: ```bash source .venv/bin/activate && python -m tests.integration.test_kafka_realtime ```