Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 4dbf193

Browse files
committed
fix more tests
1 parent 5ac3f09 commit 4dbf193

File tree

5 files changed

+95
-26
lines changed

5 files changed

+95
-26
lines changed

lib/sentiment/sentiment_classification.rb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ def request(target_to_classify)
3636
end
3737
end
3838

39+
def transform_result(result)
40+
hash_result = {}
41+
result.each { |r| hash_result[r[:label]] = r[:score] }
42+
hash_result
43+
end
44+
3945
private
4046

4147
def request_with(content, model_config)
4248
result = ::DiscourseAi::Inference::HuggingFaceTextEmbeddings.classify(content, model_config)
43-
hash_result = {}
44-
result.each { |r| hash_result[r[:label]] = r[:score] }
45-
hash_result
49+
transform_result(result)
4650
end
4751

4852
def content_of(target_to_classify)

spec/fabricators/classification_result_fabricator.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
end
77

88
Fabricator(:sentiment_classification, from: :classification_result) do
9-
model_used "sentiment"
10-
classification { { negative: 72, neutral: 23, positive: 4 } }
9+
model_used "cardiffnlp/twitter-roberta-base-sentiment-latest"
10+
classification { { negative: 0.72, neutral: 0.23, positive: 0.4 } }
1111
end
1212

1313
Fabricator(:emotion_classification, from: :classification_result) do
14-
model_used "emotion"
15-
classification { { negative: 72, neutral: 23, positive: 4 } }
14+
model_used "j-hartmann/emotion-english-distilroberta-base"
15+
classification do
16+
{ sadness: 0.72, surprise: 0.23, fear: 0.4, anger: 0.87, joy: 0.22, disgust: 0.70 }
17+
end
1618
end

spec/lib/modules/sentiment/entry_point_spec.rb

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@
6464
fab!(:post_2) { Fabricate(:post) }
6565

6666
describe "overall_sentiment report" do
67-
let(:positive_classification) { { negative: 2, neutral: 30, positive: 70 } }
68-
let(:negative_classification) { { negative: 65, neutral: 2, positive: 10 } }
67+
let(:positive_classification) { { negative: 0.2, neutral: 0.3, positive: 0.7 } }
68+
let(:negative_classification) { { negative: 0.65, neutral: 0.2, positive: 0.1 } }
6969

7070
def sentiment_classification(post, classification)
7171
Fabricate(:sentiment_classification, target: post, classification: classification)
@@ -87,12 +87,28 @@ def sentiment_classification(post, classification)
8787

8888
describe "post_emotion report" do
8989
let(:emotion_1) do
90-
{ sadness: 49, surprise: 23, neutral: 6, fear: 34, anger: 87, joy: 22, disgust: 70 }
90+
{
91+
sadness: 0.49,
92+
surprise: 0.23,
93+
neutral: 0.6,
94+
fear: 0.34,
95+
anger: 0.87,
96+
joy: 0.22,
97+
disgust: 0.70,
98+
}
9199
end
92100
let(:emotion_2) do
93-
{ sadness: 19, surprise: 63, neutral: 45, fear: 44, anger: 27, joy: 62, disgust: 30 }
101+
{
102+
sadness: 0.19,
103+
surprise: 0.63,
104+
neutral: 0.45,
105+
fear: 0.44,
106+
anger: 0.27,
107+
joy: 0.62,
108+
disgust: 0.30,
109+
}
94110
end
95-
let(:model_used) { "emotion" }
111+
let(:model_used) { "j-hartmann/emotion-english-distilroberta-base" }
96112

