Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 1.59 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How do I debug an undefined method for nil class error?
  2. require 'classifier'
  3.  
  4. classifications = '1007.09', '1006.03'
  5. traindata = Hash["1007.09" => "ADAPTER- SCREENING FOR VALVES VBS", "1006.03" => "ACTUATOR- LINEAR"]
  6.  
  7. b = Classifier::Bayes.new classifications
  8.  
  9. traindata.each do |key, value|  
  10. b.train(key, value)
  11. end
  12.        
  13. Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
  14. c:/Ruby192/lib/ruby/gems/1.9.1/gems/classifier-1.3.3/lib/classifier/bayes.rb:27:in `block in train': undefined method `[]' for nil:NilClass (NoMethodError)
  15.   from c:/Ruby192/lib/ruby/gems/1.9.1/gems/classifier-1.3.3/lib/classifier/bayes.rb:26:in `each'
  16.   from c:/Ruby192/lib/ruby/gems/1.9.1/gems/classifier-1.3.3/lib/classifier/bayes.rb:26:in `train'
  17.   from C:/_Chris/Code/classifier/smdclasser.rb:13:in `block in <main>'
  18.   from C:/_Chris/Code/classifier/smdclasser.rb:11:in `each'
  19.   from C:/_Chris/Code/classifier/smdclasser.rb:11:in `<main>'
  20.        
  21. # Provides a general training method for all categories specified in Bayes#new
  22. # For example:
  23. #     b = Classifier::Bayes.new 'This', 'That', 'the_other'
  24. #     b.train :this, "This text"
  25. #     b.train "that", "That text"
  26. #     b.train "The other", "The other text"
  27. def train(category, text)
  28.   category = category.prepare_category_name
  29.   text.word_hash.each do |word, count|
  30.     @categories[category][word]     ||=     0
  31.     @categories[category][word]      +=     count
  32.     @total_words += count
  33.   end
  34. end
  35.        
  36. b = Classifier::Bayes.new 'This', 'That', 'the_other'
  37.        
  38. b = Classifier::Bayes.new ['This', 'That', 'the_other']
  39.        
  40. b = Classifier::Bayes.new *classifications