Class: UncleKryon::Trainer

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tags = {}) ⇒ Trainer



36
37
38
39
40
41
42
43
# File 'lib/unclekryon/trainer.rb', line 36

def initialize(tags={})
  @max_tag_id_length = 0
  @max_tag_length = 0
  @tags = tags
  @trainer = NBayes::Base.new

  init_lengths
end

Instance Attribute Details

#max_tag_id_lengthObject

Returns the value of attribute max_tag_id_length.



20
21
22
# File 'lib/unclekryon/trainer.rb', line 20

def max_tag_id_length
  @max_tag_id_length
end

#max_tag_lengthObject

Returns the value of attribute max_tag_length.



21
22
23
# File 'lib/unclekryon/trainer.rb', line 21

def max_tag_length
  @max_tag_length
end

#tagsObject

Returns the value of attribute tags.



22
23
24
# File 'lib/unclekryon/trainer.rb', line 22

def tags
  @tags
end

#trainerObject

Returns the value of attribute trainer.



23
24
25
# File 'lib/unclekryon/trainer.rb', line 23

def trainer
  @trainer
end

Class Method Details

.to_tokens(text) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/unclekryon/trainer.rb', line 25

def self.to_tokens(text)
  tokens = []

  text.split(/[[:space:]]+/).each do |t|
    t.gsub!(/[[:punct:][:cntrl:]]+/,'')
    tokens.push(t) if !t.empty?
  end

  return tokens
end

Instance Method Details

#init_lengthsObject



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/unclekryon/trainer.rb', line 45

def init_lengths
  @max_tag_id_length = 0
  @max_tag_length = 0

  @tags.each do |id,tag|
    @max_tag_id_length = id.length if id.length > @max_tag_id_length
    @max_tag_length = tag.length if tag.length > @max_tag_length
  end

  @max_tag_id_length += 2 # Indention
  @max_tag_id_length = 7 if @max_tag_id_length < 7 # For "<Enter>" option
  @max_tag_length = -@max_tag_length # Left justify
end

#tag(text) ⇒ Object



99
100
101
# File 'lib/unclekryon/trainer.rb', line 99

def tag(text)
  return @trainer.classify(self.class.to_tokens(text)).max_class
end

#to_sObject



103
104
105
106
107
108
109
110
# File 'lib/unclekryon/trainer.rb', line 103

def to_s
  s = ''
  s << @trainer.to_yaml
  s << "\n"
  s << @trainer.data.category_stats

  return s
end

#train(text) ⇒ Object



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
94
95
96
97
# File 'lib/unclekryon/trainer.rb', line 59

def train(text)
  guess_tag = tag(text) # Try and guess
  tokens = self.class.to_tokens(text)

  puts '#################'
  puts '# Training Tags #'
  puts '#################'

  tf = '%%%is = %%%is' % [@max_tag_id_length,@max_tag_length]
  @tags.each do |id,tag|
    puts tf % [id,tag]
  end
  puts "<Enter> = Guess: #{guess_tag}"

  puts '-----------------'
  puts text
  puts '-----------------'
  print 'What is it? '

  # Use -t/--test option
  if DevOpts.instance.test?
    puts(tag_id = @tags.keys.sample) # For testing purposes
  else
    tag_id = $stdin.gets.chomp.strip # $stdin because app accepts args
  end
  puts

  if tag_id.empty?
    raise "Invalid guess tag[#{guess_tag}]" if !@tags.value?(guess_tag)
    tag = guess_tag
  else
    raise "Invalid tag ID[#{tag_id}]" if !@tags.include?(tag_id)
    tag = @tags[tag_id]
  end

  @trainer.train(tokens,tag)

  return tag
end