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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""News Intelligence v1.0 — Real-Time News Sentiment + Event Detection
|
| 2 |
+
FinBERT-based sentiment scoring with event classification.
|
| 3 |
+
Falls back to regex-based analysis if FinBERT unavailable.
|
| 4 |
+
"""
|
| 5 |
+
import re, os, json, requests
|
| 6 |
+
from datetime import datetime, timedelta
|
| 7 |
+
from typing import List, Dict, Optional, Tuple
|
| 8 |
+
import numpy as np
|
| 9 |
+
|
| 10 |
+
# ── Event detection keywords ─────────────────────────────────
|
| 11 |
+
EVENT_PATTERNS = {
|
| 12 |
+
'earnings': ['earnings', 'quarterly', 'revenue', 'eps', 'profit', 'q[1-4]', 'fiscal'],
|
| 13 |
+
'fed': ['federal reserve', 'fed', 'fomc', 'interest rate', 'rate hike', 'rate cut', 'powell'],
|
| 14 |
+
'cpi': ['cpi', 'inflation', 'consumer price', 'core pce'],
|
| 15 |
+
'jobs': ['jobs report', 'unemployment', 'nfp', 'nonfarm payroll', 'labor'],
|
| 16 |
+
'lawsuit': ['lawsuit', 'sec', 'doj', 'investigation', 'antitrust', 'fine', 'settlement'],
|
| 17 |
+
'merger': ['merger', 'acquisition', 'acquire', 'buyout', 'merging', 'takeover'],
|
| 18 |
+
'dividend': ['dividend', 'buyback', 'share repurchase', 'dividend yield'],
|
| 19 |
+
'split': ['stock split', 'split', 'reverse split'],
|
| 20 |
+
'upgrade': ['upgrade', 'upgraded', 'overweight', 'buy rating', 'price target raised'],
|
| 21 |
+
'downgrade': ['downgrade', 'downgraded', 'underweight', 'sell rating', 'price target cut'],
|
| 22 |
+
'product': ['product launch', 'new product', 'iphone', 'ai model', 'release date'],
|
| 23 |
+
'supply_chain': ['supply chain', 'shortage', 'inventory', 'chip shortage', 'factory'],
|
| 24 |
+
'macro': ['gdp', 'recession', 'economic growth', 'fiscal policy', 'stimulus'],
|
| 25 |
+
'geopolitical': ['war', 'sanctions', 'tension', 'china', 'trade war', 'tariff'],
|
| 26 |
+
'analyst': ['analyst', 'wall street', 'target price', 'consensus'],
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
BULLISH_WORDS = [
|
| 30 |
+
'beat', 'strong', 'growth', 'surge', 'rally', 'bullish', 'outperform',
|
| 31 |
+
'exceed', 'record', 'milestone', 'breakthrough', 'partnership', 'launch',
|
| 32 |
+
'innovation', 'momentum', 'premium', 'dominant', 'leader', 'expansion'
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
BEARISH_WORDS = [
|
| 36 |
+
'miss', 'weak', 'decline', 'drop', 'crash', 'bearish', 'underperform',
|
| 37 |
+
'loss', 'concern', 'warning', 'risk', 'lawsuit', 'investigation',
|
| 38 |
+
'fraud', 'default', 'bankruptcy', 'layoff', 'cut', 'slash', 'downturn',
|
| 39 |
+
'recession', 'contagion', 'crisis', 'collapse'
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class NewsIntelligence:
|
| 44 |
+
"""Multi-source news sentiment with FinBERT + rule-based fallback."""
|
| 45 |
+
|
| 46 |
+
def __init__(self, finbert_available: bool = None, cache_dir: str = ".cache/news"):
|
| 47 |
+
self.cache_dir = cache_dir
|
| 48 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 49 |
+
self._finbert = None
|
| 50 |
+
self._tokenizer = None
|
| 51 |
+
self._sentiment_cache = {} # ticker -> {date: score}
|
| 52 |
+
|
| 53 |
+
if finbert_available is None:
|
| 54 |
+
try:
|
| 55 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 56 |
+
self._tokenizer = AutoTokenizer.from_pretrained("ProsusAI/finbert")
|
| 57 |
+
self._finbert = AutoModelForSequenceClassification.from_pretrained("ProsusAI/finbert")
|
| 58 |
+
self._finbert.eval()
|
| 59 |
+
finbert_available = True
|
| 60 |
+
except Exception:
|
| 61 |
+
finbert_available = False
|
| 62 |
+
self.use_finbert = finbert_available
|
| 63 |
+
|
| 64 |
+
def classify_event(self, headline: str, summary: str = "") -> Tuple[str, float]:
|
| 65 |
+
"""Classify article into event type and severity (0-1)."""
|
| 66 |
+
text = (headline + " " + summary).lower()
|
| 67 |
+
scores = {}
|
| 68 |
+
for event_type, patterns in EVENT_PATTERNS.items():
|
| 69 |
+
score = 0
|
| 70 |
+
for pat in patterns:
|
| 71 |
+
count = len(re.findall(pat, text))
|
| 72 |
+
score += count
|
| 73 |
+
if score > 0:
|
| 74 |
+
scores[event_type] = score
|
| 75 |
+
|
| 76 |
+
if not scores:
|
| 77 |
+
return 'general', 0.1
|
| 78 |
+
|
| 79 |
+
best = max(scores, key=scores.get)
|
| 80 |
+
return best, min(1.0, scores[best] * 0.5)
|
| 81 |
+
|
| 82 |
+
def rule_sentiment(self, headline: str, summary: str = "") -> Dict:
|
| 83 |
+
"""Rule-based sentiment as fallback when FinBERT unavailable."""
|
| 84 |
+
text = (headline + " " + summary).lower()
|
| 85 |
+
bull = sum(text.count(w) for w in BULLISH_WORDS)
|
| 86 |
+
bear = sum(text.count(w) for w in BEARISH_WORDS)
|
| 87 |
+
total = bull + bear + 1e-10
|
| 88 |
+
# Map to 0-100 scale
|
| 89 |
+
sentiment = 50 + (bull - bear) / total * 50
|
| 90 |
+
confidence = min(1.0, total * 0.1)
|
| 91 |
+
return {
|
| 92 |
+
'score': max(0, min(100, sentiment)),
|
| 93 |
+
'confidence': confidence,
|
| 94 |
+
'method': 'rule'
|
| 95 |
+
}
|
| 96 |
+
|
| 97 |
+
def finbert_sentiment(self, headline: str, summary: str = "") -> Dict:
|
| 98 |
+
"""FinBERT inference. Returns score 0-100."""
|
| 99 |
+
if not self.use_finbert:
|
| 100 |
+
return self.rule_sentiment(headline, summary)
|
| 101 |
+
|
| 102 |
+
import torch
|
| 103 |
+
text = headline
|
| 104 |
+
if summary:
|
| 105 |
+
text += ". " + summary[:500]
|
| 106 |
+
|
| 107 |
+
inputs = self._tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 108 |
+
with torch.no_grad():
|
| 109 |
+
outputs = self._finbert(**inputs)
|
| 110 |
+
probs = torch.softmax(outputs.logits, dim=1)[0].numpy()
|
| 111 |
+
|
| 112 |
+
# FinBERT: [negative, neutral, positive]
|
| 113 |
+
neg, neu, pos = probs
|
| 114 |
+
# Map to 0-100
|
| 115 |
+
score = 50 + (pos - neg) * 50
|
| 116 |
+
confidence = 1 - neu # Higher confidence when less neutral
|
| 117 |
+
|
| 118 |
+
return {
|
| 119 |
+
'score': max(0, min(100, score)),
|
| 120 |
+
'confidence': float(confidence),
|
| 121 |
+
'probs': {'negative': float(neg), 'neutral': float(neu), 'positive': float(pos)},
|
| 122 |
+
'method': 'finbert'
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
def analyze_article(self, headline: str, summary: str = "",
|
| 126 |
+
timestamp: str = None) -> Dict:
|
| 127 |
+
"""Full article analysis: sentiment + event classification."""
|
| 128 |
+
event_type, event_severity = self.classify_event(headline, summary)
|
| 129 |
+
sentiment = self.finbert_sentiment(headline, summary)
|
| 130 |
+
|
| 131 |
+
# Adjust sentiment for event context
|
| 132 |
+
event_sentiment_override = {
|
| 133 |
+
'earnings': 0,
|
| 134 |
+
'fed': -10,
|
| 135 |
+
'lawsuit': -25,
|
| 136 |
+
'upgrade': +20,
|
| 137 |
+
'downgrade': -20,
|
| 138 |
+
'merger': +15,
|
| 139 |
+
'dividend': +10,
|
| 140 |
+
'product': +15,
|
| 141 |
+
}
|
| 142 |
+
adj_score = sentiment['score']
|
| 143 |
+
if event_type in event_sentiment_override:
|
| 144 |
+
adj_score += event_sentiment_override[event_type]
|
| 145 |
+
sentiment['adjusted_score'] = max(0, min(100, adj_score))
|
| 146 |
+
else:
|
| 147 |
+
sentiment['adjusted_score'] = adj_score
|
| 148 |
+
|
| 149 |
+
return {
|
| 150 |
+
'headline': headline,
|
| 151 |
+
'summary': summary[:200] if summary else "",
|
| 152 |
+
'timestamp': timestamp or datetime.now().isoformat(),
|
| 153 |
+
'sentiment': sentiment,
|
| 154 |
+
'event': {
|
| 155 |
+
'type': event_type,
|
| 156 |
+
'severity': event_severity,
|
| 157 |
+
}
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
def fetch_newsapi(self, query: str, api_key: str = None, days: int = 7) -> List[Dict]:
|
| 161 |
+
"""Fetch news from NewsAPI. Returns list of article analyses."""
|
| 162 |
+
if not api_key:
|
| 163 |
+
api_key = os.environ.get('NEWSAPI_KEY')
|
| 164 |
+
if not api_key:
|
| 165 |
+
return self._mock_news(query)
|
| 166 |
+
|
| 167 |
+
from_date = (datetime.now() - timedelta(days=days)).strftime('%Y-%m-%d')
|
| 168 |
+
url = f"https://newsapi.org/v2/everything?q={query}&from={from_date}&sortBy=publishedAt&language=en&apiKey={api_key}"
|
| 169 |
+
|
| 170 |
+
try:
|
| 171 |
+
r = requests.get(url, timeout=15)
|
| 172 |
+
r.raise_for_status()
|
| 173 |
+
articles = r.json().get('articles', [])
|
| 174 |
+
results = []
|
| 175 |
+
for art in articles[:10]:
|
| 176 |
+
analysis = self.analyze_article(
|
| 177 |
+
art.get('title', ''),
|
| 178 |
+
art.get('description', ''),
|
| 179 |
+
art.get('publishedAt')
|
| 180 |
+
)
|
| 181 |
+
results.append(analysis)
|
| 182 |
+
return results
|
| 183 |
+
except Exception as e:
|
| 184 |
+
print(f"NewsAPI error: {e}")
|
| 185 |
+
return self._mock_news(query)
|
| 186 |
+
|
| 187 |
+
def fetch_yfinance_news(self, ticker: str) -> List[Dict]:
|
| 188 |
+
"""Fetch news from yfinance."""
|
| 189 |
+
try:
|
| 190 |
+
import yfinance as yf
|
| 191 |
+
t = yf.Ticker(ticker)
|
| 192 |
+
news = t.news or []
|
| 193 |
+
results = []
|
| 194 |
+
for item in news[:10]:
|
| 195 |
+
title = item.get('title', '') or item.get('content', {}).get('title', '')
|
| 196 |
+
summary = item.get('summary', '') or item.get('content', {}).get('summary', '')
|
| 197 |
+
analysis = self.analyze_article(title, summary)
|
| 198 |
+
results.append(analysis)
|
| 199 |
+
return results
|
| 200 |
+
except Exception as e:
|
| 201 |
+
print(f"yfinance news error: {e}")
|
| 202 |
+
return self._mock_news(ticker)
|
| 203 |
+
|
| 204 |
+
def aggregate_sentiment(self, articles: List[Dict]) -> Dict:
|
| 205 |
+
"""Aggregate sentiment across articles with recency weighting."""
|
| 206 |
+
if not articles:
|
| 207 |
+
return {'score': 50, 'confidence': 0, 'volume': 0, 'trend': 'neutral'}
|
| 208 |
+
|
| 209 |
+
scores = []
|
| 210 |
+
for art in articles:
|
| 211 |
+
adj = art['sentiment'].get('adjusted_score', art['sentiment']['score'])
|
| 212 |
+
conf = art['sentiment'].get('confidence', 0.5)
|
| 213 |
+
scores.append((adj, conf))
|
| 214 |
+
|
| 215 |
+
if not scores:
|
| 216 |
+
return {'score': 50, 'confidence': 0, 'volume': 0, 'trend': 'neutral'}
|
| 217 |
+
|
| 218 |
+
# Weighted average by confidence
|
| 219 |
+
total_weight = sum(conf for _, conf in scores) + 1e-10
|
| 220 |
+
weighted_score = sum(s * c for s, c in scores) / total_weight
|
| 221 |
+
|
| 222 |
+
# Count by sentiment
|
| 223 |
+
bullish = sum(1 for s, _ in scores if s > 55)
|
| 224 |
+
bearish = sum(1 for s, _ in scores if s < 45)
|
| 225 |
+
neutral = sum(1 for s, _ in scores if 45 <= s <= 55)
|
| 226 |
+
|
| 227 |
+
volume = len(scores)
|
| 228 |
+
if bullish > bearish * 2:
|
| 229 |
+
trend = 'strong_bullish'
|
| 230 |
+
elif bullish > bearish:
|
| 231 |
+
trend = 'bullish'
|
| 232 |
+
elif bearish > bullish * 2:
|
| 233 |
+
trend = 'strong_bearish'
|
| 234 |
+
elif bearish > bullish:
|
| 235 |
+
trend = 'bearish'
|
| 236 |
+
else:
|
| 237 |
+
trend = 'mixed'
|
| 238 |
+
|
| 239 |
+
# Dominant event
|
| 240 |
+
events = [a['event']['type'] for a in articles]
|
| 241 |
+
event_counts = {}
|
| 242 |
+
for e in events:
|
| 243 |
+
event_counts[e] = event_counts.get(e, 0) + 1
|
| 244 |
+
dominant_event = max(event_counts, key=event_counts.get) if event_counts else 'general'
|
| 245 |
+
|
| 246 |
+
return {
|
| 247 |
+
'score': round(weighted_score, 1),
|
| 248 |
+
'confidence': round(total_weight / volume, 2),
|
| 249 |
+
'volume': volume,
|
| 250 |
+
'trend': trend,
|
| 251 |
+
'bullish_count': bullish,
|
| 252 |
+
'bearish_count': bearish,
|
| 253 |
+
'neutral_count': neutral,
|
| 254 |
+
'dominant_event': dominant_event,
|
| 255 |
+
'event_counts': event_counts,
|
| 256 |
+
}
|
| 257 |
+
|
| 258 |
+
def _mock_news(self, query: str) -> List[Dict]:
|
| 259 |
+
"""Mock news for testing without API keys."""
|
| 260 |
+
mock = [
|
| 261 |
+
f"{query} beats earnings expectations, revenue surges 15%",
|
| 262 |
+
f"{query} announces new AI product partnership",
|
| 263 |
+
f"Analysts upgrade {query} to overweight, target raised to $500",
|
| 264 |
+
f"{query} faces supply chain headwinds in Q3",
|
| 265 |
+
f"{query} maintains guidance despite macro uncertainty",
|
| 266 |
+
]
|
| 267 |
+
return [self.analyze_article(h) for h in mock]
|
| 268 |
+
|
| 269 |
+
def get_full_analysis(self, ticker: str, market: str = 'US', period_days: int = 7) -> Dict:
|
| 270 |
+
"""Full news intelligence pipeline for a ticker."""
|
| 271 |
+
# Try yfinance first
|
| 272 |
+
articles = self.fetch_yfinance_news(ticker)
|
| 273 |
+
|
| 274 |
+
# If insufficient, try NewsAPI
|
| 275 |
+
if len(articles) < 3:
|
| 276 |
+
api_articles = self.fetch_newsapi(ticker, days=period_days)
|
| 277 |
+
articles.extend(api_articles)
|
| 278 |
+
|
| 279 |
+
# Deduplicate by headline
|
| 280 |
+
seen = set()
|
| 281 |
+
unique = []
|
| 282 |
+
for a in articles:
|
| 283 |
+
key = a['headline'][:50].lower()
|
| 284 |
+
if key not in seen:
|
| 285 |
+
seen.add(key)
|
| 286 |
+
unique.append(a)
|
| 287 |
+
|
| 288 |
+
sentiment = self.aggregate_sentiment(unique)
|
| 289 |
+
sentiment['articles'] = unique[:5] # Top 5
|
| 290 |
+
sentiment['ticker'] = ticker
|
| 291 |
+
sentiment['market'] = market
|
| 292 |
+
sentiment['timestamp'] = datetime.now().isoformat()
|
| 293 |
+
return sentiment
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
if __name__ == '__main__':
|
| 297 |
+
ni = NewsIntelligence()
|
| 298 |
+
result = ni.get_full_analysis('AAPL')
|
| 299 |
+
print(f"Sentiment Score: {result['score']}/100")
|
| 300 |
+
print(f"Trend: {result['trend']}")
|
| 301 |
+
print(f"Dominant Event: {result['dominant_event']}")
|
| 302 |
+
print(f"Article Count: {result['volume']}")
|
| 303 |
+
for art in result['articles'][:3]:
|
| 304 |
+
print(f"\n 📰 {art['headline']}")
|
| 305 |
+
print(f" Score: {art['sentiment']['adjusted_score']:.1f} | Event: {art['event']['type']}")
|