2013-04-04 58 views
0

当前正在经历从Ruby开始的第12章,它构建了一个对话机器人。运行basic_client.rb文件时,bot加载。当给出'我讨厌电视'的输入时,机器人应该识别一个带有'仇恨'一词的模式,并根据模式返回响应。未初始化的常量Bot :: Wordplay(NameError)从开始Ruby教程

而是从bot文件返回一些名称错误。这些都与wordplay.rb文件有关,但我不确定是什么导致它。任何帮助,将不胜感激。

/bot.rb:73:in `block (2 levels) in possible_responses': uninitialized constant Bot::Wordplay (NameError) 
/bot.rb:69:in `collect' 
/bot.rb:69:in `block in possible_responses' 
/bot.rb:59:in `each' 
/bot.rb:59:in `possible_responses' 
/bot.rb:27:in `response_to' 
from basic_client.rb:8:in `<main>' 

文件正在运行:basic_client.rb

`require './bot' 

bot = Bot.new(:name => 'Fred', :data_file => 'fred.bot') 

puts bot.greeting 

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

puts bot.farewell` 

所需bot.rb:

require 'yaml' 
require './wordplay' 

class Bot 
attr_reader :name 

def initialize(options) 
    @name = options[:name] || "Unnamed Bot" 
    begin 
     @data = YAML.load(File.read(options[:data_file])) 
    rescue 
     raise "Can't load bot data" 
    end 
end 

def greeting 
    random_response :greeting 
end 

def farewell 
    random_response :farewell 
end 

def response_to(input) 
    prepared_input = preprocess(input.downcase) 
    sentence = best_sentence(prepared_input) 
    responses = possible_responses(sentence) 
    responses[rand(responses.length)] 
end 

private 

def random_response(key) 
    random_index = rand(@data[:responses][key].length) 
    @data[:responses][key][random_index].gsub(/\[name\]/, @name) 
end 

def preprocess(input) 
    perform_substitutions(input) 
end 

def perform_substitutions(input) 
    @data[:presubs].each { |s| input.gsub!(s[0], s[1]) } 
    input 
end 

def best_sentence(input) 
    hot_words = @data[:responses].keys.select do |k| 
     k.class == String && k =~ /^\w+$/ 
    end 

    WordPlay.best_sentence(input.sentences, hot_words) 
end 

def possible_responses(sentence) 
    responses = [] 

    # Find all patterns to try to match against 
    @data[:responses].keys.each do |pattern| 
     next unless pattern.is_a?(String) 

     # For each pattern, see if the supplied sentence contains 
     # a match. Remove substitution symbols (*) before checking. 
     # Push all responses to the responses array. 

     if sentence.match('\b' + pattern.gsub(/\*/, '') + '\b') 
      # If the pattern contains substitution placeholders, perform the substitutions 
      if pattern.include?('*') 
       responses << @data[:responses][pattern].collect do |phrase| 
        # Erase everything before the placeholder, leaving everything after it 
        matching_section = sentence.sub(/^.*#{pattern}\s+/, '') 
        # Then substitute the text after the placeholder with the pronouns switched 
        phrase.sub('*', Wordplay.switch_pronouns(matching_section)) 
       end 
      else 
       responses << @data[:responses][pattern] 
      end 
     end 
    end 

    # If there were no matches, add the default ones 
    responses << @data[:responses][:default] if responses.empty? 
    # Flatten the blocks of responses to a flat array 
    responses.flatten 
end 
end 

的文字游戏文件:

class String 
def sentences 
    self.gsub(/\n|\r/, ' ').split(/\.\s*/) 
end 

def words 
    self.scan(/\w[\w\'\-]*/) 
end 
end 

class WordPlay 
def self.switch_pronouns(text) 
    text.gsub(/\b(I am|You are|I|You|Your|My|Me)\b/i) do |pronoun| 
     case pronoun.downcase 
     when "i" 
      "you" 
     when "you" 
      "me" 
     when "me" 
      "you" 
     when "i am" 
      "you are" 
     when "you are" 
      "i am" 
     when "your" 
      "my" 
     when "my" 
      "your" 
     end 
    end.sub(/^me\b/i, 'i') 
end 

def self.best_sentence(sentences, desired_words) 
    ranked_sentences = sentences.sort_by do |s| 
     s.words.length - (s.downcase.words - desired_words).length 
    end 

    ranked_sentences.last 
end 
end 

回答

1

语言ruby对其变量和内容是区分大小写的。

因此,您需要使用Wordplay这个词在任何地方使用它。

我会建议写class Wordplay小写P. 另一种选择是写一个大写字母P的其他事件,就像Greg Guida建议的那样。

+0

啊,这就是它出错的地方..谢谢澄清。 – 2013-04-05 17:50:55

+0

欢迎您:) – scones 2013-04-05 17:52:23

0

在文字游戏在p上第73行应该是大写

+0

非常感谢。看来我需要更多地关注我的代码中的大写/小写。 – 2013-04-05 17:52:01

相关问题