97113
def emotion_classification(post, classification)
98114
Fabricate(
@@ -109,7 +125,7 @@ def strip_emoji_and_downcase(str)
109125
end
110126

111127
it "calculate averages using only public posts" do
112-
threshold = 30
128+
threshold = 0.30
113129

114130
emotion_classification(post_1, emotion_1)
115131
emotion_classification(post_2, emotion_2)

spec/lib/modules/sentiment/sentiment_classification_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616

1717
result = subject.request(target)
1818

19-
subject.available_models.each do |model|
20-
expect(result[model]).to eq(SentimentInferenceStubs.model_response(model))
19+
subject.available_classifiers.each do |model_config|
20+
expect(result[model_config.model_name]).to eq(
21+
subject.transform_result(SentimentInferenceStubs.model_response(model_config.model_name)),
22+
)
2123
end
2224
end
2325
end

spec/support/sentiment_inference_stubs.rb

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,69 @@
22

33
class SentimentInferenceStubs
44
class << self
5-
def endpoint
6-
"http://test.com/api/v1/classify"
7-
end
8-
95
def model_response(model)
10-
{ negative: 72, neutral: 23, positive: 4 } if model == "sentiment"
11-
12-
{ sadness: 99, surprise: 0, neutral: 0, fear: 0, anger: 0, joy: 0, disgust: 0 }
6+
case model
7+
when "SamLowe/roberta-base-go_emotions"
8+
[
9+
{ score: 0.90261286, label: "anger" },
10+
{ score: 0.04127813, label: "annoyance" },
11+
{ score: 0.03183503, label: "neutral" },
12+
{ score: 0.005037033, label: "disgust" },
13+
{ score: 0.0031153716, label: "disapproval" },
14+
{ score: 0.0019118421, label: "disappointment" },
15+
{ score: 0.0015849728, label: "sadness" },
16+
{ score: 0.0012343781, label: "curiosity" },
17+
{ score: 0.0010682651, label: "amusement" },
18+
{ score: 0.00100747, label: "confusion" },
19+
{ score: 0.0010035422, label: "admiration" },
20+
{ score: 0.0009957326, label: "approval" },
21+
{ score: 0.0009726665, label: "surprise" },
22+
{ score: 0.0007754773, label: "realization" },
23+
{ score: 0.0006978541, label: "love" },
24+
{ score: 0.00064793555, label: "fear" },
25+
{ score: 0.0006454095, label: "optimism" },
26+
{ score: 0.0005969062, label: "joy" },
27+
{ score: 0.0005498958, label: "embarrassment" },
28+
{ score: 0.00050068577, label: "excitement" },
29+
{ score: 0.00047403979, label: "caring" },
30+
{ score: 0.00038841428, label: "gratitude" },
31+
{ score: 0.00034546282, label: "desire" },
32+
{ score: 0.00023012784, label: "grief" },
33+
{ score: 0.00018133638, label: "remorse" },
34+
{ score: 0.00012511834, label: "nervousness" },
35+
{ score: 0.00012079607, label: "pride" },
36+
{ score: 0.000063159685, label: "relief" },
37+
]
38+
when "cardiffnlp/twitter-roberta-base-sentiment-latest"
39+
[
40+
{ score: 0.627579, label: "negative" },
41+
{ score: 0.29482335, label: "neutral" },
42+
{ score: 0.07759768, label: "positive" },
43+
]
44+
when "j-hartmann/emotion-english-distilroberta-base"
45+
[
46+
{ score: 0.7033674, label: "anger" },
47+
{ score: 0.2701151, label: "disgust" },
48+
{ score: 0.009492096, label: "sadness" },
49+
{ score: 0.0080775, label: "neutral" },
50+
{ score: 0.0049473303, label: "fear" },
51+
{ score: 0.0023369535, label: "surprise" },
52+
{ score: 0.001663634, label: "joy" },
53+
]
54+
end
1355
end
1456

1557
def stub_classification(post)
1658
content = post.post_number == 1 ? "#{post.topic.title}\n#{post.raw}" : post.raw
1759

18-
DiscourseAi::Sentiment::SentimentClassification.new.available_models.each do |model|
60+
DiscourseAi::Sentiment::SentimentClassification
61+
.new
62+
.available_classifiers
63+
.each do |model_config|
1964
WebMock
20-
.stub_request(:post, endpoint)
21-
.with(body: JSON.dump(model: model, content: content))
22-
.to_return(status: 200, body: JSON.dump(model_response(model)))
65+
.stub_request(:post, model_config.endpoint)
66+
.with(body: JSON.dump(inputs: content, truncate: true))
67+
.to_return(status: 200, body: JSON.dump(model_response(model_config.model_name)))
2368
end
2469
end
2570
end

0 commit comments

Comments
 (0)