Version 0 of Playing SIR

Updated 2004-03-07 11:22:02

Edit in progress --- if 0 {Richard Suchenwirth 2004-03-08 - In the "software museum", today I play with the Semantic Information Retriever SIR (Raphael, 1964), a software that takes natural-language sentences (a very restricted subset of English) to build a relational knowledge base, or answer questions about it. The following Tcl implementation is even weaker than the original from 40 years ago, but then again it is only a little weekend fun project... and re-plays the sample dialog with SIR as seen in the literature:

  • populate the knowledge base with some facts
  • ask questions
  • SIR will ask back if facts are missing in its chain of reasoning

}

 proc understand sentence {
    if [regexp {^what is an? (\w+)} $sentence -> item] {
       return [isa $item ?]
    }
    if [regexp {^what is (\w+)} $sentence -> item] {
       return [isa $item ?]
    }
    if [regexp {^(\w+) is a (\w+)$} $sentence -> item cat] {
       return [isa $item $cat]
    }
    if [regexp {^every (\w+) is a (\w+)$} $sentence -> item cat] {
       return [isa $item $cat]
    }
    if [regexp {^(any|every) (\w+) has (\d+) (\w+)s$} $sentence -> . cat n item] {
       return [has $item $n $cat]
    }
    if [regexp {^any (\w+) has (\d+) (\w+)$} $sentence -> cat n item] {
       return [has $item $n $cat]
    }
    if [regexp {^an? (\w+) is part of an? (\w+)$} $sentence -> part cat] {
       return [has $part * $cat]
    }
    if [regexp {^how many (\w+)s are on (\w+)$} $sentence -> part cat] {
       return [has $part ? $cat]
    }
    error "don't understand '$sentence'"
 }
 proc isa {item cat} {
    if {$cat eq "?"} {
       whats $item
    } else {
       ladd ::K($item,isa) $cat
       ladd ::K($cat,eg) $item
    }
 }
 proc has {item n cat} {
    if {$n eq "?"} {
       howmany $item $cat
    } else {
       set ::K($cat,has,$item) $n
       ladd ::K($item,ispartof) $cat
    }
 }
 proc howmany {item cat} {

puts info level 0

    if [info exists ::K($cat,has,$item)] {
       set n $::K($cat,has,$item)
       if {$n eq "*"} {ask-n $item $cat}
       return $n
    } else {
       if [info  exists ::K($cat,isa)] {
          foreach subcat $::K($cat,isa) {
             set n [howmany $item $subcat]
             if [numeric $n] {return $n}
          }
       }
       foreach fact [array names ::K $cat,has,*] {
          regexp $cat,has,(.+) $fact -> part
          set n $::K($fact)
          set n2 [howmany $item $part]
          if [numeric $n2] {return [expr $n*$n2]}
       }
    }
    return "can't tell"
 }
 proc ask-n {item cat} {
    puts "How many ${item}s per $cat?"
    understand [gets stdin]
 }
 proc numeric x {string is integer -strict $x}

 proc whats what {
    set cats $::K($what,isa)
    foreach i $cats {
        if [info exists ::K($i,isa)] {append cats " " $::K($i,isa)}
    }
    return "$what is a [join $cats {, a }]"
 }

#---- General utilities:

 proc ladd {listvar element} {
    upvar 1 $listvar list
    if ![info exists list] {set list {}}
    if {[lsearch $list $element]<0} {lappend list $element}
 }

#---- Testing

 understand {John is a boy}
 understand {every boy is a person}
 understand {any person has 2 hands}
 understand {a finger is part of a hand}
 puts [understand {what is John?}]
 puts [understand {what is a boy?}]