2012-02-08 21 views
1

我正在嘗試使用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 

我迷失在那裏去解決這個錯誤,我應該採取的下一步是什麼?

+0

我是在處理與我的代碼有關的任何問題時使用調試器的信徒。 Ruby的'rdebug'非常好,沒有一段代碼在我開發的時候沒有在那個層次上進行檢查。我是肛交,但它會導致更低的錯誤報告。查看Ruby 1.8的'gem install rdebug',或1.9.2的'gem install rdebug19'。 – 2012-02-08 18:47:50

+0

我從來沒有使用過調試器,我正在使用aptana studio編寫它。通常,當我有錯誤時,我會遵循代碼並使用我的語義和語法知識來查看問題。那麼我來堆棧或谷歌的錯誤消息。 – holaSenor 2012-02-08 18:59:02

回答

5

Classifier::Bayes.new預計值的分解數組,而不是單個參數。例如,請注意,示例代碼使用:

b = Classifier::Bayes.new 'This', 'That', 'the_other' 

而不是:

b = Classifier::Bayes.new ['This', 'That', 'the_other'] 

傳遞到自己的classifications陣列的圖示版本,它應該工作:

b = Classifier::Bayes.new *classifications 
+0

另一個問題,用我的traindata散列,如果我想用多個句子值來訓練分類「1007.09」會怎樣?哈希只允許唯一鍵,當我添加重複鍵時,只有一個使用traindata.length計數。我會簡單地創建一個新的散列並用每個散列進行訓練,而不是試圖用一個散列一次性訓練它? – holaSenor 2012-02-08 16:41:32

+0

是的,類似於「{」1007.09「=> [」第一句「,」第二句「]}。你還必須更新你的'each'循環,然後迭代每個數組的值。 – 2012-02-08 18:37:37

相關問題