Skip to content

Commit

Permalink
Upgrade for analyze_array (#10)
Browse files Browse the repository at this point in the history
* fix format of the output

* fix github action

* symbolize the key of the output

* add how to use analyze array in README

* bump version
  • Loading branch information
BlazingRockStorm authored Nov 11, 2024
1 parent 4b3b5dd commit 3e56751
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 14 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/gem-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ name: Ruby Gem
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [0.0.4.beta] - 2024-11-11
## [0.0.5.beta] - 2024-11-11

- Bug fix [@BlazingRockStorm](https://github.com/BlazingRockStorm)

Expand Down
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
sentiment-ai (0.0.4.beta)
sentiment-ai (0.0.5.beta)

GEM
remote: https://rubygems.org/
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ After calling the model, use:
sentiment.analyze_sentence('I Love Ruby')
# => "Sentiment: Positive"
```
For analyzing an array of sentence, use:
```ruby
array = ['I Love Ruby', "I use Ruby"]
sentiment.analyze_array(array)
# => [{ :sentence => 'I Love Ruby', :sentiment => 'positive' }, { :sentence => "I use Ruby", :sentiment => 'neutral' }]
```
By default, the language used for the gem is English. If you want to use the other languages, Japanese for example, use:
```ruby
sentiment = SentimentAI.new(:open_ai, OPEN_AI_KEY, :ja)
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
en:
prompt:
sentence: "Analyze the sentiment of the sentence given below.\n%{sentence}\nThe output should be in the format- Semtiment: Value"
array: "Analyze the sentiment of all sentences given below.\n%{array}\nThe output should be in the format- [{\"Sentence\" => \"Value\"}]"
array: "Analyze the sentiment of all sentences given below.\n%{array}\nThe output should be in the format- [{sentence => value, sentiment => value}]"
2 changes: 1 addition & 1 deletion config/locales/ja.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ja:
prompt:
sentence: "テキストを中立、否定的、または肯定的に分類してください。 テキスト:%{sentence}\n結果の形式- 所感:値"
array: "テキスト配列を中立、否定的、または肯定的に分類してください。 \n%{array}\n結果の形式- [{\"Câu\" => \"Gía trị bằng tiếng Anh\"}]"
array: "テキスト配列を中立、否定的、または肯定的に分類してください。 \n%{array}\n結果の形式- [{sentence => value, sentiment => value}]"
2 changes: 1 addition & 1 deletion config/locales/vi.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vi:
prompt:
sentence: "Phân tích sắc thái của câu dưới đây là tích cực, tiêu cực hay trung lập.\n%{sentence}\nKết quả trả về dưới dạng- Sắc thái: Gía trị"
array: "Phân tích sắc thái của các câu dưới đây là tích cực, tiêu cực hay trung lập.\n%{array}\nKết quả trả về dưới dạng- [{\"Câu\" => \"Gía trị bằng tiếng Anh\"}]"
array: "Phân tích sắc thái của các câu dưới đây là tích cực, tiêu cực hay trung lập.\n%{array}\nKết quả trả về dưới dạng- [{sentence => value, sentiment => value}]"
4 changes: 3 additions & 1 deletion lib/sentiment_ai.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def analyze_sentence(sentence)
end

def analyze_array(array)
JSON.parse(@generative_ai.analyze_array(array).gsub('=>', ':'))
JSON.parse(@generative_ai.analyze_array(array).gsub('=>', ':')).map do |hash|
hash.map { |key, value| [key.to_sym, value] }.to_h
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/sentiment_ai/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module SentimentAI
VERSION = '0.0.4.beta'
VERSION = '0.0.5.beta'
end
3 changes: 2 additions & 1 deletion sentiment-ai.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ Gem::Specification.new do |spec|
spec.description = 'A gem that use Generative AI for data sentiment analysis'
spec.authors = ['Quan Hoang']
spec.email = '[email protected]'
spec.files = Dir['lib/sentiment_ai.rb', 'lib/sentiment_ai/*.rb', 'lib/sentiment_ai/core/*.rb', 'config/locales/*.yml']
spec.files = Dir['lib/sentiment_ai.rb', 'lib/sentiment_ai/*.rb', 'lib/sentiment_ai/core/*.rb',
'config/locales/*.yml']
spec.homepage = 'https://rubygems.org/gems/sentiment-ai'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.1.0'
Expand Down
8 changes: 6 additions & 2 deletions spec/ai_models/gemini_pro_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
end

describe '#analyze_array' do
let(:array) {['Delicious food','Too noisy!!!',"I really don't know how to feel about Pokemon"]}
let(:result_array) {[{"Sentence" => "Delicious food", "Value" => "Positive"}, {"Sentence" => "Too noisy!!!", "Value" => "Negative"}, {"Sentence" => "I really don't know how to feel about Pokemon", "Value" => "Neutral"}]}
let(:array) { ['Delicious food', 'Too noisy!!!', "I really don't know how to feel about Pokemon"] }
let(:result_array) do
[{ :sentence => 'Delicious food', :sentiment => 'positive' },
{ :sentence => 'Too noisy!!!', :sentiment => 'negative' },
{ :sentence => "I really don't know how to feel about Pokemon", :sentiment => 'neutral' }]
end

it 'return the sentiments of all sentences in the array' do
expect(sentiment.analyze_array(array)).to eq(result_array)
Expand Down
8 changes: 6 additions & 2 deletions spec/ai_models/openai_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,12 @@
end

describe '#analyze_array' do
let(:array) {['Delicious food','Too noisy!!!',"I really don't know how to feel about Pokemon"]}
let(:result_array) {[{"Sentence" => "Delicious food", "Value" => "Positive"}, {"Sentence" => "Too noisy!!!", "Value" => ..."Negative"}, {"Sentence" => "I really don't know how to feel about Pokemon", "Value" => "Neutral"}]}
let(:array) { ['Delicious food', 'Too noisy!!!', "I really don't know how to feel about Pokemon"] }
let(:result_array) do
[{ :sentence => 'Delicious food', :sentiment => 'positive' },
{ :sentence => 'Too noisy!!!', :sentiment => 'negative' },
{ :sentence => "I really don't know how to feel about Pokemon", :sentiment => 'neutral' }]
end

it 'return the sentiments of all sentences in the array' do
expect(sentiment.analyze_array(array)).to eq(result_array)
Expand Down

0 comments on commit 3e56751

Please sign in to comment.