Class: TWSS::Engine

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/twss/engine.rb

Constant Summary collapse

DATA_FILE =
File.join(File.dirname(__FILE__), '../../data/classifier')
TRUE =
'1'
FALSE =
'0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Engine

Returns a new instance of Engine.



18
19
20
21
22
# File 'lib/twss/engine.rb', line 18

def initialize(options = {})
  @data_file = options[:data_file] || DATA_FILE
  @threshold ||= options[:threshold] || 7.0
  @classifier = load_classifier_from_file!(@data_file) || new_classifier
end

Instance Attribute Details

#thresholdObject

Returns the value of attribute threshold.



16
17
18
# File 'lib/twss/engine.rb', line 16

def threshold
  @threshold
end

Instance Method Details

#classify(str) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/twss/engine.rb', line 24

def classify(str)
  if basic_conditions_met?(str)
    c = @classifier.classifications(str)
    require 'pp'
    c[TRUE] - c[FALSE] > threshold
  else
    false
  end
end

#clear_state!Object

Clears out the current classifier instance and nukes the data file



42
43
44
45
# File 'lib/twss/engine.rb', line 42

def clear_state!
  File.delete(@data_file) if File.exists?(@data_file)
  @classifier = new_classifier
end

#dump_classifier_to_file(f = @data_file) ⇒ Object

Dumps the current classifier state to specified path



35
36
37
38
39
# File 'lib/twss/engine.rb', line 35

def dump_classifier_to_file(f = @data_file)
  o = File.open(f, 'w')
  o.write(Marshal.dump(@classifier))
  o.close
end