Class: SentimentInsights::Clients::KeyPhrases::AwsClient
- Inherits:
-
Object
- Object
- SentimentInsights::Clients::KeyPhrases::AwsClient
- Defined in:
- lib/sentiment_insights/clients/key_phrases/aws_client.rb
Constant Summary collapse
- MAX_BATCH_SIZE =
25
Instance Method Summary collapse
- #extract_batch(entries, question: nil, key_phrase_prompt: nil, sentiment_prompt: nil) ⇒ Object
-
#initialize(region: 'us-east-1') ⇒ AwsClient
constructor
A new instance of AwsClient.
Constructor Details
#initialize(region: 'us-east-1') ⇒ AwsClient
Returns a new instance of AwsClient.
10 11 12 13 |
# File 'lib/sentiment_insights/clients/key_phrases/aws_client.rb', line 10 def initialize(region: 'us-east-1') @comprehend = Aws::Comprehend::Client.new(region: region) @logger = Logger.new($stdout) end |
Instance Method Details
#extract_batch(entries, question: nil, key_phrase_prompt: nil, sentiment_prompt: nil) ⇒ Object
15 16 17 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/sentiment_insights/clients/key_phrases/aws_client.rb', line 15 def extract_batch(entries, question: nil, key_phrase_prompt: nil, sentiment_prompt: nil) responses = [] phrase_map = Hash.new { |h, k| h[k] = [] } # Split into batches for AWS Comprehend entries.each_slice(MAX_BATCH_SIZE).with_index do |batch, batch_idx| texts = batch.map { |e| e[:answer].to_s.strip[0...5000] } begin phrase_resp = @comprehend.batch_detect_key_phrases({ text_list: texts, language_code: 'en' }) sentiment_resp = @comprehend.batch_detect_sentiment({ text_list: texts, language_code: 'en' }) phrase_resp.result_list.each_with_index do |phrase_result, idx| sentiment_result = sentiment_resp.result_list.find { |s| s.index == phrase_result.index } sentiment_label = sentiment_result&.sentiment&.downcase&.to_sym || :neutral entry_index = (batch_idx * MAX_BATCH_SIZE) + idx entry = entries[entry_index] sentence = texts[idx] response_id = "r_#{entry_index + 1}" responses << { id: response_id, sentence: sentence, sentiment: sentiment_label, segment: entry[:segment] || {} } phrases = phrase_result.key_phrases.map { |p| p.text.downcase.strip }.uniq phrases.each { |phrase| phrase_map[phrase] << response_id } end phrase_resp.error_list.each do |error| @logger.warn "AWS KeyPhrase error at index #{error.index}: #{error.error_code}" end sentiment_resp.error_list.each do |error| @logger.warn "AWS Sentiment error at index #{error.index}: #{error.error_code}" end rescue Aws::Comprehend::Errors::ServiceError => e @logger.error "AWS Comprehend batch error: #{e.message}" batch.each_with_index do |entry, i| entry_index = (batch_idx * MAX_BATCH_SIZE) + i responses << { id: "r_#{entry_index + 1}", sentence: entry[:answer], sentiment: :neutral, segment: entry[:segment] || {} } end end end phrases = phrase_map.map do |phrase, ref_ids| { phrase: phrase, mentions: ref_ids.uniq, summary: nil } end { phrases: phrases, responses: responses } end |