Implementation:666ghj BettaFish WeiboMultilingualSentimentAnalyzer analyze
| Knowledge Sources | |
|---|---|
| Domains | NLP, Sentiment_Analysis, Deep_Learning |
| Last Updated | 2026-10-08 00:00 GMT |
Overview
Concrete sentiment analyzer that wraps the HuggingFace `tabularisai/multilingual-sentiment-analysis` model to classify text into 5-level sentiment (very negative to very positive) across 22 languages.
Description
The WeiboMultilingualSentimentAnalyzer class provides a unified interface for sentiment analysis on social media content. It loads a pretrained transformer model (`tabularisai/multilingual-sentiment-analysis`) that supports 22 languages, auto-selects the best available device (CUDA > MPS > CPU), and provides batch analysis capabilities. The analyzer is used by both the InsightEngine (for DB search result sentiment) and the Sentiment Model Training workflow (for production deployment).
Usage
Import this class when you need to analyze sentiment of social media text. It is used by InsightEngine's `DeepSearchAgent` to add sentiment analysis to search results, and can be used standalone for any multilingual sentiment classification task.
Code Reference
Source Location
- Repository: BettaFish
- File: `InsightEngine/tools/sentiment_analyzer.py:L79-703`
Signature
class WeiboMultilingualSentimentAnalyzer:
def __init__(self):
"""Initialize analyzer (model loaded lazily on initialize())."""
def initialize(self) -> bool:
"""Load model and tokenizer from HuggingFace. Returns success bool."""
def analyze_batch(self, texts: List[str]) -> List[Dict[str, Any]]:
"""Analyze a batch of texts. Returns list of sentiment predictions."""
def analyze_query_results(
self,
query_results: List[Dict[str, Any]],
text_field: str = "content",
min_confidence: float = 0.5
) -> Dict[str, Any]:
"""
Analyze sentiment of search results.
Args:
query_results: List of dicts with text content
text_field: Field name containing text to analyze
min_confidence: Minimum confidence threshold for high_confidence_results
Returns:
Dict with:
- total_analyzed: int
- sentiment_distribution: {非常负面: int, 负面: int, 中性: int, 正面: int, 非常正面: int}
- high_confidence_results: List
- summary: str
"""
Import
from InsightEngine.tools.sentiment_analyzer import WeiboMultilingualSentimentAnalyzer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| query_results | List[Dict] | Yes | Search results with text content |
| text_field | str | No | Field containing text (default: "content") |
| min_confidence | float | No | Confidence threshold (default: 0.5) |
Outputs
| Name | Type | Description |
|---|---|---|
| total_analyzed | int | Number of texts analyzed |
| sentiment_distribution | Dict[str, int] | 5-level sentiment counts |
| high_confidence_results | List | Results above confidence threshold |
| summary | str | Text summary of overall sentiment |
Usage Examples
Analyze Search Results
from InsightEngine.tools.sentiment_analyzer import WeiboMultilingualSentimentAnalyzer
analyzer = WeiboMultilingualSentimentAnalyzer()
analyzer.initialize() # Loads model from HuggingFace
# Analyze DB search results
results = [
{"content": "这个产品真的太好用了!", "platform": "weibo"},
{"content": "服务态度极差,不推荐", "platform": "xhs"},
{"content": "一般般,没有特别的感觉", "platform": "zhihu"},
]
analysis = analyzer.analyze_query_results(results, text_field="content")
print(f"Analyzed: {analysis['sentiment_analysis']['total_analyzed']}")
print(f"Distribution: {analysis['sentiment_analysis']['sentiment_distribution']}")
# e.g., {"非常正面": 1, "非常负面": 1, "中性": 1, "负面": 0, "正面": 0}
Related Pages
Implements Principle
- Principle:666ghj_BettaFish_Multilingual_Sentiment_Classification
- Principle:666ghj_BettaFish_Sentiment_Model_Deployment