Class: SentimentInsights::Clients::Sentiment::AwsComprehendClient

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

Constant Summary collapse

MAX_BATCH_SIZE =

AWS limit

25

Instance Method Summary collapse

Constructor Details

#initialize(region: 'us-east-1') ⇒ AwsComprehendClient

Returns a new instance of AwsComprehendClient.



10
11
12
13
# File 'lib/sentiment_insights/clients/sentiment/aws_comprehend_client.rb', line 10

def initialize(region: 'us-east-1')
  @client = Aws::Comprehend::Client.new(region: region)
  @logger = Logger.new($stdout)
end

Instance Method Details

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

Analyze a batch of entries using AWS Comprehend.

Parameters:

  • entries (Array<Hash>)

    each with :answer key

Returns:

  • (Array<Hash>)

    each with :label (symbol) and :score (float)



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/sentiment_insights/clients/sentiment/aws_comprehend_client.rb', line 18

def analyze_entries(entries, question: nil, prompt: nil, batch_size: nil)
  results = []

  entries.each_slice(MAX_BATCH_SIZE) do |batch|
    texts = batch.map { |entry| entry[:answer].to_s.strip[0...5000] } # max per AWS

    begin
      resp = @client.batch_detect_sentiment({
                                              text_list: texts,
                                              language_code: "en"
                                            })

      resp.result_list.each do |r|
        label = r.sentiment.downcase.to_sym  # :positive, :neutral, :negative, :mixed
        score = compute_score(r.sentiment, r.sentiment_score)
        results << { label: label, score: score }
      end

      # handle errors (will match by index)
      resp.error_list.each do |error|
        @logger.warn "AWS Comprehend error at index #{error.index}: #{error.error_code}"
        results.insert(error.index, { label: :neutral, score: 0.0 })
      end

    rescue Aws::Comprehend::Errors::ServiceError => e
      @logger.error "AWS Comprehend batch error: #{e.message}"
      batch.size.times { results << { label: :neutral, score: 0.0 } }
    end
  end

  results
end