Skip to content

Commit 511b102

Browse files
authored
DEV: rake task to populate sentiment in dev env (#1176)
1 parent 94e0834 commit 511b102

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# frozen_string_literal: true
2+
3+
desc "Creates sample sentiment / emotion data"
4+
task "ai:sentiment:populate", [:start_post] => [:environment] do |_, args|
5+
raise "Don't run this task in production!" if Rails.env.production?
6+
7+
Post
8+
.joins(<<~SQL)
9+
LEFT JOIN classification_results ON
10+
posts.id = classification_results.target_id AND
11+
classification_results.target_type = 'Post' AND
12+
model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest'
13+
SQL
14+
.where("classification_results.id IS NULL")
15+
.where("posts.id > ?", args[:start_post].to_i || 0)
16+
.find_each do |post|
17+
positive = rand(0.0..1.0)
18+
negative = rand(0.0..(1.0 - positive))
19+
neutral = 1 - positive - negative
20+
21+
ClassificationResult.create!(
22+
target_id: post.id,
23+
model_used: "cardiffnlp/twitter-roberta-base-sentiment-latest",
24+
classification_type: "sentiment",
25+
target_type: "Post",
26+
classification: {
27+
neutral: neutral,
28+
positive: positive,
29+
negative: negative,
30+
},
31+
)
32+
end
33+
34+
Post
35+
.joins(<<~SQL)
36+
LEFT JOIN classification_results ON
37+
posts.id = classification_results.target_id AND
38+
classification_results.target_type = 'Post' AND
39+
classification_results.model_used = 'SamLowe/roberta-base-go_emotions'
40+
SQL
41+
.where("classification_results.id IS NULL")
42+
.where("posts.id > ?", args[:start_post].to_i || 0)
43+
.find_each do |post|
44+
emotions =
45+
DiscourseAi::Sentiment::Emotions::LIST
46+
.shuffle
47+
.reduce({}) do |acc, emotion|
48+
current_sum = acc.values.sum
49+
acc.merge(emotion => rand(0.0..(1.0 - current_sum)))
50+
end
51+
52+
emotions["neutral"] = 1 - (emotions.values.sum - emotions["neutral"])
53+
54+
ClassificationResult.create!(
55+
target_id: post.id,
56+
model_used: "SamLowe/roberta-base-go_emotions",
57+
classification_type: "sentiment",
58+
target_type: "Post",
59+
classification: emotions,
60+
)
61+
end
62+
end

0 commit comments

Comments
 (0)