lib/megahal/megahal.rb
# Generate a reply to the user's input. If the learning attribute is set to true,
# MegaHAL will also learn from what the user said.  Note that it takes MegaHAL
# about one second to generate about 500 replies.
# 
# @param [String] input A string that represents the user's input. If this is
#                       nil, MegaHAL will attempt to reply with a greeting,
#                       suitable for beginning a conversation.
# @param [String] error The default reply, which will be used when no
#                       suitable reply can be formed.
#
# @return [String] MegaHAL's reply to the user's input, or the error
#                  string if no reply could be formed.
Generate a reply to the user's input. If the learning attribute is set to true,
MegaHAL will also learn from what the user said.  Note that it takes MegaHAL
about one second to generate about 500 replies.
Generate a reply to the user ' s input . If the learning attribute is set to true MegaHAL will also learn from what the user said . Note that it takes MegaHAL about one second to generate about 500 replies .
def reply(input, error="...")
    puncs, norms, words = _decompose(input ? input.strip : nil)
    keyword_symbols =
      MegaHAL.extract(norms)
          .map { |keyword| @dictionary[keyword] }
          .compact
    input_symbols = (norms || []).map { |norm| @dictionary[norm] }
    utterances = []
    9.times { utterances << _generate(keyword_symbols) }
    utterances << _generate([])
    utterances.delete_if { |utterance| utterance == input_symbols }
    utterances.compact!
    reply = nil
    while reply.nil? && utterances.length > 0
      break unless utterance = _select_utterance(utterances, keyword_symbols)
      reply = _rewrite(utterance)
      utterances.delete(utterance)
    end
    _learn(puncs, norms, words) if @learning && norms
    return reply || error
  end
def reply ( input , error = "..." ) puncs , norms , words = _decompose ( input ? input . strip : nil ) keyword_symbols = MegaHAL . extract ( norms ) . map { | keyword | @dictionary [ keyword ] } . compact input_symbols = ( norms || [ ] ) . map { | norm | @dictionary [ norm ] } utterances = [ ] 9 . times { utterances << _generate ( keyword_symbols ) } utterances << _generate ( [ ] ) utterances . delete_if { | utterance | utterance == input_symbols } utterances . compact! reply = nil while reply . nil? && utterances . length > 0 break unless utterance = _select_utterance ( utterances , keyword_symbols ) reply = _rewrite ( utterance ) utterances . delete ( utterance ) end _learn ( puncs , norms , words ) if @learning && norms return reply || error end
Generate a reply to the user's input.
Generate a reply to the user ' s input .
# create candidate utterances # select the best utterance, and handle _rewrite failure # learn from what the user said _after_ generating the reply
MegaHAL's reply to the user's input, or the error string if no reply could be formed.
MegaHAL ' s reply to the user ' s input or the error string if no reply could be formed .
String
input
error
error The default reply, which will be used when no suitable reply can be formed.
error The default reply which will be used when no suitable reply can be formed .