Sentiment Engine
The Sentiment Engine is the core analysis system that powers SENTi-radar’s emotion detection, sentiment classification, and theme identification. It processes text from multiple sources (X, Reddit, YouTube, News RSS) and produces actionable insights.Architecture
The engine is implemented inTopicDetail.tsx (lines 34-313) and consists of three main components:
- Emotion Lexicon — Keyword dictionaries for 6 emotions
- Theme Detection — Domain-specific keyword matching
- Sentiment Scorer — Aggregates emotion data into sentiment classifications
Emotion Lexicon
The engine uses a keyword-based lexicon mapping emotions to trigger words:Keywords are matched case-insensitively using regex pattern matching. Partial matches are supported (e.g., “frustrat” matches “frustrated”, “frustration”, “frustrating”).
Core Functions
scoreEmotions
Analyzes an array of text strings and returns emotion distribution.
texts(string[]): Array of text samples (posts, comments, headlines)
EmotionData objects sorted by percentage (highest first)
EmotionData Interface:
- Join all texts into a single lowercase string
- For each emotion, count matches of all keywords using regex
- Calculate percentage:
(emotion_count / total_matches) * 100 - Sort by percentage descending
- Normalize to ensure sum equals exactly 100%
Theme Detection
The engine identifies topic themes using domain-specific keyword matching:Theme Detection Algorithm
FromanalyzeTopicFully (lines 251-257):
Sentiment Analysis
analyzeTopicFully
Master analysis function that combines emotion scoring, sentiment classification, and theme detection.
topicTitle(string): The topic being analyzed (e.g., “AI Regulation Debate”)headlines(string[]): News headlines from Google News RSScomments(string[]): YouTube comments and video titlesscrapedPosts(ScrapedPost[]): Posts from X and Reddit via Scrape.doscrapeDoResults(ScrapeDoResult[]): Per-source status info
AnalysisResult object
AnalysisResult Interface
Sentiment Classification Logic
From lines 262-268:- Negative: Negative keywords > Positive keywords × 1.3
- Positive: Positive keywords > Negative keywords × 1.3
- Mixed: Neither condition met
Crisis Level Detection
From line 268:- High: 4+ negative keywords detected
- Medium: 2-3 negative keywords
- None: 0-1 negative keywords
Local Summary Generation
buildLocalSummary
Generates a markdown summary when LLMs are unavailable (fallback mode).
topic(TopicCard): The topic card objectanalysis(AnalysisResult): Analysis result fromanalyzeTopicFully
LLM Integration
buildLLMPrompt
Constructs prompts for Gemini or Groq LLMs to generate enhanced summaries.
system(string): System prompt defining assistant behavioruser(string): User prompt with analysis data and format instructions
- Tier 1: Gemini 2.0 Flash (if
VITE_GEMINI_API_KEYset) - Tier 2: Groq Llama 3.3 70B (if
VITE_GROQ_API_KEYset) - Tier 3: Local summary (guaranteed, no API required)
Data Source Labeling
buildDataSourceLabel
Constructs a human-readable label listing all active data sources.
"YouTube + News RSS + X via Scrape.do + Reddit via Scrape.do""X via Scrape.do + Reddit via Scrape.do""Keyword Analysis"(when no sources returned data)
Orchestration Function
streamSummary
Master orchestrator that fetches data from all sources, analyzes it, and streams the summary.
-
Fetch data in parallel (lines 462-473):
- YouTube comments via YouTube Data API v3
- Google News headlines via RSS
- X and Reddit posts via Scrape.do
-
Analyze all data (line 483):
-
Emit emotions immediately (lines 500-505):
- Generate summary with LLM or local fallback (lines 512-570)
Performance Considerations
Parallel Data Fetching
All data sources are fetched in parallel usingPromise.allSettled:
- No blocking on slow sources
- Failures in one source don’t break others
- Maximum throughput
Regex Optimization
Keyword matching escapes special regex characters:Extending the Engine
Adding New Emotions
-
Add keywords to
EMOTION_KEYWORDS: -
Update the
Emotiontype inmockData.ts:
Adding New Themes
- Add theme configuration to
TOPIC_THEMES:
Custom Sentiment Rules
Modify sentiment classification thresholds:Related Documentation
- Scrape.do Provider — Fetches X and Reddit posts for analysis
- YouTube Data Source — Fetches YouTube comments and video metadata
- Data Sources Overview — Complete guide to all data sources
- Emotion Classification Feature — User-facing emotion analysis feature