Class: Splam::Rules::WordLength

Inherits:
Splam::Rule show all
Defined in:
lib/splam/rules/word_length.rb

Instance Attribute Summary

Attributes inherited from Splam::Rule

#body, #reasons, #score, #suite, #weight

Instance Method Summary collapse

Methods inherited from Splam::Rule

#add_score, inherited, #initialize, #line_safe?, #name, run

Constructor Details

This class inherits a constructor from Splam::Rule

Instance Method Details

#average(arr) ⇒ Object



8
9
10
# File 'lib/splam/rules/word_length.rb', line 8

def average(arr)
  sum(arr) / arr.size
end

#median(arr) ⇒ Object



12
13
14
15
# File 'lib/splam/rules/word_length.rb', line 12

def median(arr)
  a2 = arr.sort
  a2[arr.size / 2] unless arr.empty?
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/splam/rules/word_length.rb', line 17

def run
  words = []
  words = @body.split(/\s/)
  words.delete_if { |w| w =~ /^https?\:\/\// }
  words.collect! { |word| word.size }

  # Only count word lengths over 10
  if words.size > 5
    add_score 5, "Average word length over 5"  if average(words) > 5
    add_score 10, "Average word length over 10" if average(words) > 10
    add_score 5, "Median word length over 5"   if median(words) > 5
    add_score 10, "Median word length over 10"  if median(words) > 10
  end
end

#sum(arr) ⇒ Object

Basic array functions



4
5
6
# File 'lib/splam/rules/word_length.rb', line 4

def sum(arr)
  arr.inject  {|sum,x| sum + x }
end