Posted By

philkav on 12/18/09


Tagged


Versions (?)

AI Assignment - Monthy Python Skit


 / Published in: Prolog
 

  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %% What have the romans ever done for us? %%%%
  3. %% Written by : Philip Kavanagh %%%%%
  4. %% DT228/4 - C06412882 %%%%%%
  5. %% Prologue Assignment December 2009 %%%%%%%
  6. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  7.  
  8.  
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. %% -- INSTRUCTIONS -- %%%
  11. %% Prolog will print a statement, prompts user for input. %%%%
  12. %% User enters a sentance and ends it with a full stop, %%%%%
  13. %% exclamation point or question mark. Prolog will then decide %%%%%%
  14. %% whether or not the answer is valid and will return it's decision. %%%%%%%
  15. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  16.  
  17. %%
  18. %% said(X) :- assert(X, "a", X), retract said(Y), assert said(X)
  19.  
  20. %% ----- Function called BY THE MAIN FUNCTION (See last line of code) the first time the program is run
  21. firstRun:-
  22. nl,write('REG : They bled us white, the bastards. They\'ve taken everything we had. And not just from us! From our fathers,and from our father\'s fathers.'),
  23. nl,write('LOR : And from our father\'s father\'s fathers.'),
  24. nl,write('REG : Yeah.'),
  25. nl,write('LOR : And from our father\'s father\'s father\'s fathers.'),
  26. nl,write('REG : Yeah, all right Stan, don\'t delay with the point. And what have they ever given us in return?'),
  27. nl,write('REG : What have the romans ever done for us?'),nl,run.
  28.  
  29. %% ----- this is the programs base
  30. run:-
  31. write('YOU : '),getSentance(_),nl,run.
  32.  
  33. %% ----- To end the program. (This may be incorrectly applied).
  34. quit:-
  35. break.
  36.  
  37.  
  38.  
  39. %% ----- read list and pick keywords -----
  40. %% ----- Search recursively through the list - If you find the word, cut out, otherwise search the remainder of the list.
  41. %% ----- exclamation mark '!' is used to cut.
  42. contains([aqueduct|_]) :- !,write('REG : Oh. Yeah, yeah, they did give us the aqueduct, ah, that\'s true,'),apartFrom,assert(#(aqueduct)),listing(#).
  43. contains([sanitation|_]) :- !,write('REG : Yeah, all right, I\'ll grant you the sanitation,'),apartFrom,assert(#(sanitation)),listing(#).
  44. contains([roads|_]) :- !,write('REG : Oh, yeah, obviously the roads. I mean the roads go without saying, don\'t they? '),apartFrom,assert(#(roads)),listing(#).
  45. contains([irrigation|_]) :- !,write('REG : Yeah, yeah, all right, fair enough - irrigation too... '),apartFrom,assert(#(irrigation)),listing(#).
  46. contains([medicine|_]) :- !,write('REG : Yes, ok - medicine! '),apartFrom,assert(#(medicine)),listing(#).
  47. contains([education|_]) :- !,write('REG : Well sure yeah, education I suppose? '),apartFrom,assert(#(education)),listing(#).
  48. contains([wine|_]) :- !,write('REG : Yes and the wine... '),apartFrom,assert(#(wine)),listing(#).
  49. contains([publicbaths|_]) :- !,write('REG : And the publicbaths, ok'),apartFrom,assert(#(publicbaths)),listing(#).
  50. contains([order|_]) :- !,write('REG : Brought order, very well '),apartFrom,assert(#(order)),listing(#).
  51. contains([peace|_]) :- !,write('REG : Oh, peace! Shut Up!'),nl,write('________________'),nl,nl,write('---End Scene---'),nl,write('________________'),nl,nl,quit.
  52. %% ----- recursion and NOT case
  53. contains([Head|Tail]) :- contains(Tail), !.
  54. contains(_) :- write('REG : The Romans had nothing to do with that!').
  55.  
  56.  
  57. apartFrom:-write('Well apart from the : ').
  58.  
  59.  
  60. %% ----- this function reads in the things and creates a list from them -----
  61. readIntoList1([Head|Tail]):-read(X),not(X=end),Head=X,readIntoList1(Tail).
  62. readIntoList1([]).
  63.  
  64. readIntoList2([Head|Tail]):-read(X),not(X=end),Head=X,readIntoList2(Tail).
  65. readIntoList2([]).
  66.  
  67.  
  68. %% ----- Here we are converting the list format to a proper output format -----
  69. %% ----- Instead of [this, is, a, list] beign displayed, it will say : this is a list
  70. writeSentance([H|T]):-name(H,[Fl|Lt]),Cap is Fl-32,name(X,[Cap|Lt]),write(X),writeSentanceTail(T).
  71. writeSentanceTail([]):-write('.').
  72. writeSentanceTail([H|T]):-write(', '),write(H),writeSentanceTail(T).
  73.  
  74.  
  75. %% ----- these functions are where we turn sentances into lists in order to process each individual word -----
  76. getSentance([W|Ws]):-get0(Char),readWord(Char,W,C1),restOfSentance(W,C1,Ws),contains([W|Ws]).
  77.  
  78. restOfSentance(W,_,[]):-lastword(W),!.
  79. restOfSentance(_,C,[W1|Ws]):-readWord(C,W1,C1),restOfSentance(W1,C1,Ws).
  80.  
  81. readWord(C,W,C1):-single_character(C),!,name(W,[C]),get0(C1).
  82. readWord(C,W,C2):-in_word(C,NewC),!,get0(C1),restOfWord(C1,Cs,C2),name(W,[NewC|Cs]).
  83. readWord(_,W,C2):-get0(C1),readWord(C1,W,C2).
  84.  
  85. restOfWord(C,[NewC|Cs],C2):-in_word(C,NewC),!,get0(C1),restOfWord(C1,Cs,C2).
  86. restOfWord(C,[],C).
  87.  
  88. %%---------------(ASCII) allow :
  89. single_character(44). %% comma ','
  90. single_character(58). %% colon ':'
  91. single_character(33). %% exclamation '!'
  92. single_character(59). %% semi-colon ';'
  93. single_character(63). %% question '?'
  94. single_character(46). %% full-stop '.'
  95.  
  96. %%-- The characters are between :
  97. in_word(C,C):-C > 96,C < 123. %% lowercase letters
  98. in_word(C,C):-C > 47,C < 58. %% Numbers
  99. in_word(39,39). %% single inverted comma
  100. in_word(45,45). %% hyphenation
  101. in_word(C,L):-C>64,C<91,L is C+32. %% uppercase letters
  102.  
  103. lastword('.').
  104. lastword('!').
  105. lastword('?').
  106. %% ----- end of sentance to list -----
  107.  
  108.  
  109.  
  110.  
  111. %% ----- will load when the program is run -----
  112. :-dynamic(#(X)),nl,firstRun.

Report this snippet  

You need to login to post a comment.