gpt4 book ai didi

ruby - 如何为 nil 类错误调试未定义的方法?

转载 作者:太空宇宙 更新时间:2023-11-03 17:39:00 25 4
gpt4 key购买 nike

我正在尝试使用 cardmagic 分类器 gem 创建一个分类器。这是我的代码:

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>'

这是 gem 代码的来源:

# 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

我不知道去哪里解决这个错误,下一步我应该做什么?

最佳答案

Classifier::Bayes.new 需要一个展开的值数组,而不是单个参数。例如,请注意示例代码使用:

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

而不是:

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

传入 classifications 数组的 splat 版本,它应该可以工作:

b = Classifier::Bayes.new *classifications

关于ruby - 如何为 nil 类错误调试未定义的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9196684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com