%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AIPP Practical 8
% 2004, Week 9
% Morphology and ELIZA

:- use_module(library(lists)).

% This file contains the original code for generate_morph/3 and ELIZA. 
% You will be instructed to insert new predicates into the allocated
% spaces and modify some existing code.

% Follow the guidelines for practical 8 on pg 157 of the coursenotes.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% starts/2








%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% plural/2













/* Write the second version of plural here. This take a word file as input and write the pluralised words to the screen. The input file should contain a single word on each line followed by a full-stop

e.g.
cake.
xylophone.
knife.
box.
spy.
terminal.

Remember that you can use end_of_file to indicate when you have read all the contents. */










 
   

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% generate_morph/3

% Insert your new code here.






% Below is the generate_morph/3 predicate as defined in the lecture. 
% You don't need to modify this, just add new basecases above.

generate_morph(Baseform,Suffix,Derivedform):-
	name(Baseform,BaseformChs),
	name(Suffix,SuffixChs),
	morph(BaseformChs,SuffixChs,DerivedformChs),
	name(Derivedform,DerivedformChs).

morph("fe","s","ves"):-!.
morph("e","ed","ed"):-!.
morph([],Suffix,Suffix).

morph([H|T],Suffix,[H|L2]):-
	morph(T,Suffix,L2).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ELIZA
% This is the original version of ELIZA. You should run this code as it is
% and then modify it as instructed in the practical.

go:-
	read_in(Input),
        process(Input).

process([W1,_]):-
      member(W1, [bye, goodbye, quit, halt, chow]),
      write('I hope I was of some help.'), nl.

process(Input):-
      build_response(Input, Output),	
      reply(Output), nl,
      go.

build_response(Input, Output):-
       rule(Input, Output).

% rule/2

rule([Greeting|Rest],	 [hi|Rest]):- member(Greeting, [hi, hello]).
rule([i,hate,X,'.'],     [do, you, really, hate, X, ?]).
rule([_,you,Y,me,'?'],   [why, do, me, ask, if, 'I', Y, you, ?]).
rule([i,like,X,'.'],     [does, anyone, else, in, your, family, like, X,?]).
rule([are, you, X,'?'],  [what, makes, me, think, 'I', am, X, ?]).
rule([you, are, X, '.'], [does, it, please, you, to, believe, 'I', am,X,?]).
rule(_,                  [please, go, on, '.']).

% Input/Output routines:
%
% reply/1
%

reply([FirstWord|Rest]):-
      reply0([FirstWord|Rest]).

/* reply([FirstWord|Rest]):-
      capitalize(FirstWord, CapitalizedFirstWord),
      reply0([CapitalizedFirstWord|Rest]). */

reply0([]):- !.

reply0([X,Y]) :-
      member(Y,[?,'.',!]),
      !,
      write(X),
      write(Y),
      nl.

reply0([FirstWord|Rest]) :-
      write(FirstWord),
      tab(1),
      reply0(Rest).

% Read in a sentence:

% **** The rest of the code performs in the input and output for 
% ELIZA. You do not need to understand all this to complete the practical.

read_in([W|Ws]):-
      get0(C),
      readword(C,W,C1),
      restsent(W,C1,Ws).

% Given a word and the character after it, read in the
% rest of the sentence:

restsent(W,_,[]):-
      lastword(W), !.

restsent(_,C,[W1|Ws]):-
      readword(C,W1,C1),
      restsent(W1,C1,Ws).

% Read in a single word, given an initial character, and remembering
% what character came after the word:

readword(C,W,C1):-
      single_character(C), !,
      name(W,[C]),
      get0(C1).

readword(C,W,C2):-
      in_word(C, NewC), !,
      get0(C1),
      restword(C1,Cs,C2),
      name(W,[NewC|Cs]).

readword(_,W,C2):-
      get0(C1),
      readword(C1,W,C2).

restword(C,[NewC|Cs],C2):-
      in_word(C,NewC), !,
      get0(C1),
      restword(C1,Cs,C2).

restword(C,[],C).

% These characters form words on their own:

single_character(44).      % ','
single_character(59).      % ';'
single_character(58).      % ':'
single_character(63).      % '?'
single_character(33).      % '!'
single_character(46).      % '.'

% These characters can appear within a word:

in_word(C,C):-
      C > 96,
      C < 123.

in_word(C,L):-
      C > 64,
      C < 91,
      L is C+32.

in_word(C,C):-
      C > 47,
      C < 58.

in_word(39,39).
in_word(45,45).

% These words terminate a sentence:

lastword('.').                                                          
lastword('!').
lastword('?').
