2023-02-23 10:25:00 -05:00
|
|
|
# frozen_string_literal: true
|
2023-03-14 15:03:50 -04:00
|
|
|
|
|
|
|
module DiscourseAi
|
2023-02-23 10:25:00 -05:00
|
|
|
module Sentiment
|
|
|
|
class EntryPoint
|
2023-02-23 13:50:10 -05:00
|
|
|
def inject_into(plugin)
|
|
|
|
sentiment_analysis_cb =
|
|
|
|
Proc.new do |post|
|
|
|
|
if SiteSetting.ai_sentiment_enabled
|
|
|
|
Jobs.enqueue(:post_sentiment_analysis, post_id: post.id)
|
|
|
|
end
|
|
|
|
end
|
2023-02-23 10:25:00 -05:00
|
|
|
|
2023-02-23 13:50:10 -05:00
|
|
|
plugin.on(:post_created, &sentiment_analysis_cb)
|
|
|
|
plugin.on(:post_edited, &sentiment_analysis_cb)
|
2023-11-08 08:50:37 -05:00
|
|
|
|
|
|
|
plugin.add_report("overall_sentiment") do |report|
|
|
|
|
report.modes = [:stacked_chart]
|
2024-11-04 07:14:34 -05:00
|
|
|
threshold = 0.6
|
2023-11-09 15:23:25 -05:00
|
|
|
|
|
|
|
sentiment_count_sql = Proc.new { |sentiment| <<~SQL }
|
|
|
|
COUNT(
|
2024-11-04 07:14:34 -05:00
|
|
|
CASE WHEN (cr.classification::jsonb->'#{sentiment}')::float > :threshold THEN 1 ELSE NULL END
|
2023-11-09 15:23:25 -05:00
|
|
|
) AS #{sentiment}_count
|
|
|
|
SQL
|
2023-11-08 08:50:37 -05:00
|
|
|
|
|
|
|
grouped_sentiments =
|
2023-11-09 15:23:25 -05:00
|
|
|
DB.query(
|
|
|
|
<<~SQL,
|
2023-11-28 23:17:46 -05:00
|
|
|
SELECT
|
2023-11-08 08:50:37 -05:00
|
|
|
DATE_TRUNC('day', p.created_at)::DATE AS posted_at,
|
2023-11-09 15:23:25 -05:00
|
|
|
#{sentiment_count_sql.call("positive")},
|
|
|
|
-#{sentiment_count_sql.call("negative")}
|
2023-11-28 23:17:46 -05:00
|
|
|
FROM
|
2023-11-08 08:50:37 -05:00
|
|
|
classification_results AS cr
|
|
|
|
INNER JOIN posts p ON p.id = cr.target_id AND cr.target_type = 'Post'
|
|
|
|
INNER JOIN topics t ON t.id = p.topic_id
|
|
|
|
INNER JOIN categories c ON c.id = t.category_id
|
|
|
|
WHERE
|
|
|
|
t.archetype = 'regular' AND
|
|
|
|
p.user_id > 0 AND
|
2024-11-04 07:14:34 -05:00
|
|
|
cr.model_used = 'cardiffnlp/twitter-roberta-base-sentiment-latest' AND
|
2023-11-08 08:50:37 -05:00
|
|
|
(p.created_at > :report_start AND p.created_at < :report_end)
|
|
|
|
GROUP BY DATE_TRUNC('day', p.created_at)
|
|
|
|
SQL
|
2023-11-09 15:23:25 -05:00
|
|
|
report_start: report.start_date,
|
|
|
|
report_end: report.end_date,
|
|
|
|
threshold: threshold,
|
|
|
|
)
|
2023-11-08 08:50:37 -05:00
|
|
|
|
|
|
|
data_points = %w[positive negative]
|
|
|
|
|
2023-11-09 15:23:25 -05:00
|
|
|
return report if grouped_sentiments.empty?
|
|
|
|
|
2023-11-08 08:50:37 -05:00
|
|
|
report.data =
|
|
|
|
data_points.map do |point|
|
|
|
|
{
|
|
|
|
req: "sentiment_#{point}",
|
2024-06-07 14:08:06 -04:00
|
|
|
color: point == "positive" ? report.colors[:lime] : report.colors[:purple],
|
2023-11-08 08:50:37 -05:00
|
|
|
label: I18n.t("discourse_ai.sentiment.reports.overall_sentiment.#{point}"),
|
|
|
|
data:
|
|
|
|
grouped_sentiments.map do |gs|
|
2023-11-09 15:23:25 -05:00
|
|
|
{ x: gs.posted_at, y: gs.public_send("#{point}_count") }
|
2023-11-08 08:50:37 -05:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin.add_report("post_emotion") do |report|
|
2024-08-02 17:23:29 -04:00
|
|
|
report.modes = [:stacked_line_chart]
|
2024-11-04 07:14:34 -05:00
|
|
|
threshold = 0.3
|
2023-11-09 15:23:25 -05:00
|
|
|
|
|
|
|
emotion_count_clause = Proc.new { |emotion| <<~SQL }
|
2024-08-02 17:23:29 -04:00
|
|
|
COUNT(
|
2024-11-04 07:14:34 -05:00
|
|
|
CASE WHEN (cr.classification::jsonb->'#{emotion}')::float > :threshold THEN 1 ELSE NULL END
|
2024-08-02 17:23:29 -04:00
|
|
|
) AS #{emotion}_count
|
|
|
|
SQL
|
2023-11-08 08:50:37 -05:00
|
|
|
|
|
|
|
grouped_emotions =
|
2023-11-09 15:23:25 -05:00
|
|
|
DB.query(
|
|
|
|
<<~SQL,
|
2024-08-02 17:23:29 -04:00
|
|
|
SELECT
|
|
|
|
DATE_TRUNC('day', p.created_at)::DATE AS posted_at,
|
|
|
|
#{emotion_count_clause.call("sadness")},
|
|
|
|
#{emotion_count_clause.call("surprise")},
|
|
|
|
#{emotion_count_clause.call("fear")},
|
|
|
|
#{emotion_count_clause.call("anger")},
|
|
|
|
#{emotion_count_clause.call("joy")},
|
|
|
|
#{emotion_count_clause.call("disgust")}
|
|
|
|
FROM
|
|
|
|
classification_results AS cr
|
|
|
|
INNER JOIN posts p ON p.id = cr.target_id AND cr.target_type = 'Post'
|
|
|
|
INNER JOIN users u ON p.user_id = u.id
|
|
|
|
INNER JOIN topics t ON t.id = p.topic_id
|
|
|
|
INNER JOIN categories c ON c.id = t.category_id
|
|
|
|
WHERE
|
|
|
|
t.archetype = 'regular' AND
|
|
|
|
p.user_id > 0 AND
|
2024-11-04 07:14:34 -05:00
|
|
|
cr.model_used = 'j-hartmann/emotion-english-distilroberta-base' AND
|
2024-08-02 17:23:29 -04:00
|
|
|
(p.created_at > :report_start AND p.created_at < :report_end)
|
|
|
|
GROUP BY DATE_TRUNC('day', p.created_at)
|
|
|
|
SQL
|
2023-11-09 15:23:25 -05:00
|
|
|
report_start: report.start_date,
|
|
|
|
report_end: report.end_date,
|
|
|
|
threshold: threshold,
|
|
|
|
)
|
2023-11-08 08:50:37 -05:00
|
|
|
|
2024-06-07 14:08:06 -04:00
|
|
|
return report if grouped_emotions.empty?
|
|
|
|
|
2024-08-02 17:23:29 -04:00
|
|
|
emotions = [
|
|
|
|
{ name: "sadness", color: report.colors[:turquoise] },
|
|
|
|
{ name: "disgust", color: report.colors[:lime] },
|
|
|
|
{ name: "fear", color: report.colors[:purple] },
|
|
|
|
{ name: "anger", color: report.colors[:magenta] },
|
|
|
|
{ name: "joy", color: report.colors[:yellow] },
|
|
|
|
{ name: "surprise", color: report.colors[:brown] },
|
|
|
|
]
|
2023-11-08 08:50:37 -05:00
|
|
|
|
|
|
|
report.data =
|
2024-08-02 17:23:29 -04:00
|
|
|
emotions.map do |emotion|
|
2023-11-08 08:50:37 -05:00
|
|
|
{
|
2024-08-02 17:23:29 -04:00
|
|
|
req: "emotion_#{emotion[:name]}",
|
|
|
|
color: emotion[:color],
|
|
|
|
label: I18n.t("discourse_ai.sentiment.reports.post_emotion.#{emotion[:name]}"),
|
2023-11-08 08:50:37 -05:00
|
|
|
data:
|
2024-08-02 17:23:29 -04:00
|
|
|
grouped_emotions.map do |ge|
|
|
|
|
{ x: ge.posted_at, y: ge.public_send("#{emotion[:name]}_count") }
|
2023-11-08 08:50:37 -05:00
|
|
|
end,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2023-02-23 10:25:00 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|