]> git.openstreetmap.org Git - rails.git/blob - lib/spam_scorer.rb
Move spam scoring to own class
[rails.git] / lib / spam_scorer.rb
1 # frozen_string_literal: true
2
3 class SpamScorer
4   SPAMMY_PHRASES = [
5     "Business Description:", "Additional Keywords:"
6   ].freeze
7
8   def initialize(text)
9     @text = text
10   end
11
12   def score
13     link_count = 0
14     link_size = 0
15
16     doc = Nokogiri::HTML(text.to_html)
17
18     if doc.content.empty?
19       link_proportion = 0
20     else
21       doc.xpath("//a").each do |link|
22         link_count += 1
23         link_size += link.content.length
24       end
25
26       link_proportion = link_size.to_f / doc.content.length
27     end
28
29     spammy_phrases = SPAMMY_PHRASES.count do |phrase|
30       doc.content.include?(phrase)
31     end
32
33     ([link_proportion - 0.2, 0.0].max * 200) +
34       (link_count * 40) +
35       (spammy_phrases * 40)
36   end
37
38   private
39
40   attr_reader :text
41 end