我正在嘗試使用cardmagic分類器創建分類器。這是我的代碼:我該如何調試一個未定義的方法來避免類錯誤?
require 'classifier'
classifications = '1007.09', '1006.03'
traindata = Hash["1007.09" => "ADAPTER- SCREENING FOR VALVES VBS", "1006.03" => "ACTUATOR- LINEAR"]
b = Classifier::Bayes.new classifications
traindata.each do |key, value|
b.train(key, value)
end
但是當我運行此我得到以下錯誤:
Notice: for 10x faster LSI support, please install http://rb-gsl.rubyforge.org/
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)
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/classifier-1.3.3/lib/classifier/bayes.rb:26:in `each'
from c:/Ruby192/lib/ruby/gems/1.9.1/gems/classifier-1.3.3/lib/classifier/bayes.rb:26:in `train'
from C:/_Chris/Code/classifier/smdclasser.rb:13:in `block in <main>'
from C:/_Chris/Code/classifier/smdclasser.rb:11:in `each'
from C:/_Chris/Code/classifier/smdclasser.rb:11:in `<main>'
這是從創業板代碼的源代碼:
# Provides a general training method for all categories specified in Bayes#new
# For example:
# b = Classifier::Bayes.new 'This', 'That', 'the_other'
# b.train :this, "This text"
# b.train "that", "That text"
# b.train "The other", "The other text"
def train(category, text)
category = category.prepare_category_name
text.word_hash.each do |word, count|
@categories[category][word] ||= 0
@categories[category][word] += count
@total_words += count
end
end
我迷失在那裏去解決這個錯誤,我應該採取的下一步是什麼?
我是在處理與我的代碼有關的任何問題時使用調試器的信徒。 Ruby的'rdebug'非常好,沒有一段代碼在我開發的時候沒有在那個層次上進行檢查。我是肛交,但它會導致更低的錯誤報告。查看Ruby 1.8的'gem install rdebug',或1.9.2的'gem install rdebug19'。 – 2012-02-08 18:47:50
我從來沒有使用過調試器,我正在使用aptana studio編寫它。通常,當我有錯誤時,我會遵循代碼並使用我的語義和語法知識來查看問題。那麼我來堆棧或谷歌的錯誤消息。 – holaSenor 2012-02-08 18:59:02