chartbastan/backend/app/schemas/sentiment_score.py
2026-02-01 09:31:38 +01:00

78 lines
3.7 KiB
Python

"""
Pydantic schemas for sentiment scores.
This module defines request and response schemas for sentiment analysis operations.
"""
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field, ConfigDict
class SentimentScoreBase(BaseModel):
"""Base schema for sentiment score data."""
entity_id: str = Field(..., description="Foreign key to the entity being analyzed")
entity_type: str = Field(..., description="Type of entity ('tweet' or 'reddit_post')")
score: float = Field(..., ge=-1.0, le=1.0, description="Overall compound sentiment score")
sentiment_type: str = Field(..., description="Classification ('positive', 'negative', or 'neutral')")
positive: float = Field(default=0.0, ge=0.0, le=1.0, description="Positive proportion score")
negative: float = Field(default=0.0, ge=0.0, le=1.0, description="Negative proportion score")
neutral: float = Field(default=0.0, ge=0.0, le=1.0, description="Neutral proportion score")
class SentimentScoreCreate(SentimentScoreBase):
"""Schema for creating a new sentiment score."""
pass
class SentimentScoreResponse(SentimentScoreBase):
"""Schema for sentiment score response."""
id: int = Field(..., description="Primary key")
created_at: datetime = Field(..., description="Timestamp when the sentiment was analyzed")
model_config = ConfigDict(from_attributes=True)
class SentimentAnalysisRequest(BaseModel):
"""Schema for requesting sentiment analysis."""
text: str = Field(..., min_length=1, max_length=1000, description="Text to analyze")
class SentimentAnalysisResponse(BaseModel):
"""Schema for sentiment analysis response."""
compound: float = Field(..., ge=-1.0, le=1.0, description="Overall compound score")
positive: float = Field(..., ge=0.0, le=1.0, description="Positive proportion")
negative: float = Field(..., ge=0.0, le=1.0, description="Negative proportion")
neutral: float = Field(..., ge=0.0, le=1.0, description="Neutral proportion")
sentiment: str = Field(..., description="Classification ('positive', 'negative', or 'neutral')")
class BatchSentimentAnalysisRequest(BaseModel):
"""Schema for requesting batch sentiment analysis."""
texts: list[str] = Field(..., min_length=1, max_length=1000, description="List of texts to analyze")
class BatchSentimentAnalysisResponse(BaseModel):
"""Schema for batch sentiment analysis response."""
results: list[SentimentAnalysisResponse] = Field(..., description="List of sentiment analysis results")
total_count: int = Field(..., description="Total number of texts analyzed")
class AggregatedSentimentMetrics(BaseModel):
"""Schema for aggregated sentiment metrics."""
total_count: int = Field(..., ge=0, description="Total number of sentiments")
positive_count: int = Field(..., ge=0, description="Count of positive sentiments")
negative_count: int = Field(..., ge=0, description="Count of negative sentiments")
neutral_count: int = Field(..., ge=0, description="Count of neutral sentiments")
positive_ratio: float = Field(..., ge=0.0, le=1.0, description="Ratio of positive sentiments")
negative_ratio: float = Field(..., ge=0.0, le=1.0, description="Ratio of negative sentiments")
neutral_ratio: float = Field(..., ge=0.0, le=1.0, description="Ratio of neutral sentiments")
average_compound: float = Field(..., ge=-1.0, le=1.0, description="Average compound score")
class SentimentScoreListResponse(BaseModel):
"""Schema for a list of sentiment scores."""
data: list[SentimentScoreResponse]
count: int = Field(..., description="Total number of sentiment scores")
meta: dict = Field(default_factory=dict, description="Additional metadata")