Class: Decidim::Ai::SpamDetection::Strategy::Bayes
- Defined in:
- decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb
Instance Attribute Summary
Attributes inherited from Base
Instance Method Summary collapse
- #classify(content) ⇒ Object
-
#initialize(options = {}) ⇒ Bayes
constructor
A new instance of Bayes.
- #log ⇒ Object
-
#score ⇒ Object
The Bayes strategy returns a score between that can be lower than -1 As per ClassifierReborn documentation, closest to 0 is being picked as the dominant category.
-
#untrain(category, content) ⇒ Object
Calling this method without any trained categories will throw an error.
Methods inherited from Base
Constructor Details
#initialize(options = {}) ⇒ Bayes
Returns a new instance of Bayes.
10 11 12 13 14 15 16 |
# File 'decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb', line 10 def initialize( = {}) super @options = { adapter: :memory, categories: [:ham, :spam], params: {} }.deep_merge() @available_categories = [:categories] @backend = ClassifierReborn::Bayes.new(*available_categories, backend: configured_backend) end |
Instance Method Details
#classify(content) ⇒ Object
33 34 35 36 |
# File 'decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb', line 33 def classify(content) @category, @internal_score = backend.classify_with_score(content) category end |
#log ⇒ Object
18 19 20 21 22 |
# File 'decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb', line 18 def log return unless category "The Classification engine marked this as #{category}" end |
#score ⇒ Object
The Bayes strategy returns a score between that can be lower than -1 As per ClassifierReborn documentation, closest to 0 is being picked as the dominant category
From original documentation:
Returns the scores in each category the provided +text+. E.g.,
b.classifications "I hate bad words and you"
=> {"Uninteresting"=>-12.6997928013932, "Interesting"=>-18.4206807439524}
The largest of these scores (the one closest to 0) is the one picked out by #classify
46 47 48 |
# File 'decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb', line 46 def score category.presence == "spam" ? 1 : 0 end |
#untrain(category, content) ⇒ Object
Calling this method without any trained categories will throw an error
25 26 27 28 29 |
# File 'decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb', line 25 def untrain(category, content) return unless backend.categories.collect(&:downcase).collect(&:to_sym).include?(category) backend.untrain(category, content) end |