Home Knowledge Base Sentiment Analysis and Text Classification

Sentiment Analysis and Text Classification

Sentiment Analysis Determine the emotional tone or opinion in text.

Approaches

ApproachSpeedAccuracyCustomization
Rule-basedFastLowEasy
Traditional MLFastMediumMedium
TransformerMediumHighHigh
LLMSlowHighestVery easy

LLM Sentiment Analysis

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

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

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

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

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

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

sentimentclassificationintent

Explore 500+ Semiconductor & AI Topics

From EUV lithography to CUDA optimization — search the full knowledge base or chat with our AI assistant.