2023-11-28 10:28:36 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
desc "Backfill sentiment for all posts"
|
|
|
|
task "ai:sentiment:backfill", [:start_post] => [:environment] do |_, args|
|
|
|
|
public_categories = Category.where(read_restricted: false).pluck(:id)
|
|
|
|
|
|
|
|
Post
|
|
|
|
.joins("INNER JOIN topics ON topics.id = posts.topic_id")
|
2023-12-27 19:01:57 -05:00
|
|
|
.joins(
|
|
|
|
"LEFT JOIN classification_results ON classification_results.target_id = posts.id AND classification_results.target_type = 'Post'",
|
|
|
|
)
|
2023-11-28 10:28:36 -05:00
|
|
|
.where("classification_results.target_id IS NULL")
|
|
|
|
.where("posts.id >= ?", args[:start_post].to_i || 0)
|
|
|
|
.where("category_id IN (?)", public_categories)
|
|
|
|
.where(posts: { deleted_at: nil })
|
2024-03-12 20:01:24 -04:00
|
|
|
.where(topics: { deleted_at: nil })
|
2023-11-28 10:28:36 -05:00
|
|
|
.order("posts.id ASC")
|
|
|
|
.find_each do |post|
|
|
|
|
print "."
|
2023-12-27 19:01:57 -05:00
|
|
|
DiscourseAi::PostClassificator.new(
|
|
|
|
DiscourseAi::Sentiment::SentimentClassification.new,
|
|
|
|
).classify!(post)
|
2023-11-28 10:28:36 -05:00
|
|
|
end
|
|
|
|
end
|