Class: SentimentAI::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sentiment_ai.rb

Instance Method Summary collapse

Constructor Details

#initialize(provider, api_key, language = :en) ⇒ Base

Returns a new instance of Base.



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sentiment_ai.rb', line 23

def initialize(provider, api_key, language = :en)
  I18n.locale = language
  @generative_ai = case provider
                   when :open_ai
                     Core::OpenAIDriver.new(api_key)
                   when :gemini_ai_pro
                     Core::GeminiDriver.new(api_key)
                   when :anthropic
                     Core::AnthropicDriver.new(api_key)
                   else
                     raise ArgumentError
                   end
end

Instance Method Details

#analyze_array(array) ⇒ Object



47
48
49
# File 'lib/sentiment_ai.rb', line 47

def analyze_array(array)
  array.map { |sentence| analyze_sentence(sentence) }
end

#analyze_csv(input_csv_path, sentence_column, output_directory) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sentiment_ai.rb', line 51

def analyze_csv(input_csv_path, sentence_column, output_directory)
  csv_data = CSV.read(input_csv_path, headers: true)
  unless csv_data.headers.include?(sentence_column)
    raise ArgumentError,
          "#{sentence_column} column not found in CSV file"
  end

  File.open("#{output_directory}/output.csv", 'w') do |file|
    file.write("sentence,sentiment\n")
    csv_data.each do |row|
      result = analyze_sentence(row[sentence_column])
      file.write "#{result[:sentence]},#{result[:sentiment]}\n"
    end
  end
end

#analyze_sentence(sentence) ⇒ Object



37
38
39
40
# File 'lib/sentiment_ai.rb', line 37

def analyze_sentence(sentence)
  sentiment = @generative_ai.analyze_sentence(sentence)
  { sentence: sentence, sentiment: sentiment }
end

#positive_check(sentence) ⇒ Object



42
43
44
45
# File 'lib/sentiment_ai.rb', line 42

def positive_check(sentence)
  sentiment_bool = @generative_ai.positive_check(sentence)
  { sentence: sentence, positive: sentiment_bool == 'true' }
end