# coding: utf-8 class Eliza def initialize() @history = [] # Instanz-Variable, in allen Methoden sichtbar # Eventuell weitere Variablen für bessere Antworten initialisieren # http://www.linguasorb.com/english/verbs/most-common-verbs/ @verbs = File.readlines("verblist-useful.txt").map{|l| l.split("\t")}.flatten end def antwort(satz) # hier satz und eventuell @history analysieren, antwort darauf aufbauen # Überprüfen, kommmen personalpronomen in 1. und 2. Person vor. # "Vertauschen" swaps = {"I"=>"you", "I'm"=>"you're", "I've" => "you've", "I am"=>"you are", "me" => "you", "my" => "your", "mine" => "yours", "myself" => "yourself", "your" => "my", "yours" => "mine", "yourself" => "myself", "you're" => "I'm", "you've" => "I have", "you are" => "I am" } antwort = satz.gsub(/(\bI('m|'ve| am|\b)|\bm[ey]\b|\bmine\b|\bmysel\b|(\byou(r|'re|'ve|rs|rself)\b)|\byou\s+\w+|you[!?.,]|you$)/i) {|m| puts "Found ->#{m}<-" if swaps[m] swaps[m] else if m=~/\byou\s+(\w+)/i if @verbs.include?($1) "I #{$1}" else "me #{$1}" end else m.gsub("you","me") end end } type = antwort.strip[-1] if type=="?" ans = ["It might be that", "Sometimes it might happen that", "Unlikely, but it is possible that", "It is questionable whether"] rest = ["Does this bother you?", "Is this why you came to me?", "Have you talked about this with your friends?", "Does your mother know?", "Do you want to reconsider?"] antwort = ans[rand(ans.size)]+" "+antwort[0...-1]+". "+rest[rand(rest.size)] else ans = ["Is it your fault that", "Does it happen often that", "What is your reaction that", "Does your mother care that"] antwort = ans[rand(ans.size)]+" "+antwort+"?" end @history.push(satz) return antwort end def is_good_bye?(satz) # Hier auswerten, ob das Gespräche beendet werden soll return false end def talk() puts "Please write a line" while (true) text = gets.chomp break if is_good_bye?(text) puts antwort(text) end puts "Good bye!" end end # Wird nur ausgeführt, wenn diese Datei # direkt auf der Kommandozeile # aufgerufen wird. if __FILE__==$0 e = Eliza.new e.talk() end