Class: TWSS::Trainer

Inherits:
Object
  • Object
show all
Defined in:
lib/twss/trainer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, options = {}) ⇒ Trainer

Returns a new instance of Trainer.



9
10
11
12
13
# File 'lib/twss/trainer.rb', line 9

def initialize(engine, options = {})
  @engine = engine
  engine.clear_state!
  @training_percentage = options[:training_percentage] || 0.9
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



7
8
9
# File 'lib/twss/trainer.rb', line 7

def engine
  @engine
end

#training_percentageObject (readonly)

Returns the value of attribute training_percentage.



7
8
9
# File 'lib/twss/trainer.rb', line 7

def training_percentage
  @training_percentage
end

Instance Method Details

#run_tests(path) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/twss/trainer.rb', line 58

def run_tests(path)
  positive_test_file = File.join(path, 'test_twss.txt')
  negative_test_file = File.join(path, 'test_non_twss.txt')
  
  total_positive = total_documents(positive_test_file)
  total_negative = total_documents(negative_test_file)
  
  false_negatives = 0
  false_positives = 0
  total = 0
  correct = 0
  test_each(positive_test_file, (total_positive * training_percentage).to_i) do |line, result|
    if result
      correct += 1
    else
      false_negatives += 1
    end
    total += 1
  end

  test_each(negative_test_file, (total_negative * training_percentage).to_i) do |line, result|
    if !result
      correct += 1
    else
      false_positives += 1
    end
    total += 1
  end
  
  puts
  puts "Test set size: #{total}"
  puts "Overall accuracy: #{100 * correct / total.to_f}%"
  puts "False positives: #{false_positives} (#{100 * false_positives / total_negative.to_f}%)"
  puts "False negatives: #{false_negatives} (#{100 * false_negatives / total_positive.to_f}%)"
  puts
end

#run_training(path) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/twss/trainer.rb', line 34

def run_training(path)
  positive_file = File.join(path, 'twss.txt')
  negative_file = File.join(path, 'non_twss.txt')

  puts "Clearing state..."
  engine.clear_state!

  puts "Training NON-TWSS strings..."
  File.read(negative_file).each_line do |l|
    print '.'
    $stdout.flush
    engine.train(TWSS::Engine::FALSE, l)
  end
  puts

  puts "Training TWSS strings..."
  File.read(positive_file).each_line do |l|
    print '.'
    $stdout.flush
    engine.train(TWSS::Engine::TRUE, l)
  end
  puts      
end

#test_each(file, sample_size, &blk) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/twss/trainer.rb', line 95

def test_each(file, sample_size, &blk)
  i = 0
  File.read(file).each_line do |line|
    return if i > sample_size
    l = line.strip
    unless l.empty?
      r = TWSS(l)
      puts l + ' => ' + r.to_s
      blk.call(l, r)
      i += 1
    end
  end
end

#total_documents(file) ⇒ Object



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

def total_documents(file)
  t = 0
  File.read(file).each_line do |l|
    t += 1
  end
  t
end

#trainObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/twss/trainer.rb', line 15

def train
  path = File.join(File.dirname(__FILE__), '../../data/')

  run_training(path)

  puts "Writing to file..."
  engine.dump_classifier_to_file

  run_tests(path)
end