0

抱歉,如果这是一个新手问题,但我不知道如何解决这个问题。当我尝试运行下面的代码时,我目前遇到了这些错误:

bot.rb:58:in `rescue in initialize': Can't load bot data (RunTimeError)

bot.rb:55:in `initialize'

basic_client.rb:3:in `new'

basic_client.rb:3:in `<top (required)>'

这是 bot.rb 的源代码,错误出现在“@data = YAML.load(File.open(options[:data_file]).read)”部分。

# A basic implementation of a chatterbot
class Bot
  attr_reader :name

  # Initializes the bot object, loads in the external YAML data
  # file and sets the bot's name. Raises an exception if
  # the data loading process fails.
  def initialize(options)
    @name = options[:name] || "Unnamed Bot"
    begin
      @data = YAML.load(File.open(options[:data_file]).read)
    rescue
      raise "Can't load bot data"
    end
  end

这是 basic_client.rb 文件的源代码:

require './bot'

bot = Bot.new(:name => ARGV[0], :data_file => ARGV[1])

puts bot.greeting

while input = $stdin.gets and input.chomp != 'end'
  puts '>> ' + bot.response_to(input)
end

puts bot.farewell

如果有人可以帮助我,那就太好了。此外,如果您需要有关该问题的更多信息或说明,我也可以提供。

谢谢!

4

1 回答 1

0

更改救援,以便您可以看到完整的消息:

 rescue => e
  raise "Can't load bot data because: #{e}"

然后我会说那里出现错误意味着您的文件格式错误(使用http://yamllint.com/检查其语法)或路径不正确。您需要确保正确地将 yaml 文件的路径作为第二个参数 (ARGV[1]) 传递给 basic_client.rb:

ruby basic_client.rb "C3PO" "bot.yml"

我不确定“bot.yml”应该是什么样子,但它必须是您期望在@data变量中的数据。

于 2013-10-20T04:53:23.483 回答