""" SQLAlchemy model for sentiment scores. This module defines the database model for storing sentiment analysis results from tweets and posts. """ from datetime import datetime from sqlalchemy import Column, Integer, String, Float, DateTime, Index, ForeignKey from sqlalchemy.orm import relationship from app.database import Base class SentimentScore(Base): """ Model for storing sentiment analysis results. Attributes: id: Primary key entity_id: Foreign key to the entity being analyzed (tweet_id or post_id) entity_type: Type of entity ('tweet' or 'reddit_post') score: Overall compound sentiment score (-1 to 1) sentiment_type: Classification ('positive', 'negative', or 'neutral') positive: Positive proportion score (0 to 1) negative: Negative proportion score (0 to 1) neutral: Neutral proportion score (0 to 1) created_at: Timestamp when the sentiment was analyzed """ __tablename__ = "sentiment_scores" id = Column(Integer, primary_key=True, index=True) entity_id = Column(String(255), nullable=False, index=True) entity_type = Column(String(50), nullable=False, index=True) # 'tweet' or 'reddit_post' score = Column(Float, nullable=False, index=True) # Compound score sentiment_type = Column(String(20), nullable=False, index=True) # 'positive', 'negative', 'neutral' positive = Column(Float, nullable=False, default=0.0) negative = Column(Float, nullable=False, default=0.0) neutral = Column(Float, nullable=False, default=0.0) created_at = Column(DateTime, nullable=False, index=True, default=datetime.utcnow) # Indexes for performance __table_args__ = ( Index('idx_sentiment_scores_entity', 'entity_id', 'entity_type'), Index('idx_sentiment_scores_score', 'score'), Index('idx_sentiment_scores_type', 'sentiment_type'), Index('idx_sentiment_scores_created_at', 'created_at'), ) def __repr__(self) -> str: return f"" def to_dict(self) -> dict: """Convert sentiment score model to dictionary.""" return { 'id': self.id, 'entity_id': self.entity_id, 'entity_type': self.entity_type, 'score': self.score, 'sentiment_type': self.sentiment_type, 'positive': self.positive, 'negative': self.negative, 'neutral': self.neutral, 'created_at': self.created_at.isoformat() if self.created_at else None }