2023-02-24 11:25:02 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-03-14 15:03:50 -04:00
|
|
|
module DiscourseAi
|
2023-02-24 11:25:02 -05:00
|
|
|
module Sentiment
|
|
|
|
class SentimentClassification
|
|
|
|
def type
|
|
|
|
:sentiment
|
|
|
|
end
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
def available_classifiers
|
|
|
|
DiscourseAi::Sentiment::SentimentSiteSettingJsonSchema.values
|
2023-02-24 11:25:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_classify?(target)
|
|
|
|
content_of(target).present?
|
|
|
|
end
|
|
|
|
|
2023-03-07 13:39:28 -05:00
|
|
|
def get_verdicts(_)
|
2024-11-04 07:14:34 -05:00
|
|
|
available_classifiers.reduce({}) do |memo, model|
|
|
|
|
memo[model.model_name] = false
|
2023-03-07 13:39:28 -05:00
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def should_flag_based_on?(_verdicts)
|
2023-02-24 11:25:02 -05:00
|
|
|
# We don't flag based on sentiment classification.
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(target_to_classify)
|
2023-12-27 19:01:57 -05:00
|
|
|
target_content = content_of(target_to_classify)
|
2023-02-24 11:25:02 -05:00
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
available_classifiers.reduce({}) do |memo, model|
|
|
|
|
memo[model.model_name] = request_with(target_content, model)
|
2023-02-24 11:25:02 -05:00
|
|
|
memo
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
def transform_result(result)
|
|
|
|
hash_result = {}
|
|
|
|
result.each { |r| hash_result[r[:label]] = r[:score] }
|
|
|
|
hash_result
|
|
|
|
end
|
|
|
|
|
2023-02-24 11:25:02 -05:00
|
|
|
private
|
|
|
|
|
2024-11-04 07:14:34 -05:00
|
|
|
def request_with(content, model_config)
|
|
|
|
result = ::DiscourseAi::Inference::HuggingFaceTextEmbeddings.classify(content, model_config)
|
|
|
|
transform_result(result)
|
2023-02-24 11:25:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def content_of(target_to_classify)
|
2024-01-17 13:17:58 -05:00
|
|
|
content =
|
|
|
|
if target_to_classify.post_number == 1
|
|
|
|
"#{target_to_classify.topic.title}\n#{target_to_classify.raw}"
|
|
|
|
else
|
|
|
|
target_to_classify.raw
|
|
|
|
end
|
|
|
|
|
|
|
|
Tokenizer::BertTokenizer.truncate(content, 512)
|
2023-02-24 11:25:02 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|