You are on page 1of 1

%% %% %% %% %% %%

COMP304 2012 Prolog Project 1 Rudi Theunissen ID: 300259174 Question 1

%% Starts Dr. Roberts start:write('how do you feel today?'), nl, ask.

% Initial message to start of the 'session' % Initiate request / response cycle

%% Reads input from user and initiates response ask :read(Input), % Read a line from standard input Input \== end_of_file, % Check if input was end of file -> end name(Input, Codes), % Converts the input string to ASCII codes split(Codes, Question), % Call to split the ASCII codes into words reply(Question), % Calls to reply to the question ask. % Recursively ask the user for a reponse %% Splits an ASCII code string into words split([], []) :- !. split(Codes, [Word|Result]) :split(Codes, Rest, Characters), name(Word, Characters), split(Rest, Result). split([], [], []) :- !. split([46|_], [], []) :- !. split([32|T], T, []) :- !. split([H|T], Rest, [H|List]) :split(T, Rest, List).

% No codes left, base case % Identify first word % Turn codes into a word % Move onto rest of codes % No codes left, end of word % Full-stop, end of word % Space, end of word % If not the end of a word, % Add code to output list and recurse

%% Calls to answer the question and print the reply reply(Question) :answer(Question, Answer), % Call to answer the question printReply(Answer). % Print the reply %% Answers the question answer(Question, Answer) :match(Question, Keyword), keyword(Keyword, Prefix), transform(Question, Response), append(Prefix, Response, Answer). %% When no keyword was found in the question answer(_, Answer) :X is random(3), fallback(X, Answer). fallback(0, [how, does, that, make, you, feel]). fallback(1, [can, you, elaborate, on, that]). fallback(2, [what, else, is, on, your, mind]). %% Finds a keyword match in the question match([], _) :- fail. match([Word|_], Word):- keyword(Word, _). match([_|Rest], Result):- match(Rest, Result).

% % % %

Find a matching keyword Match the keyword to prefix Transform the question Append transformation to prefix

% Fallback question to invoke response

% No keyword match % Check if current word is a keyword match % Current word did not succeed, check the rest

%% Transforms the perspective of the question transform([], []). transform([Word|Rest], [Replacement|Result]) :- % List of words, prepend replacement to result replace(Word, Replacement), % Replace the word with its transformation transform(Rest, Result). % Transform the rest of the list

%% Prints the reply, question mark, and new line. printReply(Reply) :printSentence(Reply), % Print the response sentence write('?'), nl. % Follow with question mark and new line printSentence([]). printSentence([W|[]]) :write(W). printSentence([W|R]) :write(W), write(' '), printSentence(R). %% Replacement key-value pairs replace(my, your). replace(i, you). replace(me, you). replace(makes, make). replace(you, me). replace(am, are). replace(myself, yourself). %% No replacement was found, word stays the same replace(Word, Word). %% Keyword key-value pairs with respective prefixes keyword(feel, [what, makes]). keyword(have, [are, you, sure]). keyword(know, [are, you, sure]). keyword(need, [why, do]). keyword(like, [why, do]). keyword(love, [why, do]). keyword(hate, [why, do]). % No words left, done % Last word % Print the last word % % % % Not the last word Print word Print space Print the rest recursively