| Over on comp.lang.lisp Mark Tarver the creator of Qi challenged the world to come up with a version of Eliza shorter than the Qi version, I forget, fifty lines? Anyway, Stevan Apter lowered the bar to 3 with K: e:{$[(#b)=i:(|/'b:(w@:&~(w:(`$" "\:x@&~x in".?"))in`)in\:/:A)?1b;E@*1?#E
" "~n:c@*,/(c:{x@*1?#x}C i)ss/:N;c
.q.ssr[c;n;" "/:$(),P'(w[k],p:1_'(0,1+k:b[i]?1b)_`,w)"I"$n]]}
Over to you, pg! :)ps. Ruby done good, came in at 55: Script =
[[:x, ["father",'mother','brother','sister'], :y],
["Tell me about your ", :_, "."]],
[[:x, ["am", "i'm"], :y], [["Why are you", :y, "?"],
["Have you always been", :y, "?"]]],
[[:x, "I", :y], ["Why do you", :y, "?"]],
[[:x, 'you', :y], [["We're talking about you, not me."],
["Please don't be so personal."]]],
[[:x], [["That's very interesting. Do go on."],
["Tell me more."],
["I'm not sure that I understand you fully."],
["Can you elaborate on that?"] ]]
def change_person s
h = { 'I','you', 'my','your', 'myself','yourself',
'you are','I am', "you're", 'I am' }
tmp = s.scan(/you are|you're|\w+|\W+/).map{|s|
h[s] or h.invert[s] or s }
tmp[-1] = "me" if "I" == tmp[-1]
tmp.join
endpatterns, symbols, replies = [], [], [] Script.each{|ary|
syms = []
patterns << Regexp.new( "^" +
ary[0].map{|x|
case x
when Symbol
syms << x
"(.*?)"
when String
"\\b#{ x }\\b"
when Array
syms << :_
"\\b(#{ x.join('|') })\\b"
else
".*?"
end
}.join + "$", true ) # Case-insensitive.
symbols << syms
ary[1] = ary[1].sort_by{ rand } if ary[1][0].is_a? Array
replies << ary[1]
} while true
print "? "
resp = gets.strip.sub(/[.!?,;]+$/, "")
break if 'quit' == resp
patterns.each_with_index{|pat,i|
if match_data = resp.match( pat )
reply = replies[i]
if reply[0].is_a? Array
# Rotate replies for variety.
reply.push( reply.shift )
reply = reply[0]
end
captures = match_data.captures.map{|s| change_person s}
# Create associative array mapping symbols to values.
t = Hash[ *symbols[i].zip( captures ).flatten ]
puts reply.map{|x| x.is_a?(Symbol) ? t[x] : x }.join
break
end
}
endpps. Norvig's from PAIP was reported at 150 LOC. |