# Reads and writes to ALICE (ProgramD). # (Put this file in the ProgramD/bin directory.) class ALICE def initialize @alice = IO.popen("simple-console.bat", "r+") # windows #@alice = IO.popen("simple-console.sh", "r+") # linux while line = @alice.gets puts "#{line}" # look for the end of the line that immediately precedes the first prompt if line =~ /help\.$/ then break end end end def putstr(s = '') s.strip! @alice.puts(s.to_s) getstr end def getResponse(s = '') return putstr(s) end def getstr line = @alice.gets # Get rid of the INFO line. full_response = "" while (line = @alice.gets) =~ /^YourBot\> / if line =~ /YourBot\> (.*)/ then line = $1 else next end full_response << line.chomp << " " c = @alice.getc # Check the first character of the next line if c.chr == "[" then break end # If it's the first chr of a prompt, break @alice.ungetc(c) # If it's not a prompt, push the chr back on the stream. end return full_response end end if $0 == __FILE__ alice = ALICE.new print "\n> "; $stdout.flush quitwords = [":q", "quit", "bye", "exit"] while (line = gets) !~ /^(#{quitwords.join('|')})+/ and line !~ /^$/ puts alice.putstr(line.to_s) print "\n> "; $stdout.flush end alice.putstr("/exit") print "Bye!" end