home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR22 / JORF21_2.ZIP / ISA.J < prev    next >
Text File  |  1993-07-05  |  6KB  |  220 lines

  1. Class:Isa
  2.  
  3. Isa:Start
  4.   New (Key, TextIn)
  5.   Jorf:File ("ISA.JRF", Here)
  6.   While (Kbd:Got!="Esc_Key")
  7.     Win:Add("Is-A data base")
  8.       String:"Enter information in the form of:     A is B"
  9.       String:"Enter a query in the form:            Is A B"
  10.       String:"You can get a query list with:     What is B"
  11.       String:""
  12.       String:"Enter 'Display' to display the data base. "
  13.       String:""
  14.       Input:"", Wid:40, Field:"TextIn"
  15.       String:""
  16.     If (TextIn)
  17.       Isa:Parse (TextIn)
  18.     Win:Del
  19.     TextIn = Null
  20.   Return
  21.  
  22.  
  23. Isa:Display
  24.   New (Rule:Rule, Counter)
  25.   Win:Add("Is-A Data Base",0,0,10,50,Here)
  26.   Rule->Type = 2
  27.   Rule->Obj  = "is"
  28.   Rule->KLen = 1
  29.   While (Ok)
  30.     Rule:Next (Rule)
  31.     If (Rule->Fnd = False)
  32.       Win:Pause
  33.       Return
  34.     If (Counter > 9)
  35.       Win:Pause
  36.       Counter = 0
  37.     Str:PutLine(Rule->Ptr)
  38.     ++Counter
  39.   Return
  40.  
  41. Isa:IsArticle (Word)
  42.   Switch (Word)
  43.     Case "a"
  44.     Case "an"
  45.     Case "the"
  46.       Return (True)
  47.   Return (False)
  48.  
  49. Isa:Parse (Text)
  50.   | Parse the sentence to see what is in it
  51.   New (Pos, StartText, GoalText, Result)
  52.  
  53.   Pos = Str:In(Text," is ")
  54.   If (Pos)
  55.     StartText = Str:At(Text, 1, Pos-1)
  56.     GoalText  = Str:At(Text, Pos+4, Str:Len(Text))
  57.  
  58.   Switch
  59.     | What is B
  60.     Case (Word:At(StartText,1) = "what")
  61.       Result = Isa:Search (Null, GoalText)
  62.       If (Result = Null)
  63.         Result = Isa:Search (GoalText, Null)
  64.       If (Result = Null)
  65.         Result = Text:Add ("I don't know")
  66.       Msg:Add (Null,"Ok",Result)
  67.       Return
  68.  
  69.     | A is B
  70.     Case (StartText && GoalText)
  71.       Isa:SaveRule (StartText, GoalText, Text)
  72.       Msg:Add
  73.         I Understand
  74.       Return
  75.  
  76.     | is A B
  77.     Case (Pos < 1)
  78.       If (Word:At (Text, 1) = "is")
  79.         | This is kinda a pain -
  80.         |  need to parse two expressions without having any
  81.         |  real guide as to the separation between them.
  82.         |  Instead, assume expressions are one word or
  83.         |  an article (a,an,the) followed by one word.
  84.         Pos = 2
  85.         StartText = Word:At (Text,Pos)
  86.         If (Isa:IsArticle(StartText))
  87.           ++Pos
  88.           StartText = StartText + " " + Word:At(Text,Pos)
  89.         ++Pos
  90.         GoalText = Word:At(Text, Pos)
  91.         If (Isa:IsArticle (GoalText))
  92.           ++Pos
  93.           GoalText = GoalText + " " + Word:At (Text,Pos)
  94.         Result = Isa:Search (StartText, GoalText)
  95.         If (Result)
  96.           Msg:Add(Null,"Ok",Result)
  97.         Else
  98.           Msg:Add
  99.             I don't know whether
  100.             {StartText} is {GoalText}
  101.         Return
  102.       If (Word:At (Text, 1) = "display")
  103.         Isa:Display
  104.         Return
  105.  
  106.   | If nothing has been done, try showing a help screen
  107.   Msg:Add
  108.     Statements should take the
  109.     form "A is B". examples:
  110.  
  111.       Josephine is a Goat
  112.       A Goat is a Mammal
  113.       A Mammal is an Animal
  114.       Socrates is a Person
  115.       A Person is Mortal
  116.  
  117.     Queries should begin with "Is"
  118.     or "What".  Examples:
  119.       What is mortal
  120.       Is Socrates mortal
  121.       Is Josephine an Animal
  122.       What is an Animal
  123.  
  124.     The special command "Display" will
  125.     display the Is-A Data Base.
  126.   Return
  127.  
  128. Isa:SaveRule (StartText, GoalText, Text)
  129.   | Store text as O-A-V and A-V-O to allow
  130.   | front and back chaining
  131.   New (Rule:Rule)
  132.   Rule->Type   = 1
  133.   Rule->Obj    = StartText
  134.   Rule->Att    = "is"
  135.   Rule->Val    = GoalText
  136.   Rule->Ptr    = Text
  137.   Rule:Add (Rule)
  138.  
  139.   Rule->Type   = 2
  140.   Rule->Obj    = "is"
  141.   Rule->Att    = GoalText
  142.   Rule->Val    = StartText
  143.   Rule->Ptr    = Text
  144.   Rule:Add (Rule)
  145.  
  146. Isa:Search (StartText, GoalText)
  147.   | Search data base for item
  148.   New (Rule:Rule, Result)
  149.   Switch
  150.     | If Start and Goal are known
  151.     |   first look to see if there is a
  152.     |   rule that encompasses both
  153.     Case (StartText && GoalText)
  154.       Rule->Type   = 1
  155.       Rule->Obj    = StartText
  156.       Rule->Att    = "is"
  157.       Rule->Val    = GoalText
  158.       Rule->KLen   = 3
  159.       Rule:Find (Rule)
  160.       If (Rule->Ptr)
  161.         | This was easy!
  162.         Return (Text:Add ("Yes, " + Rule->Ptr), Null)
  163.       Else
  164.         | There is no rule for Start and Goal
  165.         | So lets see what rules there are for Start,
  166.         | and check if any match the Goal.
  167.         Rule->Type   = 1
  168.         Rule->Obj    = StartText
  169.         Rule->Att    = "is"
  170.         Rule->Val    = Null
  171.         Rule->Fnd    = False
  172.         Rule->KLen   = 2
  173.         While (Ok)
  174.           Rule:Next (Rule)
  175.           | If not Fnd then we are out of rules for Start
  176.           If (Rule->Fnd = False)
  177.             Return (Null)
  178.           | If Fnd, see if this Mid-rule matches the goal
  179.           Result = Isa:Search (Rule->Val, GoalText)
  180.  
  181.           | If a match was Fnd, we are home free,
  182.           |  add this rule to the result list, and return.
  183.           |  Otherwise, loop back to see if there is another
  184.           |  Start rule we can check.
  185.           If (Result)
  186.             Result = Text:Add (Rule->Ptr, Result)
  187.             Return (Text:Top (Result))
  188.  
  189.  
  190.     Case (GoalText)
  191.       | StartText is null, so look through all goaltexts
  192.       Rule->Type = 2
  193.       Rule->Obj  = "is"
  194.       Rule->Att  = GoalText
  195.       Rule->KLen = 2
  196.       | Dropping through to loop below
  197.  
  198.     Case (StartText)
  199.       | GoalText is null, so look through all Start texts
  200.       Rule->Type = 1
  201.       Rule->Obj  = StartText
  202.       Rule->Att  = "is"
  203.       Rule->KLen = 2
  204.       | Dropping through to loop below
  205.  
  206.     Else
  207.       | Both Start and Goal are null.
  208.       | This should probably be an error because
  209.       | it indicates that something has gone wrong.
  210.       Return (Null)
  211.  
  212.   While Ok
  213.     | Read through any matching rules, exit when
  214.     | there are no more..
  215.     Rule:Next (Rule)
  216.     If (Rule->Fnd = False)
  217.       Return (Text:Top (Result))
  218.     Result = Text:Add (Rule->Ptr, Result)
  219.  
  220.