現在、初級Ruby第12章を進めており、会話ボットを構築しています。basic_client.rb ファイルを実行すると、ボットが読み込まれます。「私はテレビが嫌いです」という入力が与えられると、ボットは「嫌い」という単語のパターンを認識し、そのパターンに基づいて応答を返す必要があります。
代わりに、ボット ファイルから多数の名前エラーが返されます。これらはすべて 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