8
\$\begingroup\$

CleverBot is a conversational AI that works fundamentally by repeating responses from real humans it has heard previously. It starts off pretty dumb, but once the database becomes (a lot) larger it begins sounding more like an actual person!

How your program should work

  1. Output Hi!
  2. Receive the user's reply as a string
    • The reply is added to a list of responses associated with the program's last output (eg. the first reply would associate with Hi!).
  3. Output one of the responses associated with the reply from step 2
    • Every output has a list of responses associated with it.
    • The response should be randomly selected from the list of associated responses.
    • Matching of previous replies is case-insensitive and without symbols (only 0-9, A-Z and space are matched). For example, entering hi could return any response associated with hi, Hi!, ///H|||I\\\, etc.
    • If the user's reply does not match any previous responses, randomly select a response from the entire list of responses.
  4. Repeat steps 2 and 3 indefinitely

Rules

  • Output must be exactly the same as the user originally entered it
  • If a reply that needs to be added to the list of responses is already in the list, it can be optionally be added again (so it appears in the list twice)
  • Input will never be empty and will only contain printable ASCII (code points 32 to 126)
  • All possible random values must have an equal chance of occurring
  • Must be a full program (not just a function)
  • Must output to STDOUT or closest equivalent
  • Must take input from STDIN or closest equivalent

Test Case

Hi!
> hi...
hi...
> HI
HI                               // could output "hi..." or "HI"
> How are you?
How are you?                     // could output "hi...", "HI" or "How are you?"
> Good.
Good.                            // could output "hi...", "HI", "How are you?" or "Good."
> ~!@#$*`'"(how ar%&e you_\|./<?
Good.
> Good.
~!@#$*`'"(how ar%&e you_\|./<?   // could output "~!@#..." or "Good."
> 2Good.
HI                               // could output any of the user's previous replies

This is . Shortest answer in bytes wins!

\$\endgroup\$
3
  • \$\begingroup\$ Related. \$\endgroup\$
    – xenia
    Jan 16, 2016 at 9:23
  • \$\begingroup\$ Can there be two responses associated to one reply? \$\endgroup\$
    – TanMath
    Jan 16, 2016 at 11:00
  • \$\begingroup\$ @TanMath Yes, there can and should. Each reply has a list of responses. \$\endgroup\$
    – user81655
    Jan 16, 2016 at 11:01

1 Answer 1

2
\$\begingroup\$

JavaScript (ES6) 155 148 bytes

c=t=>t.replace(/[^\dA-Z ]/gi,"");r=["Hi!"];for(a=[],m={};u=prompt(alert(o=r[new Date%r.length]));r=m[c(u)]||a)(m[o=c(o)]=m[o]||[]).push(u),a.push(u)

Less golfed:

c=t=>t.replace(/[^\dA-Z ]/gi,"");
r=["Hi!"];
for(a=[], m={}; u=prompt( alert( o=r[new Date%r.length] ) );
r= m [ c(u) ] || a )( m[ o=c(o) ] = m[o] || [] ).push(u), a.push(u)
  • 3 bytes saved thanks to @balint
\$\endgroup\$
1
  • \$\begingroup\$ Use the date tehnique for randoms new Date%(r.length|0) \$\endgroup\$
    – Bálint
    May 13, 2016 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.