Class: BabyFace::Stand
- Inherits:
-
Object
- Object
- BabyFace::Stand
- Defined in:
- lib/baby_face/stand.rb
Instance Method Summary collapse
-
#initialize(mod) ⇒ Stand
constructor
A new instance of Stand.
- #save ⇒ Object
- #scan(prefix, obj) ⇒ Object
- #to_feature ⇒ Object
- #wakachi(text) ⇒ Object
Constructor Details
#initialize(mod) ⇒ Stand
Returns a new instance of Stand.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/baby_face/stand.rb', line 6 def initialize(mod) @mod = mod @categories = mod.class.class_variable_get(:@@_categories) @categories.each do |category| self.class.class_eval do define_method("#{category}?") { bayes.classify(to_feature).gsub(" ", "_").downcase == category.to_s } define_method("train_#{category}") { bayes.train(category, to_feature) } end end @tokenizer = mod.class.class_variable_get(:@@_tokenizer) end |
Instance Method Details
#save ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/baby_face/stand.rb', line 52 def save bayes # Memoization (first access is in transaction) pstore.transaction do pstore[@mod.class.name] = bayes pstore.commit end end |
#scan(prefix, obj) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/baby_face/stand.rb', line 24 def scan(prefix, obj) obj.class.class_variable_get(:@@_features).map do |attr| _prefix = prefix.nil? ? attr : "#{prefix}_#{attr}" value = obj.send(attr) if value.is_a? BabyFace scan(_prefix, value) elsif value.is_a? Array value.map do |val| scan(_prefix, val) end elsif value.is_a? Hash value.map do |key, val| scan("#{_prefix}_#{key}", val) end else wakachi(value.to_s).map do |text| "#{_prefix}_#{text}" end end end end |
#to_feature ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/baby_face/stand.rb', line 23 def to_feature def scan(prefix, obj) obj.class.class_variable_get(:@@_features).map do |attr| _prefix = prefix.nil? ? attr : "#{prefix}_#{attr}" value = obj.send(attr) if value.is_a? BabyFace scan(_prefix, value) elsif value.is_a? Array value.map do |val| scan(_prefix, val) end elsif value.is_a? Hash value.map do |key, val| scan("#{_prefix}_#{key}", val) end else wakachi(value.to_s).map do |text| "#{_prefix}_#{text}" end end end end scan(nil, @mod).flatten.join(" ") end |
#wakachi(text) ⇒ Object
48 49 50 |
# File 'lib/baby_face/stand.rb', line 48 def wakachi(text) @tokenizer ? @tokenizer.call(text) : text.split end |