Version 0 of A Program That Learns

Updated 2007-09-17 09:15:55 by GPS

George Peter Staplin - Mon Sep 17, 2007 I was pondering how to get the computer to ask me questions, so that it could learn. I rediscovered that learning is a recursive process at times.

Example usage:

 $ tclsh8.5 learn.tcl 
 what?  say hello
 how? puts hello
 what? puts a command
 how? you're too primitive to understand how to eval a command
 what? you're you
 how? it's a word
 what? it's it is
 how? what? a singular, 1
 how? the creators of English
 what? the hmm, next question?
 how? I don't know
 what? I George
 ...

Press Ctrl-d for EOF when you don't know how to answer a question, and it will return to what it was asking before.

When the script is done, it will print the rules that it learned.


 proc what? {{this ""}} {
  puts -nonewline "what? $this "
  flush stdout
  string trim [gets stdin]
 }

 proc how? {} {
  puts -nonewline "how? "
  flush stdout
  string trim [gets stdin]
 }


 proc ask-recurse {word} {
  global rules
  if {[info exists rules($word)]} return
  set rules([what? $word]) [set how [how?]]
  foreach w [split $how] {
   ask-recurse [string trim $w]
  }
 }

 proc ask this {
  global rules
  set rules([what? $this]) [set how [how?]]
  foreach word [split $how] {
   ask-recurse $word
  }
 }

 proc main {} {
  global rules
  while {![eof stdin]} {
   ask "" 
  }
  parray rules
 }
 main