Class: Bogofilter

Inherits:
Object
  • Object
show all
Defined in:
lib/bogofilter.rb,
lib/bogofilter/version.rb

Defined Under Namespace

Classes: Error

Constant Summary collapse

VERSION =
"0.1.0"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dbpath:, verbosity: 0) ⇒ Bogofilter

Returns a new instance of Bogofilter.

Raises:



40
41
42
43
44
45
# File 'lib/bogofilter.rb', line 40

def initialize(dbpath:, verbosity: 0)
  @dbpath = dbpath
  @verbosity = verbosity

  raise Error.new('verbosity can be between 0-4') if !(0..4).cover?(verbosity)
end

Class Method Details

.configurationObject



10
11
12
# File 'lib/bogofilter.rb', line 10

def self.configuration
  run(arguments: ["-Q"], verbosity: 0)[0]
end

.run(arguments: [], stdin: nil, verbosity:) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/bogofilter.rb', line 25

def self.run(arguments: [], stdin: nil, verbosity:)
  verbosity_args = verbosity > 0 ? ["-D", "-#{'v' * verbosity}"] : []
  command = Shellwords.join(["bogofilter", "-C", "-e", *verbosity_args, *arguments])

  stdout, status = Open3.capture2(command, stdin_data: stdin)

  if verbosity > 0
    $stdout.puts stdout
  end

  raise Error.new("command failed with #{status}") if status != 0

  [stdout, status]
end

.versionObject



14
15
16
17
18
19
20
21
22
23
# File 'lib/bogofilter.rb', line 14

def self.version
  result = run(arguments: ["-V"], verbosity: 0)[0]
  version_match = result.match(%r{bogofilter version (\d+\.\d+\.\d+)})

  if version_match
    version_match[1]
  else
    raise Error.new('cannot detect bogofilter version')
  end
end

Instance Method Details

#add_ham(text) ⇒ Object



64
65
66
67
# File 'lib/bogofilter.rb', line 64

def add_ham(text)
  _stdout, status = run(arguments: ["-n"], stdin: text)
  status == 0
end

#add_spam(text) ⇒ Object



54
55
56
57
# File 'lib/bogofilter.rb', line 54

def add_spam(text)
  _stdout, status = run(arguments: ["-s"], stdin: text)
  status == 0
end

#classify(text) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bogofilter.rb', line 74

def classify(text)
  stdout, _status = run(arguments: ["-T"], stdin: text)
  classification, score = stdout.split(" ", 2)
  mapping = {
    "H" => :ham,
    "S" => :spam,
    "U" => :unsure,
  }

  {
    result: mapping[classification],
    score: score.to_f,
  }
end

#is_ham?(text) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/bogofilter.rb', line 95

def is_ham?(text)
  classification = classify(text)
  return nil if classification[:result] == :unsure
  classification[:result] == :ham
end

#is_spam?(text) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
92
93
# File 'lib/bogofilter.rb', line 89

def is_spam?(text)
  classification = classify(text)
  return nil if classification[:result] == :unsure
  classification[:result] == :spam
end

#remove_ham(text) ⇒ Object



69
70
71
72
# File 'lib/bogofilter.rb', line 69

def remove_ham(text)
  _stdout, status = run(arguments: ["-N"], stdin: text)
  status == 0
end

#remove_spam(text) ⇒ Object



59
60
61
62
# File 'lib/bogofilter.rb', line 59

def remove_spam(text)
  _stdout, status = run(arguments: ["-S"], stdin: text)
  status == 0
end

#run(arguments: [], stdin: nil) ⇒ Object



47
48
49
50
51
52
# File 'lib/bogofilter.rb', line 47

def run(arguments: [], stdin: nil)
  self.class.run(arguments: ["-d", @dbpath, *arguments],
    stdin: stdin,
    verbosity: @verbosity,
  )
end