Class: SentimentInsights::Insights::Entities
- Inherits:
-
Object
- Object
- SentimentInsights::Insights::Entities
- Defined in:
- lib/sentiment_insights/insights/entities.rb
Overview
Extracts and summarizes named entities from survey responses
Instance Method Summary collapse
-
#extract(entries, question: nil, prompt: nil) ⇒ Hash
Extract named entities and build summarized output.
-
#initialize(provider: nil, provider_client: nil) ⇒ Entities
constructor
A new instance of Entities.
Constructor Details
#initialize(provider: nil, provider_client: nil) ⇒ Entities
Returns a new instance of Entities.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/sentiment_insights/insights/entities.rb', line 7 def initialize(provider: nil, provider_client: nil) effective_provider = provider || SentimentInsights.configuration&.provider || :sentimental @provider_client = provider_client || case effective_provider when :openai require_relative '../clients/entities/open_ai_client' Clients::Entities::OpenAIClient.new when :claude require_relative '../clients/entities/claude_client' Clients::Entities::ClaudeClient.new when :aws require_relative '../clients/entities/aws_client' Clients::Entities::AwsClient.new when :sentimental raise NotImplementedError, "Entity recognition is not supported for the 'sentimental' provider" else raise ArgumentError, "Unsupported provider: #{effective_provider}" end end |
Instance Method Details
#extract(entries, question: nil, prompt: nil) ⇒ Hash
Extract named entities and build summarized output
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 |
# File 'lib/sentiment_insights/insights/entities.rb', line 30 def extract(entries, question: nil, prompt: nil) entries = entries.to_a raw_result = @provider_client.extract_batch(entries, question: question, prompt: prompt) responses = raw_result[:responses] || [] entities = raw_result[:entities] || [] # Index responses by ID response_index = responses.each_with_object({}) { |r, h| h[r[:id]] = r } enriched_entities = entities.map do |entity_entry| mentions = entity_entry[:mentions] || [] mention_responses = mentions.map { |id| response_index[id] }.compact segment_dist = Hash.new { |h, k| h[k] = Hash.new(0) } mention_responses.each do |resp| (resp[:segment] || {}).each do |seg_key, seg_val| segment_dist[seg_key][seg_val] += 1 end end { entity: entity_entry[:entity], type: entity_entry[:type], mentions: mentions, summary: { total_mentions: mentions.size, segment_distribution: segment_dist } } end SentimentInsights::Export::Result.wrap({ entities: enriched_entities, responses: responses }) end |