sentiment

package
v0.1.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 22, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Analysis

type Analysis struct {
	Score      Score     `json:"score"`
	Label      string    `json:"label"`
	Confidence float64   `json:"confidence"`
	Keywords   []string  `json:"keywords,omitempty"`
	AnalyzedAt time.Time `json:"analyzedAt"`
}

Analysis represents the sentiment analysis result for a single message or conversation.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler provides HTTP handlers for the sentiment API.

func NewHandler

func NewHandler(analyzer *SentimentAnalyzer, detector *TrendDetector) *Handler

NewHandler creates a new sentiment API handler.

func (*Handler) HandleAnalyzeSentiment

func (h *Handler) HandleAnalyzeSentiment(w http.ResponseWriter, r *http.Request)

HandleAnalyzeSentiment handles POST /api/conversations/{id}/sentiment. Analyzes messages and returns sentiment timeline with trend.

func (*Handler) HandleGetSentiment

func (h *Handler) HandleGetSentiment(w http.ResponseWriter, r *http.Request)

HandleGetSentiment handles GET /api/conversations/{id}/sentiment. Returns the stored trend for a conversation.

func (*Handler) RegisterRoutes

func (h *Handler) RegisterRoutes(mux *http.ServeMux)

RegisterRoutes registers the sentiment API routes on a ServeMux.

type LLMProvider

type LLMProvider interface {
	AnalyzeSentiment(ctx context.Context, systemPrompt, userPrompt string) (*Analysis, error)
}

LLMProvider abstracts the AI sentiment analysis backend.

type Message

type Message struct {
	ID        string    `json:"id"`
	Body      string    `json:"body"`
	Role      string    `json:"role"`
	Timestamp time.Time `json:"timestamp"`
}

Message represents a message for sentiment analysis.

type Score

type Score float64

Score represents a sentiment score from -1 (very negative) to 1 (very positive).

const (
	ScoreVeryNegative Score = -1.0
	ScoreNegative     Score = -0.5
	ScoreNeutral      Score = 0.0
	ScorePositive     Score = 0.5
	ScoreVeryPositive Score = 1.0
)

func (Score) Label

func (s Score) Label() string

Label returns a human-readable label for the score.

type SentimentAnalyzer

type SentimentAnalyzer struct {
	// contains filtered or unexported fields
}

SentimentAnalyzer performs sentiment analysis on messages.

func NewSentimentAnalyzer

func NewSentimentAnalyzer(provider LLMProvider) *SentimentAnalyzer

NewSentimentAnalyzer creates a new SentimentAnalyzer. If provider is nil, only lexicon-based analysis is used.

func (*SentimentAnalyzer) Analyze

func (a *SentimentAnalyzer) Analyze(ctx context.Context, text string) (*Analysis, error)

Analyze performs sentiment analysis on a single text.

func (*SentimentAnalyzer) AnalyzeMessages

func (a *SentimentAnalyzer) AnalyzeMessages(ctx context.Context, messages []Message) ([]Analysis, error)

AnalyzeMessages performs sentiment analysis on multiple messages.

type TimelinePoint

type TimelinePoint struct {
	Timestamp time.Time `json:"timestamp"`
	Score     Score     `json:"score"`
	Label     string    `json:"label"`
	MessageID string    `json:"messageId,omitempty"`
}

TimelinePoint represents a sentiment measurement at a point in time.

type Trend

type Trend struct {
	ConversationID string          `json:"conversationId"`
	Points         []TimelinePoint `json:"points"`
	CurrentScore   Score           `json:"currentScore"`
	AverageScore   Score           `json:"averageScore"`
	Direction      string          `json:"direction"` // "improving", "declining", "stable"
	SharpDrop      bool            `json:"sharpDrop"`
	SharpDropAt    *time.Time      `json:"sharpDropAt,omitempty"`
}

Trend represents a sentiment trend over a conversation.

type TrendConfig

type TrendConfig struct {
	SharpDropThreshold float64 // score drop per message that triggers alert (default 0.5)
	AlertCallback      func(conversationID string, trend *Trend)
}

TrendConfig holds configuration for the TrendDetector.

func DefaultTrendConfig

func DefaultTrendConfig() TrendConfig

DefaultTrendConfig returns default trend detection configuration.

type TrendDetector

type TrendDetector struct {
	// contains filtered or unexported fields
}

TrendDetector tracks sentiment trends over conversation timelines.

func NewTrendDetector

func NewTrendDetector(analyzer *SentimentAnalyzer, cfg TrendConfig) *TrendDetector

NewTrendDetector creates a new TrendDetector.

func (*TrendDetector) GetTrend

func (d *TrendDetector) GetTrend(conversationID string) (*Trend, bool)

GetTrend returns the stored trend for a conversation.

func (*TrendDetector) TrackConversation

func (d *TrendDetector) TrackConversation(ctx context.Context, conversationID string, messages []Message) (*Trend, error)

TrackConversation analyzes all messages and builds a sentiment timeline.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL