Class: SentimentInsights::Clients::Sentiment::SentimentalClient

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

Overview

Client that uses the Sentimental gem for local sentiment analysis.

Instance Method Summary collapse

Constructor Details

#initializeSentimentalClient

Returns a new instance of SentimentalClient.



8
9
10
11
# File 'lib/sentiment_insights/clients/sentiment/sentimental_client.rb', line 8

def initialize
  @analyzer = Sentimental.new
  @analyzer.load_defaults  # load built-in positive/negative word scores
end

Instance Method Details

#analyze_entries(entries, question: nil, prompt: nil, batch_size: nil) ⇒ Array<Hash>

Analyzes each entry’s answer text and returns an array of sentiment results.

Parameters:

  • entries (Array<Hash>)

    An array of response hashes (each with :answer).

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

    (unused) Global question context, not needed for local analysis.

Returns:

  • (Array<Hash>)

    An array of hashes with sentiment classification and score for each entry.



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sentiment_insights/clients/sentiment/sentimental_client.rb', line 17

def analyze_entries(entries, question: nil, prompt: nil, batch_size: nil)
  puts "Inside sentimental"
  entries.map do |entry|
    text = entry[:answer].to_s.strip
    label = @analyzer.sentiment(text)  # :positive, :neutral, or :negative
    score = case label
            when :positive then 1.0
            when :negative then -1.0
            else 0.0
            end
    { label: label, score: score }
  end
end