sentiment

**Sentiment Analysis and Text Classification** **Sentiment Analysis** Determine the emotional tone or opinion in text. **Approaches** | Approach | Speed | Accuracy | Customization | |----------|-------|----------|---------------| | Rule-based | Fast | Low | Easy | | Traditional ML | Fast | Medium | Medium | | Transformer | Medium | High | High | | LLM | Slow | Highest | Very easy | **LLM Sentiment Analysis** ```python def analyze_sentiment(text: str) -> dict: result = llm.generate(f""" Analyze the sentiment of this text. Return JSON with: - sentiment: positive, negative, or neutral - confidence: 0-1 - explanation: brief reason Text: {text} """) return json.loads(result) ``` **Structured Output** ```python from pydantic import BaseModel class SentimentResult(BaseModel): sentiment: Literal["positive", "negative", "neutral"] confidence: float aspects: list[dict] # Aspect-based sentiment result = instructor_client.create( response_model=SentimentResult, messages=[{"role": "user", "content": text}] ) ``` **Text Classification** **Intent Detection** ```python intents = ["question", "command", "greeting", "complaint", "feedback"] def classify_intent(text: str) -> str: result = llm.generate(f""" Classify this message into one category: Categories: {intents} Message: {text} Category: """) return result.strip() ``` **Topic Classification** ```python def classify_topics(text: str) -> list: result = llm.generate(f""" Assign relevant topics to this text. Available topics: technology, business, health, sports, politics Text: {text} Topics (comma-separated): """) return [t.strip() for t in result.split(",")] ``` **Multi-Label Classification** ```python class Classification(BaseModel): categories: list[str] confidence: dict[str, float] primary_category: str result = instructor_client.create( response_model=Classification, messages=[{"role": "user", "content": f"Classify: {text}"}] ) ``` **Batch Processing** ```python def classify_batch(texts: list, categories: list) -> list: results = [] for text in texts: # Use async for parallelization result = classify(text, categories) results.append(result) return results ``` **Best Practices** - Use few-shot examples for consistent classification - Validate outputs against known categories - Consider classification confidence for uncertain cases - Fine-tune smaller models for high-volume use cases

Go deeper with CFSGPT

Get AI-powered deep-dives, save terms, and run advanced simulations — free account.

Create Free Account