Class: SentimentInsights::Insights::Sentiment

Inherits:
Object
  • Object
show all
Defined in:
lib/sentiment_insights/insights/sentiment.rb

Overview

Analyzes sentiment of survey responses and produces summarized insights.

Constant Summary collapse

DEFAULT_TOP_COUNT =
5

Instance Method Summary collapse

Constructor Details

#initialize(provider: nil, provider_client: nil, top_count: DEFAULT_TOP_COUNT) ⇒ Sentiment

Initialize with a specified provider or a concrete provider client. If no provider is given, default to the Sentimental (local) provider.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sentiment_insights/insights/sentiment.rb', line 15

def initialize(provider: nil, provider_client: nil, top_count: DEFAULT_TOP_COUNT)
  effective_provider = provider || SentimentInsights.configuration&.provider || :sentimental
  @provider_client = provider_client || case effective_provider
                                        when :openai
                                          Clients::Sentiment::OpenAIClient.new
                                        when :claude
                                          Clients::Sentiment::ClaudeClient.new
                                        when :aws
                                          Clients::Sentiment::AwsComprehendClient.new
                                        else
                                          Clients::Sentiment::SentimentalClient.new
                                        end
  @top_count = top_count
end

Instance Method Details

#analyze(entries, question: nil, prompt: nil, batch_size: 50) ⇒ Hash

Analyze a batch of entries and return sentiment insights.

Parameters:

  • entries (Array<Hash>)

    An array of response hashes, each with :answer and :segment.

  • question (String, nil) (defaults to: nil)

    Optional global question text or metadata for context.

Returns:

  • (Hash)

    Summary of sentiment analysis (global, segment-wise, top comments, and annotated responses).



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sentiment_insights/insights/sentiment.rb', line 34

def analyze(entries, question: nil, prompt: nil, batch_size: 50)
  # Ensure entries is an array of hashes with required keys
  entries = entries.to_a
  # Get sentiment results for each entry from the provider client
  results = @provider_client.analyze_entries(entries, question: question, prompt: prompt, batch_size: batch_size)

  # Combine original entries with sentiment results
  annotated_responses = entries.each_with_index.map do |entry, idx|
    res = results[idx] || {}
    {
      answer:  entry[:answer],
      segment: entry[:segment] || {},
      sentiment_label: res[:label],
      sentiment_score: res[:score]
    }
  end

  global_summary = prepare_global_summary(annotated_responses)
  segment_summary = prepare_segment_summary(annotated_responses)

  top_positive_comments, top_negative_comments = top_comments(annotated_responses)

  # Assemble the result hash and wrap with export functionality
  SentimentInsights::Export::Result.wrap({
    global_summary:       global_summary,
    segment_summary:      segment_summary,
    top_positive_comments: top_positive_comments,
    top_negative_comments: top_negative_comments,
    responses:            annotated_responses
  })
end