Feature: Smart Comment Filter
Relevant source files
The following files were used as context for generating this wiki page:
The Smart Comment Filter is an intelligent system designed to pre-process real-time YouTube comments by selecting only those relevant to Indonesian football and the national team (Timnas). Its primary purpose is to improve the quality of data fed into the sentiment analysis models, thereby increasing accuracy, reducing processing time, and providing more meaningful insights to the end-user. The system employs a hybrid approach, prioritizing Google's Gemini AI for contextual understanding and using a robust, rule-based fallback mechanism.
This feature is a key component of the v5.0.0 release, contributing to a 90%+ relevance rate for analyzed comments and a 60% faster processing speed. It is primarily utilized in the Live Comments and Live Analysis sections of the dashboard.
Sources: SMART_COMMENT_FILTER.md, CHANGELOG.md:30, DEPLOYMENT_SUMMARY.md:18
Architecture Overview
The Smart Comment Filter operates through a multi-layered architecture, combining AI-driven analysis with rule-based systems for both efficiency and reliability. The core logic resides in two main places: the Python backend for AI interaction and the Next.js API route for advanced scoring and integration with the frontend.
The general data flow for filtering a comment is as follows:
This diagram illustrates the two main filtering stages: an initial relevance scoring in the Next.js API and the deeper AI/fallback analysis in the Python backend.
Sources: dashboard-next/app/api/live-comments/route.ts:180-192, src/modeling/comment_filter.py:28-31
AI-Powered Filtering (Gemini)
The primary filtering mechanism uses the gemini-2.5-flash model from Google's Generative AI to determine comment relevance. This is handled by the GeminiCommentFilter class in the Python backend.
Sources: src/modeling/comment_filter.py:10
Logic Flow
- Rate Limit Check: Before making an API call, the system checks if the request count is within the defined limit (10 requests per hour). If the limit is exceeded, it bypasses the AI and uses the Python fallback filter.
- Prompt Generation: A detailed prompt is constructed, instructing the AI to analyze the comment based on specific criteria for relevance to Indonesian football.
- API Call: The prompt is sent to the Gemini model.
- Response Parsing: The system expects a JSON response and parses it to extract the
relevant(boolean) andreason(string) fields. - Fallback on Error: If the API call fails or the response is malformed, the system defaults to the Python fallback filter.
Sources: src/modeling/comment_filter.py:12-63
Gemini AI Prompt
The system uses a specific prompt in Indonesian to guide the AI's decision-making process.
Analisis apakah komentar ini relevan untuk analisis sentiment sepak bola/timnas Indonesia:
"{comment}"
Kriteria RELEVAN:
- Membahas sepak bola, timnas, pemain, pelatih, pertandingan
- Mengandung opini, kritik, dukungan, atau emosi
- Memiliki konteks yang jelas
Kriteria TIDAK RELEVAN:
- Spam, promosi, link
- Tidak ada hubungan dengan sepak bola
- Hanya emoji atau singkatan tidak jelas
- Komentar random tanpa konteks
Jawab JSON: {{"relevant": true/false, "reason": "alasan singkat"}}
Sources: src/modeling/comment_filter.py:33-47
Rate Limiting
To manage API costs and usage, a conservative rate limit is implemented.
| Parameter | Value | Description |
|---|---|---|
max_requests | 10 | Maximum number of Gemini API calls allowed. |
reset_interval | 3600 seconds | The time window (1 hour) after which the count resets. |
Sources: src/modeling/comment_filter.py:14, src/modeling/comment_filter.py:19
Advanced Relevance Scoring & Fallback
When the Gemini AI is unavailable or bypassed, the system uses a sophisticated, score-based filtering algorithm implemented in the Next.js API route. This system assigns a relevance score to each comment based on a set of positive and negative factors.
Sources: ENHANCED_FILTERING_SYSTEM.md, dashboard-next/app/api/live-comments/route.ts:74-162
Scoring Algorithm
The filterRelevantComments function calculates a relevanceScore for each comment. A comment is deemed relevant if its final score is greater than 0.
| Factor | Score Change | Description & Keywords |
|---|---|---|
| Positive Factors | ||
| Football Context | +3 | Contains keywords like timnas, pssi, sepak bola, player names, etc. |
| Opinion Detected | +2 | Contains opinion words like bagus, jelek, harusnya, why, etc. |
| Detailed Comment | +1 | Comment length is greater than 30 characters. |
| Contains Question | +1 | The comment text includes a ?. |
| Negative Factors | ||
| Spam Detected | -5 | Contains spam keywords like subscribe, promo, http, .com, etc. |
| Too Short | -2 | Comment length is less than 8 characters. |
| Emoji Only | -3 | More than 60% of the comment consists of non-alphanumeric characters. |
| Repeated Characters | -2 | Contains 5 or more of the same character repeated consecutively (e.g., aaaaa). |
| All Caps | -1 | The comment is longer than 10 characters and is in all uppercase. |
Sources: ENHANCED_FILTERING_SYSTEM.md:38-51, dashboard-next/app/api/live-comments/route.ts:129-155
Python Fallback Filter
The src/modeling/comment_filter.py file contains a simpler, secondary fallback method (_fallback_filter) that is used only when the Gemini API call fails within the Python service itself. It uses keyword matching for football context and spam indicators.
Sources: src/modeling/comment_filter.py:65-101
API and UI Integration
The Smart Comment Filter is primarily exposed through the /api/live-comments API endpoint and controlled via the Live Comments page in the dashboard.
API Endpoint: GET /api/live-comments
This endpoint fetches and processes comments from YouTube. The filtering behavior is controlled by query parameters.
| Parameter | Type | Default | Description |
|---|---|---|---|
videoId | string | lDtSjKb_8Jo | The ID of the YouTube video to fetch comments from. |
filter | boolean | true | If true, enables the Advanced Relevance Scoring. Set to false to get raw comments. |
sentiment | boolean | false | If true, performs sentiment analysis on the filtered comments. |
maxResults | integer | 200 | The maximum number of comments to fetch (options: 100, 200, 500, 1000). |
Sources: dashboard-next/app/api/live-comments/route.ts:178-182, SMART_COMMENT_FILTER.md:27-33
The API response is enhanced with filtering metadata when the feature is active.
{
"comments": [
{
"id": "...",
"text": "Timnas harus main lebih bagus",
"relevanceScore": 5,
"filterReason": "Football context",
"sentiment": "negative",
"confidence": 0.85,
"reasoning": "Criticism pattern detected"
}
],
"total": 45,
"totalFetched": 200,
"filtered": true,
"maxResults": 200,
"sentimentAnalysis": {
"enabled": true,
"summary": {"positive": 15, "negative": 25, "neutral": 5}
}
}
Sources: ENHANCED_FILTERING_SYSTEM.md:21-35, dashboard-next/app/api/live-comments/route.ts:241-252
User Interface
The /live-comments page provides user controls for the filtering system.
This diagram shows the user interaction flow on the Live Comments page to activate the filter.
- Smart Filter Toggle: A checkbox allows users to enable or disable the filter.
- Status Indicators: The UI displays whether the filter is active and shows the count of relevant comments versus the total fetched.
- Relevance Score: Each filtered comment can display its relevance score.
- Filter Reason: The reason for a comment's inclusion (e.g., "Football context", "Opinion detected") is shown.
Sources: dashboard-next/app/live-comments/page.tsx:28, dashboard-next/app/live-comments/page.tsx:106-111, dashboard-next/app/live-comments/page.tsx:197-201
Conclusion
The Smart Comment Filter is a critical feature that significantly enhances the quality and relevance of the data used for sentiment analysis. By combining the contextual power of Gemini AI with a robust, score-based fallback system, it effectively removes spam and irrelevant content. This results in a 60% reduction in processing time, a 90%+ comment relevance rate, and ultimately, more accurate and trustworthy sentiment insights for analyzing public opinion on Indonesian football.