################################################################################ # Stores and displays TODO items. # # > TODO: fix todoagent # Okay, I have added "fix todoagent". # # > show todos # 1: fix todoagent # # > delete item 1 # Okay, I have deleted "fix todoagent" # # > ################################################################################ require 'yaml' $Quitwords = [':q', 'quit', 'exit', 'bye'] class MyAgent def initialize(args=Array.new) @workingDir = File.expand_path(File.dirname(__FILE__)) @filename = @workingDir + "/todoagent-api.yaml" unless load_message = load_synonyms @patterns = [ [/^(.*?) is a synonym for (.*)/, :add_synonym], [/^save syn/, :save_synonyms], [/^load syn/, :load_synonyms], [/^add pattern (.*), (.*)/, :add_pattern], [/^>(.*)/, :defining_method], [/^restart/, :restart] ] save_synonyms # creates patterns.yaml on first use. else puts load_message end @method_def = '' end def def_method method_def file = IO.read(__FILE__) return "Doesn't have 'end # class'" unless file =~ /(.*)end # class(.*)if \$0 == __FILE__(.*)/m grp1 = $1.to_s; grp2 = $2.to_s; grp3 = $3.to_s new_filename = get_new_filename File.open(new_filename, "w+") { |f| f.puts grp1 + method_def + "\n\nend # class\n\nif $0 == __FILE__" + grp3 } @patterns.push( [@new_regexp, @new_symbol] ) save_synonyms puts "#{method_def}" MyAgent.class_eval(method_def) end # If this file has a '.' + a number before the '.rb' extension # (i.e. foo.0.rb), increment the number (i.e. return foo.1.rb). # Otherwise return the name of this file unchanged. def get_new_filename old_filename = __FILE__ if old_filename =~ /(.*)\.(\d+)\.rb$/ incr = $2.to_i + 1 return new_filename = $1.to_s + "." + incr.to_s + ".rb" end return old_filename # Dangerous! end # Match input against each regexp pattern in @patterns; # if there's a match, call the method associated with the pattern. def getResponse input r = "Default response." @score = 2 #puts "input==#{input}" @patterns.each { |pattern| if input =~ pattern[0] return [self.send(pattern[1], pattern[0], input), @score] rescue return r end } return r end def load_synonyms(pattern=nil, input=nil) if File.exist?(@filename) @patterns = YAML.load_file(@filename) return "I have loaded #{@filename}." end return false end def save_synonyms(pattern=nil, input=nil) r = "Eep! error saving synonyms! " begin File.open(@filename, 'w') do |out| YAML.dump(@patterns, out) end return "Okay I have saved the synonyms to disk." rescue => error return r << error end end def add_synonym(pattern, input) return "Problem matching pattern." unless input =~ pattern grp1 = %r{(?i-mx:^#{$1})} grp2 = %r{(?i-mx:^#{$2})} method ='' @patterns.each { |p| #puts "p[0]==#{p[0]}; p[0].class==#{p[0].class}; grp2==#{grp2}; grp2.class==#{grp2.class}" if p[0].to_s == grp2.to_s method = p[1] break end } return unless method.to_s != nil @patterns.push( [grp1, method] ) save_synonyms return "Okay, #{grp1} has been added, and will now call #{method}." end def add_pattern(pattern=nil, input=nil) if input =~ pattern @new_regexp = $1 @new_symbol = $2 @new_regexp.gsub!(/\//, '') new_re = %r{#{@new_regexp}} # Make the regexp case insensitive. new_re_s = new_re.to_s if new_re_s =~ /(-mix)/ new_re_s = $~.pre_match << "i-mx" << $~.post_match end @new_regexp = %r{#{new_re_s}} #puts "@new_regexp==#{@new_regexp}" method_def = " def #{@new_symbol}(pattern=nil, input=nil)\n return \"#\{input} doesn't match #\{pattern}\" unless input =~ pattern\n return 'Default response from " + @new_symbol + "'\n end # method_def" return def_method(method_def) else return "#{input} doesn't match #{pattern}." end end def defining_method(pattern=nil, input=nil) if input =~ pattern @method_def << "\n " << $1.to_s if @method_def =~ /end # method_def/ return def_method(@method_def) end return '' else return "#{input} doesn't match #{pattern}." end end def method_missing input, *rest return getResponse(input.to_s) end # Strips delimiters. def process str str.strip! str.sub!(/\?$/, '') str.sub!(/^\[/, '') str.sub!(/\]$/, '') str.sub!(/^(?:"|')/, '') str.sub!(/(?:"|')$/, '') str.sub!(/\.$/, '') str.sub!(/;$/, '') str.sub!(/,$/, '') str.sub!(/!$/, '') str.sub!(/\?$/, '') return str end def restart(pattern=nil,input=nil) exec "ruby #{__FILE__}" end # method_def def add_todo(pattern=nil, input=nil) return "#{input} doesn't match #{pattern}" unless input =~ pattern grp1 = process $1 todos = File.new("todos.txt", "a") todosarr = IO.readlines("todos.txt") if todosarr == nil then todosarr = Array.new end count = todosarr.size + 1 todos.puts"#{count}: " << grp1 todos.close @score = 4 return "Okay, I have added \"#{grp1}\"." end # method_def def show_todos(pattern=nil, input=nil) return "#{input} doesn't match #{pattern}" unless input =~ pattern @score = 4 begin todos = File.new("todos.txt", "r") rescue; return "No TODOs!"; end toReturn = "" count = 1 while (line = todos.gets) if line =~ /(\d+)\: / line.sub!(/(\d+)\: /, "#{count}: ") count += 1 end toReturn << line end if toReturn.strip.size == 0 then return "No TODOs" end todos.close todos = File.new("todos.txt", "w") todos.puts(toReturn) todos.close return toReturn end # method_def def delete_todo(pattern=nil, input=nil) return "#{input} doesn't match #{pattern}" unless input =~ pattern grp1 = process $1 #puts "grp1==#{grp1}" todos = IO.readlines("todos.txt") newtodos = Array.new itemdeleted = "" todos.each { |line| found = false case line when /^#{grp1}\: (.*)/ found = true; itemdeleted = $1.to_s end newtodos.push line unless found } #puts "newtodos: #{newtodos}" newfile = File.new("todos.txt", "w+") if newtodos.size == 0 newfile.close begin File.delete(newfile) rescue; puts "Inexplicable error!"; end else newfile.puts(newtodos.join("")) newfile.close end if found then @score = 5; return "Okay, I have deleted \"#{itemdeleted}\"" end @score = 4 return "I can't find item \"#{grp1}\"." end # method_def end # class if $0 == __FILE__ bot = MyAgent.new print "\n> "; $stdout.flush while (line = gets) !~ /^#{$Quitwords.join('|')}$/i and line !~ /^$/ response = bot.send(line.to_s).to_s if response.class == "Array" then response = response[0] end response = response.chop # remove the score puts response + "\n\n" print "> "; $stdout.flush end puts "Bye" end # of file