Class: Trainer
- Inherits:
-
Object
- Object
- Trainer
- Defined in:
- lib/trainer.rb
Instance Attribute Summary collapse
-
#classifier ⇒ Object
readonly
Returns the value of attribute classifier.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(sources_dir, output_dir) ⇒ Trainer
constructor
A new instance of Trainer.
- #train(sources_dir, output_dir) ⇒ Object
Constructor Details
#initialize(sources_dir, output_dir) ⇒ Trainer
Returns a new instance of Trainer.
9 10 11 |
# File 'lib/trainer.rb', line 9 def initialize(sources_dir, output_dir) @classifier = train(sources_dir,output_dir) end |
Instance Attribute Details
#classifier ⇒ Object (readonly)
Returns the value of attribute classifier.
7 8 9 |
# File 'lib/trainer.rb', line 7 def classifier @classifier end |
Class Method Details
.files_for(sources_dir, language) ⇒ Object
36 37 38 |
# File 'lib/trainer.rb', line 36 def self.files_for(sources_dir,language) files = Dir["#{sources_dir}/#{language}/*"] end |
.languages_for(sources_dir) ⇒ Object
32 33 34 |
# File 'lib/trainer.rb', line 32 def self.languages_for(sources_dir) languages = Dir["#{sources_dir}/*"].map {|dir| dir.split("/").last} end |
Instance Method Details
#train(sources_dir, output_dir) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/trainer.rb', line 13 def train(sources_dir,output_dir) b = Classifier::Bayes.new languages = Trainer.languages_for(sources_dir) languages.each do |language| b.add_category language files = Trainer.files_for(sources_dir,language) files.each do |f| file_content = File.read(f) b.train language, file_content end end marshal(output_dir,b) return b end |