home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / gofer230.zip / Progs / Gofer / Demos / Expert / Knowledge.hs < prev    next >
Text File  |  1994-06-23  |  3KB  |  84 lines

  1. {------------------------------------------------------------------------------
  2.                                    KNOWLEDGE
  3.  
  4. Knowledge, in the form of sentences and phrases with variables in them, is
  5. represented using a tree structure. Simple parsers are provided for rules,
  6. goals, relations and nouns. Functions are provided for converting a text file
  7. into a table of definitions and for accessing the table.
  8. ------------------------------------------------------------------------------}
  9.  
  10. module Knowledge where
  11. import Result
  12. import Table
  13.  
  14. -- The type `Phrase' is a tree-like data structure for storing sentences and
  15. -- phrases. A phrase is usually a term consisting of a word with a list of
  16. -- subphrases. Variables are picked out separately as they have a special role,
  17. -- and a function is provided for extracting a duplicate-free list of the names
  18. -- of the variables in a phrase. Variable names start with capital letters. A
  19. -- single type is used rather than separate types for rules, goals, relations
  20. -- and so on to make it easier to write the matching and searching modules.
  21.  
  22. data Phrase = Term String [Phrase] | Var String
  23.  
  24. vars p = nub (names p)  where
  25.    names (Var x) = [x]
  26.    names (Term x ps) = concat [names p | p <- ps]
  27.  
  28. -- The display function `showPhrase' assumes that the only phrases are
  29. -- variables, nouns, and pairs of subphrases with joining words between them.
  30.  
  31. showPhrase (Var x) = x
  32. showPhrase (Term x []) = x
  33. showPhrase (Term op [p1,p2]) =
  34.    showPhrase p1 ++ " " ++ op ++ " " ++ showPhrase p2
  35.  
  36. -- Each parser takes a list of words and returns a Phrase. The parsers for
  37. -- rules, goals and relations involve finding the joining word and the two
  38. -- lists of words on either side with `split', and then parsing the two lists.
  39. -- A rule is a relation and a goal joined by `if'. A goal is a collection of
  40. -- relations joined by `and' and `or', with `and' binding tighter. A relation
  41. -- is two nouns joined by a verb, and a noun is a word, perhaps preceded by
  42. -- `a', `an' or `the' for readability. These parsers are neither very general
  43. -- (no brackets, for instance) nor very efficient, nor do they detect errors.
  44.  
  45. rule ws = split ws relation "if" goal
  46.  
  47. goal ws
  48.    | elem "or" ws  = split ws goal "or" goal
  49.    | elem "and" ws = split ws goal "and" goal
  50.    | otherwise     = relation ws
  51.  
  52. relation ws =
  53.    split ws noun verb noun  where
  54.    verb = head [w | w<-ws, elem w verbs]
  55.    verbs = ["is","describes","has","can","eats"]
  56.  
  57. noun [a,x] | elem a ["a","an","the"]  =  noun [a++" "++x]
  58. noun [x] | ('A' <= head x) && (head x <= 'Z') = Var x
  59. noun [x]  =  Term x []
  60.  
  61. split ws f op g =
  62.    Term op [f lhs, g rhs]
  63.    where
  64.    lhs = takeWhile (/=op) ws
  65.    rhs = tail (dropWhile (/=op) ws)
  66.  
  67. -- The `definitions' function takes a list of text lines and converts it into a
  68. -- table of definitions. Each entry is a verb, together with the rules which
  69. -- are define that verb. Each verb is either completely defined in the table
  70. -- (eg `is', `describes') or is completely undefined so that the user has to be
  71. -- asked (eg `has', `eats'). The `relevant' function extracts from the table
  72. -- the list of rules which are relevant to a given relation.
  73.  
  74. definitions ls =
  75.    updateList newTable [(v, def v) | v<-verbs] where
  76.    def v = [r | r<-rs, verb r == v]
  77.    verbs = nub [verb r | r<-rs]
  78.    verb (Term "if" [Term v ns, g]) = v
  79.    rs = [rule (words l) | l<-ls]
  80.  
  81. relevant defs (Term v ns) =
  82.    if fails lookup then [] else answer lookup where
  83.    lookup = find defs v
  84.