On 22 Feb, 22:09, William James <w_a_x_...@[EMAIL PROTECTED]
> wrote:
> On Feb 22, 5:59 am, Mark Tarver <dr.mtar...@[EMAIL PROTECTED]
> wrote:
>
>
>
>
>
> > On 22 Feb, 11:49, Mark Tarver <dr.mtar...@[EMAIL PROTECTED]
> wrote:
>
> > > Having got 3936 LOC through a 4000 LOC implementation, I thought I'd
> > > do some recreational
> > > hacking and do an old old program I've not looked at for some time -
> > > Eliza. =A0 You all know Eliza well enough for me not to have to
spell =
it
> > > out. =A0The challenge is to implement or dig up an Eliza program
(you
> > > don't have to write it yourself) in your favourite FPL. =A0Note that
t=
he
> > > script that drives Eliza's responses should not be counted towards
the=
> > > LOC count. =A0Some constraints.
>
> > > 1. =A0The script itself should be changeable by any novice. =A0That
is=
to
> > > say that it should not
> > > =A0 =A0 =A0be a pile of hard-wired code written in the native
language=
of
> > > the program or require
> > > =A0 =A0 =A0deep programming skills.
>
> > > 2. =A0The program should receive keyboard input from the user -
> > > including the usual punctuation
> > > =A0 =A0 =A0and any characters he wants to enter without crashing.
>
> > > During the Harrop Wars on comp.lang.lisp a lot of stuff was thrown
> > > around about the desirability of pattern matching. =A0The challenge
is=
> > > interesting because it involves a style of pattern-matching called
> > > 'segment pattern matching' that is not hard-wired into most FPLs and
> > > I'd like to see how well different FPLs cope with something outside
> > > the standard.
>
> > > Oh last thing; don't get too uptight about this. =A0It's only a bit
of=
> > > fun.
>
> > > Mark
>
> > Well here is my shot at it in Qi. =A0Total LOC excluding script is 70
> > LOC.
>
> I added some features and wrote it in Ruby.
>
> Sample session:
>
> ? Hi. I'm going to be your worst nightmare.
> Why are you going to be my worst nightmare?
> ? I think you're full of it.
> Why do you think I am full of it?
> ? My mother told me so.
> Tell me about your mother.
> ? No. You tell me about yours.
> We're talking about you, not me.
> ? You're obviously retarded.
> Please don't be so personal.
> ? Damn your eyes!
> I'm not sure that I understand you fully.
> ? Understand a kick in the pants?
> Can you elaborate on that?
> ? I always ignore idiots.
> Why do you always ignore idiots?
> ? I am afraid of dinosaurs.
> Have you always been afraid of dinosaurs?
> ? quit
>
> Script =3D
> =A0 [[:x, ["father",'mother','brother','sister'], :y],
> =A0 =A0 ["Tell me about your ", :_, "."]],
> =A0 [[:x, ["am", "i'm"], :y], [["Why are you", :y, "?"],
> =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0["Have you
alwa=
ys been", :y, "?"]]],
> =A0 [[:x, "I", :y], ["Why do you", :y, "?"]],
> =A0 [[:x, 'you', :y], [["We're talking about you, not me."],
> =A0 =A0 =A0 =A0 =A0 =A0 =A0["Please don't be so personal."]]],
> =A0 [[:x], [["That's very interesting. Do go on."],
> =A0 =A0 =A0 =A0 ["Tell me more."],
> =A0 =A0 =A0 =A0 ["I'm not sure that I understand you fully."],
> =A0 =A0 =A0 =A0 ["Can you elaborate on that?"] ]]
>
> def change_person s
> =A0 h =3D { 'I','you', =A0'my','your', 'myself','yourself',
> =A0 =A0 'you are','I am', "you're", 'I am' }
> =A0 tmp =3D s.scan(/you are|you're|\w+|\W+/).map{|s|
> =A0 =A0 h[s] or h.invert[s] or s }
> =A0 tmp[-1] =3D "me" =A0if "I" =3D=3D tmp[-1]
> =A0 tmp.join
> end
>
> patterns, symbols, replies =3D [], [], []
>
> Script.each{|ary|
> =A0 syms =3D []
> =A0 patterns << =A0Regexp.new( "^" +
> =A0 =A0 ary[0].map{|x|
> =A0 =A0 =A0 case x
> =A0 =A0 =A0 =A0 when Symbol
> =A0 =A0 =A0 =A0 =A0 syms << x
> =A0 =A0 =A0 =A0 =A0 "(.*?)"
> =A0 =A0 =A0 =A0 when String
> =A0 =A0 =A0 =A0 =A0 "\\b#{ x }\\b"
> =A0 =A0 =A0 =A0 when Array
> =A0 =A0 =A0 =A0 =A0 syms << :_
> =A0 =A0 =A0 =A0 =A0 "\\b(#{ x.join('|') })\\b"
> =A0 =A0 =A0 =A0 else
> =A0 =A0 =A0 =A0 =A0 ".*?"
> =A0 =A0 =A0 end
> =A0 =A0 }.join + "$", true ) # Case-insensitive.
> =A0 symbols << syms
> =A0 ary[1] =3D ary[1].sort_by{ rand } =A0if ary[1][0].is_a? Array
> =A0 replies << ary[1]
>
> }
>
> while true
> =A0 print "? "
> =A0 resp =3D gets.strip.sub(/[.!?,;]+$/, "")
> =A0 break if 'quit' =3D=3D resp
> =A0 patterns.each_with_index{|pat,i|
> =A0 =A0 if match_data =3D resp.match( pat )
> =A0 =A0 =A0 reply =3D replies[i]
> =A0 =A0 =A0 if reply[0].is_a? Array
> =A0 =A0 =A0 =A0 # Rotate replies for variety.
> =A0 =A0 =A0 =A0 reply.push( reply.shift )
> =A0 =A0 =A0 =A0 reply =3D reply[0]
> =A0 =A0 =A0 end
> =A0 =A0 =A0 captures =3D match_data.captures.map{|s| change_person s}
> =A0 =A0 =A0 # Create associative array mapping symbols to values.
> =A0 =A0 =A0 t =3D Hash[ *symbols[i].zip( captures ).flatten ]
> =A0 =A0 =A0 puts reply.map{|x| x.is_a?(Symbol) ? t[x] : x }.join
> =A0 =A0 =A0 break
> =A0 =A0 end
> =A0 }
> end- Hide quoted text -
>
> - Show quoted text -
First I've seen Ruby used. Don't understand a damn thing of
course :),
but 55 LOC puts it top.
Mark


|