Class: Decidim::Ai::SpamDetection::Strategy::Bayes

Inherits:
Base
  • Object
show all
Defined in:
decidim-ai/lib/decidim/ai/spam_detection/strategy/bayes.rb

Instance Attribute Summary

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#train

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(options = {})
  super
  @options = { adapter: :memory, categories: [:ham, :spam], params: {} }.deep_merge(options)

  @available_categories = options[: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

#logObject



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

#scoreObject

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