home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lifeos2.zip / LIFE-1.02 / EXAMPLES / NL.LF < prev    next >
Text File  |  1996-06-04  |  16KB  |  429 lines

  1. % Copyright 1992 Digital Equipment Corporation
  2. % All Rights Reserved
  3.  
  4. % This parses simple English sentences.
  5. % Try the query 'nl_parse([john,runs],S)?'
  6. % The grammar given here is very small and limited.
  7.  
  8. module("nl") ?
  9. public( nl_parse,
  10.     flu, malaria, frisbee, monk, monks, lunch, lunches, steak, steaks,
  11.     salad, file, files, compiler, pascal, computer, computers, mary, john,
  12.     spot, life, she, he, it, walks, walk, walked, runs, ran,
  13.     compile, compiles, compiled, compile, compiles, compiled, delete,
  14.     deletes, deleted, eat, eats, ate, catch, catches, caught, my, her, his,
  15.     its, the, a, some, many, small, big, fast, slow, bad, good, did,
  16.     should, could, would, can, will, but, slowly, quickly, correctly, in,
  17.     over, from, of, to, who, with, which, what, when, why, who, how, hey,
  18.     how, why, yes, no ) ? 
  19.  
  20. % The Type Hierarchy:
  21.  
  22. human_disease <| disease.
  23.  
  24. food := {vegetable;meat}.
  25.  
  26. inanimate := {food;information;tree}.
  27.  
  28. inanimate <| projectile.
  29.  
  30. animate := {human;inhuman;you}.
  31.  
  32. code <| computer.
  33.  
  34. inhuman := {dog;computer}.
  35.  
  36. % The Functions:
  37.  
  38. can_catch(human_disease) -> human.
  39. can_catch(disease) -> animate.
  40. can_catch(frisbee) -> {human; dog}.
  41. can_catch(projectile) -> human.
  42. can_catch(@) -> animate.
  43.  
  44. eaten_by(vegetarian) -> vegetable.
  45. eaten_by(carnivore) -> meat.
  46. eaten_by(omnivore) -> food.
  47. eaten_by(comp) -> information.
  48.  
  49. % The Parser:
  50.  
  51. nl_parse(List, Cat) :-
  52.     lookup(List, Defs),
  53.     nl_parsed(Defs, Cat, []).
  54.  
  55. nl_parsed([Def|Rest], Cat, Rest) :-
  56.     grammar_rule2(Def, Cat, Def).
  57.  
  58. nl_parsed([Def|Rest], Cat, S1) :-
  59.     grammar_rule(Def, Cat, Def, Needing),
  60.     nl_parsed(Rest, Needing, S1).
  61.  
  62. nl_parsed([Def|Rest], Cat, S1) :-
  63.     grammar_rule2(Def, Cat, Cat1),
  64.     nl_parsed([Def|Rest], Cat1, S1).
  65.  
  66. nl_parsed([Def|Rest], Cat, S1) :-
  67.     grammar_rule(Def, Cat, Cat1, Cat2),
  68.     nl_parsed([Def|Rest], Cat1, S2),
  69.     nl_parsed(S2, Cat2, S1).
  70.  
  71. grammar_rule2(@,
  72.     par(parsetree => tree(label => par, lhs => X), anaphora_out => B),
  73.     X:paragraph(anaphora_in => [], anaphora_out => B)).
  74.  
  75. grammar_rule(@,
  76.     paragraph(anaphora_in => A, anaphora_out => B, 
  77.           parsetree => tree(label => paragraph, lhs => X, rhs => Y)),
  78.     X:s(anaphora_in => A, anaphora_out => B),
  79.     Y:sstop).
  80.  
  81. grammar_rule2([], sstop, []).
  82.  
  83. grammar_rule(@,
  84.     paragraph(anaphora_in => A, anaphora_out => C, 
  85.           parsetree => tree(label => paragraph, lhs => X, rhs => Y)),
  86.     X:s(anaphora_in => A, anaphora_out => B),
  87.     moreps(anaphora_in => B, anaphora_out => C, parsetree => Y)).
  88.  
  89. grammar_rule([],
  90.     moreps(anaphora_in => A, anaphora_out => B,
  91.           parsetree => tree(label => moreps, lhs => X, rhs => Y)),
  92.     X:[],
  93.     paragraph(anaphora_in => A, anaphora_out => B, parsetree => Y)).
  94.     
  95. grammar_rule2(@,
  96.     s(form => X, parsetree => Y, anaphora_in => A, anaphora_out => B),
  97.     ss(form => X, parsetree => Y, anaphora_in => A, anaphora_out => B)).
  98.  
  99. grammar_rule(@,
  100.     s(form => conj(form => Z), anaphora_in => A, anaphora_out => C,
  101.       parsetree => tree(label => s, lhs => X, rhs => Y)),
  102.     ss(parsetree => X, form => Z, anaphora_in => A, anaphora_out => B),
  103.     cs(parsetree => Y, form => Z, anaphora_in => B, anaphora_out => C)).
  104.     
  105. grammar_rule(sconj,
  106.     cs(form => Z, parsetree => tree(label => cs, lhs => X, rhs => Y),
  107.        anaphora_in => A, anaphora_out => B),
  108.     X:sconj,
  109.     s(parsetree => Y, form => Z, anaphora_in => A, anaphora_out => B)).
  110.     
  111. grammar_rule({art;n;adj;pn;pron},
  112.     ss(form => statement, anaphora_in => A, anaphora_out => C,
  113.        parsetree => tree(label => ss, lhs => X, rhs => Y)),
  114.     np(number => N, class=>T, anaphora_in => A, anaphora_out => B, parsetree => X),
  115.     vp(number => N, subject=>T, anaphora_in =>B, anaphora_out => C, parsetree => Y)).
  116.  
  117. grammar_rule2({iv;tv},
  118.     ss(form => command, anaphora_in => A, anaphora_out => B,
  119.        parsetree => tree(label => ss, lhs => [you], rhs => X)),
  120.     vp(subject => you, tense => present, number => plural, parsetree => X,
  121.        anaphora_in => A, anaphora_out => B)).
  122.  
  123. grammar_rule(adv,
  124.     ss(form => statement, anaphora_in => A, anaphora_out => B,
  125.        parsetree => tree(label => ss, lhs => X, rhs => Y)),
  126.     X:adv,
  127.     ss(parsetree => Y, anaphora_in => A, anaphora_out => B)).
  128.  
  129. grammar_rule(modal,
  130.     ss(form => modal_question, anaphora_in => A, anaphora_out => C,
  131.        parsetree => tree(label => ss, lhs => X, rhs => Y)),
  132.     mnp(class=>S, parsetree => X, anaphora_in => A, anaphora_out => B),
  133.     vp(number => plural, tense => present, subject=>S, parsetree => Y,
  134.           anaphora_in => B, anaphora_out => C)).
  135.  
  136. grammar_rule2(interj,
  137.     ss(form => interj, parsetree => tree(label => ss, lhs => X),
  138.            anaphora_in => A, anaphora_out => A),
  139.     X:interj).
  140.  
  141. grammar_rule(interj,
  142.     ss(form => interj, parsetree => tree(label => ss, lhs => X, rhs => Y),
  143.        anaphora_in => A, anaphora_out => B),
  144.     X:interj, 
  145.     s(parsetree => Y, anaphora_in => A, anaphora_out => B)).
  146.  
  147. grammar_rule(modal,
  148.     mnp(class => T, parsetree => tree(label => np, rhs => X, lhs => Y),
  149.            anaphora_in => A, anaphora_out => B),
  150.     X:modal,
  151.     snp(class => T, parsetree => Y, anaphora_in => A, anaphora_out => B)).
  152.  
  153. grammar_rule2({art;adj;n;pn;pron},
  154.     np(number => N, class => T, parsetree => tree(label => np, lhs => X),
  155.        anaphora_in => A, anaphora_out => B),
  156.     snp(number => N, class => T, parsetree => X,
  157.         anaphora_in => A, anaphora_out => B)).
  158.     
  159. grammar_rule({art;adj;n;pn;pron},
  160.     np(number => @, class => @, anaphora_in => A, anaphora_out => C,
  161.        parsetree => tree(label => np, lhs => X, rhs => Y)),
  162.     snp(parsetree => X, anaphora_in => A, anaphora_out => B),
  163.     cnp(parsetree => Y, anaphora_in => B, anaphora_out => C)).
  164.  
  165. grammar_rule(art,
  166.     snp(number => N, class => T, anaphora_in => A, anaphora_out => B,
  167.         parsetree => tree(label => snp, lhs => X, rhs => Y)),
  168.     X:art(number => N),
  169.     ssnp(number => N, class => T, parsetree => Y, anaphora_in => A, 
  170.          anaphora_out => B)).
  171.  
  172. grammar_rule2({adj;n},
  173.     snp(number => N, class => T, anaphora_in => A, anaphora_out => B,
  174.         parsetree => tree(label => snp, lhs => X)),
  175.     ssnp(number => N, class => T, parsetree => X, anaphora_in => A, 
  176.         anaphora_out => B)).
  177. % N should be plural above, but breaks eating function calls then.
  178.  
  179. grammar_rule2(pn,
  180.     snp(number => N, class => T, anaphora_in => A, anaphora_out => [X|A],
  181.         parsetree => tree(label => snp, lhs => X)),
  182.     X:pn(number => N, class => T)).
  183.     
  184. grammar_rule2(pron,
  185.     snp(number => N, class => T, anaphora_in => A, anaphora_out => A,
  186.         parsetree => tree(label => snp, lhs => Anaphor)),
  187.     pron(number => N, gender => G, class => T)) :-
  188.    member(Anaphor:@(number => N, gender => G, class => T), A).
  189.     
  190. grammar_rule2(n, 
  191.     ssnp(number => N, class => T, anaphora_in => A, anaphora_out => [X|A],
  192.          parsetree => tree(label => ssnp, lhs => X)),
  193.     X:n(number => N, class => T)).
  194.  
  195. grammar_rule(n,
  196.     ssnp(number=>N, class => T, anaphora_in => A, anaphora_out => B,
  197.          parsetree => tree(label => ssnp, lhs => X, rhs => Y)),
  198.     X:n(number=>N, class => T),
  199.     pp(parsetree => Y, anaphora_in => [X|A], anaphora_out => B)).
  200.  
  201. grammar_rule(adj,
  202.     ssnp(number => N, class => T, anaphora_in => A, anaphora_out => B,
  203.          parsetree => tree(label => ssnp, lhs => X, rhs => Y)),
  204.     X:adj, 
  205.     ssnp(number=>N, class => T, parsetree => Y, anaphora_in => A,
  206.              anaphora_out => B)).
  207.  
  208. grammar_rule(conj,
  209.     cnp(parsetree => tree(label => cnp, lhs => X, rhs => Y),
  210.         anaphora_in => A, anaphora_out => B),
  211.     X:conj,
  212.     np(parsetree => Y, anaphora_in => A, anaphora_out => B)).
  213.  
  214. grammar_rule2({iv;tv;modal;adv},
  215.     vp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => B,
  216.        parsetree => tree(label => vp, lhs => X)),
  217.     svp(number => N, subject => S, tense => T, parsetree => X, anaphora_in => A, 
  218.             anaphora_out => B)).
  219.  
  220. grammar_rule({iv;tv;modal;adv},
  221.     vp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => C,
  222.        parsetree => tree(label => vp, lhs => X, rhs => Y)),
  223.     svp(parsetree => X, number => N, subject => S, tense => T, 
  224.         anaphora_in => A, anaphora_out => B),
  225.     cvp(parsetree => Y, number => N, subject => S, tense => T,
  226.         anaphora_in => B, anaphora_out => C)).
  227.  
  228. grammar_rule2(iv,
  229.     svp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => A,
  230.         parsetree => tree(label => svp, lhs => X)),
  231.     X:iv(number => N, subject => S, tense => T)).
  232.  
  233. grammar_rule(iv,
  234.     svp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => B,
  235.         parsetree => tree(label => svp, lhs => X, rhs => Y)),
  236.     X:iv(number => N, subject => S, tense => T),
  237.     pp(parsetree => Y, anaphora_in => A, anaphora_out => B)).
  238.  
  239. grammar_rule(iv,
  240.     svp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => A,
  241.         parsetree => tree(label => svp, lhs => X, rhs => Y)),
  242.     X:iv(number => N, subject => S, tense => T),
  243.     Y:adv).
  244.  
  245. grammar_rule(tv,
  246.     svp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => B,
  247.         parsetree => tree(label => svp, lhs => X, rhs => Y)),
  248.     X:tv(number => N, subject => S, object => O, tense => T),
  249.     np(class=>O, parsetree => Y, anaphora_in => A, anaphora_out => B)).
  250.  
  251. grammar_rule(modal,
  252.     svp(number => @, subject => S, tense => modal, anaphora_in => A, 
  253.             anaphora_out => B,
  254.         parsetree => tree(label => svp, lhs => X, rhs => Y)),
  255.     X:modal,
  256.     svp(subject => S, number => plural, tense => present, parsetree => Y,
  257.             anaphora_in => A, anaphora_out => B)).
  258.  
  259. grammar_rule(adv,
  260.     svp(number => N, subject => S, anaphora_in => A, 
  261.             anaphora_out => B,
  262.         parsetree => tree(label => svp, lhs => X, rhs => Y)),
  263.     X:adv,
  264.     svp(subject => S, number => N, tense => past, parsetree => Y,
  265.             anaphora_in => A, anaphora_out => B)).
  266.  
  267. grammar_rule(tv,
  268.     svp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => B,
  269.         parsetree => tree(label => svp, lhs => X, rhs => Y)),
  270.     X:tv(number => N, subject => S, object=>O, tense => T),
  271.     ppandnp(class=>O, parsetree => Y, anaphora_in => A, anaphora_out => B)).
  272.  
  273. grammar_rule(prep,
  274.     ppandnp(class => S, anaphora_in => A, anaphora_out => C,
  275.             parsetree => tree(label => ppandnp, lhs => X, rhs => Y)),
  276.     pp(parsetree => X, anaphora_in => A, anaphora_out => B),
  277.     np(class => S, parsetree => Y, anaphora_in => B, anaphora_out => C)).
  278.  
  279. grammar_rule(conj,
  280.     cvp(number => N, subject => S, tense => T, anaphora_in => A, anaphora_out => B,
  281.         parsetree => tree(label => cvp, lhs => X, rhs => Y)),
  282.     X:conj,
  283.     vp(parsetree => Y, number => N, subject => S, tense => T,
  284.         anaphora_in => A, anaphora_out => B)).
  285.  
  286. grammar_rule(prep, 
  287.     pp(parsetree => tree(label => pp, lhs => X, rhs => Y,
  288.            anaphora_in => A, anaphora_out => B)),
  289.     X:prep,
  290.     np(parsetree => Y, anaphora_in => A, anaphora_out => B)).
  291.  
  292. % The Lexicon:
  293.  
  294. dictionary(flu, n(number => singular, class => disease)).
  295. dictionary(malaria, n(number => singular, class => human_disease)).
  296. dictionary(frisbee, n(number => singular, class => frisbee)).
  297. dictionary(monk, n(number => singular, class => human(eating_habit => vegetarian))).
  298. dictionary(monks, n(number => plural, class => human(eating_habit => vegetarian))).
  299. dictionary(lunch, n(number => singular, class => food)).
  300. dictionary(lunches, n(number => plural, class => food)).
  301. dictionary(steak, n(number => singular, class => meat)).
  302. dictionary(steaks, n(number => plural, class => meat)).
  303. dictionary(salad, n(number => singular, class => vegetable)).
  304. dictionary(file, n(number => singular, class => information)).
  305. dictionary(files, n(number => plural, class => information)).
  306. dictionary(compiler, n(number => singular, 
  307.                class => code(eating_habit => comp))).
  308. dictionary(pascal, n(number => singular, 
  309.                class => code(eating_habit => comp))).
  310. dictionary(computer, n(number => singular, 
  311.                class => computer(eating_habit => comp))).
  312. dictionary(computers, n(number => plural, 
  313.             class => computer(eating_habit => comp))).
  314.  
  315. dictionary(mary, pn(number => singular, gender => female, 
  316.             class => human(eating_habit => omnivore))).
  317. dictionary(john, pn(number => singular, gender => male,
  318.             class => human(eating_habit => omnivore))).
  319. dictionary(pascal, pn(number => singular, gender => male,
  320.               class => human(eating_habit => omnivore))).
  321. dictionary(spot, pn(number => singular, 
  322.            class => dog(eating_habit => carnivore))).
  323. dictionary(life, pn(number => singular, gender => neuter,
  324.            class => code(eating_habit => comp))).
  325.  
  326. dictionary(she, pron(number => singular, gender => female, class => human)).
  327. dictionary(he, pron(number => singular, gender => male, class => human)).
  328. dictionary(it, pron(number => singular, gender => male, class => {inanimate;inhuman})).
  329.  
  330. dictionary(walks, iv(subject => animate, number => singular, tense => present)).
  331. dictionary(walk, iv(subject => animate, number => plural, tense => present)).
  332. dictionary(walked, iv(subject => animate, tense => past)).
  333. dictionary(runs, iv(subject => animate, number => singular, tense => present)).
  334. dictionary(run, iv(subject => animate, number => plural, tense => present)).
  335. dictionary(ran, iv(subject => animate, tense => past)).
  336. dictionary(compile, iv(subject => animate, number => plural, tense => present)).
  337. dictionary(compiles, iv(subject => animate, number => singular, tense => present)).
  338. dictionary(compiled, iv(subject => animate, tense => past)).
  339.  
  340. dictionary(compile, tv(subject => animate, number => plural, object => file,
  341.                tense => present)).
  342. dictionary(compiles, tv(subject => animate, number => singular, object => file,
  343.             tense => present)).
  344. dictionary(compiled, tv(subject => animate, object => file, tense => past)).
  345. dictionary(delete, tv(subject => animate, number => plural, object => file,
  346.                tense => present)).
  347. dictionary(deletes, tv(subject => animate, number => singular, object => file,
  348.                tense => present)).
  349. dictionary(deleted, tv(subject => animate, object => file, tense => past)).
  350. dictionary(eat, tv(subject => animate(eating_habit => EH), 
  351.             object => {food;information} & eaten_by(EH),
  352.             number => plural, tense => present)).
  353. dictionary(eats, tv(subject => animate(eating_habit => EH),
  354.             object => {food;information} & eaten_by(EH),
  355.             number => singular, tense => present)).
  356. dictionary(ate, tv(subject => animate(eating_habit => EH), 
  357.             object => {food;information} & eaten_by(EH),
  358.             tense => past)).
  359. dictionary(catch, tv(subject => animate & can_catch(Y),
  360.                      object => Y:{projectile; disease},
  361.              number => plural, tense => present)).
  362. dictionary(catches, tv(subject => animate & can_catch(Y),
  363.                        object => Y:{projectile; disease},
  364.                number => singular, tense => present)).
  365. dictionary(caught, tv(subject => animate & can_catch(Y),
  366.                        object => Y:{projectile; disease},
  367.                tense => past)).
  368.  
  369. dictionary(my, art(class => human)).
  370. dictionary(her, art(class => human)).
  371. dictionary(his, art(class => human)).
  372. dictionary(its, art).
  373. dictionary(the, art).
  374. dictionary(a, art(number => singular)).
  375. dictionary(some, art(number => plural)).
  376. dictionary(many, art(number => plural)).
  377.  
  378. dictionary(small, adj).
  379. dictionary(big, adj).
  380. dictionary(fast, adj).
  381. dictionary(slow, adj).
  382. dictionary(bad, adj).
  383. dictionary(good, adj).
  384.  
  385. dictionary(did, modal).
  386. dictionary(should, modal).
  387. dictionary(could, modal).
  388. dictionary(would, modal).
  389. dictionary(can, modal).
  390. dictionary(will, modal).
  391.  
  392. dictionary(and, conj).
  393. dictionary(or, conj).
  394.  
  395. dictionary(and, sconj).
  396. dictionary(but, sconj).
  397. dictionary(or, sconj).
  398.  
  399. dictionary(slowly, adv).
  400. dictionary(quickly, adv).
  401. dictionary(correctly, adv).
  402.  
  403. dictionary(in, prep).
  404. dictionary(over, prep).
  405. dictionary(from, prep).
  406. dictionary(of, prep).
  407. dictionary(to, prep).
  408. dictionary(who, prep).
  409. dictionary(with, prep).
  410. dictionary(which, prep).
  411.  
  412. dictionary(what, wh_word).
  413. dictionary(when, wh_word).
  414. dictionary(why, wh_word).
  415. dictionary(who, wh_word).
  416. dictionary(how, wh_word).
  417.  
  418. dictionary(hey, interj).
  419. dictionary(how, interj).
  420. dictionary(why, interj).
  421. dictionary(yes, interj).
  422. dictionary(no, interj).
  423.  
  424. dictionary([], []).
  425.  
  426. lookup([], []).
  427. lookup([Word|Rest], [Def|More]) :-
  428.     dictionary(Word, Def),
  429.     lookup(Rest, More).