
:- consult(intrface).
:- consult(lexicon).
:- consult(grammar).
:- consult(synproc).
:- consult(semantic).
:- consult(events).

start :-
	nl, nl,
	write('Welcome to the Natural Language Query System!'), nl,
	write('Please ask a natural English question about '), nl,
	write('any inventor or invention, and the system will '), nl,
	write('try to answer you as well as possible.'),
	askQuestion.

askQuestion :-
	retractall(parseSemantics(_,_,_,_)),
	retractall(outputGoals(_,_,_,_)),
	asserta(parseSemantics(unknown, unknown, unknown, unknown)),
	asserta(outputGoals(no, no, no, no)),
	nl, nl,
	write('Please try to be as grammatical as possible.'),
	nl, nl,
	write('Your question:  '),
	(getInputQuestion(QuestionText), ! 		; write('Failed getInputQuestion!'), QuestionText = [], nl, nl),
	(produceChart(QuestionText), !		; write('Failed produceChart!'), nl, nl),
	nl,
	getResults(QuestionText).		% Continue in next stage of analysis


getResults(QuestionText) :-
	length(QuestionText, QuestionLength),
	chart(sentence, 0, QuestionLength, []), !,	% Check that a sentence was successfully parsed
	(getParseTree(sentence, 0, QuestionLength, ParseTree), !	; write('Failed getParseTree!'), ParseTree = [], nl, nl),
	(makeQuery, ! 								; write('Failed makeQuery!')),
	nl, nl, nl,
	write('Would you like to see details about how your question was analysed? (Y/N)  '),
	get_single_char(Details),
	checkDetails(Details, QuestionText, ParseTree),
	write('Would you like to ask again? (Y/N)  '),
	get_single_char(Response), 
	checkResponse(Response).
getResults(_) :- 
	write('I am sorry, but the question you asked could not be understood.'), nl, nl,
	write('Please remember to use natural English questions,'), nl,
	write('and to restrict the content of your question to specific'), nl,
	write('inventors, inventions, dates of invention and invention locations.'), nl, nl,
	write('Would you like to ask again? (Y/N)  '),
	get_single_char(Response),
	checkResponse(Response).

checkResponse(Response) :-
	(Response == 89
	; Response == 121),
	!, nl, nl, nl, nl, nl, nl, nl,
	askQuestion.
checkResponse(Response) :-
	(Response == 78
	; Response == 110), !.
checkResponse(_) :-
	nl, nl,
	write('Would you like to ask again? (Y/N)  '),
	get_single_char(Response),
	checkResponse(Response).

checkDetails(Response, QuestionText, ParseTree) :-
	(Response == 89
	; Response == 121),
	!,
	nl, nl,
	write('The recognized word list is: '), nl,
	write(QuestionText), nl,
	write('     '), analyseChart(QuestionText),
	nl, nl,
	write('The parse tree for your question is: '), nl, 
	write('     '), write(ParseTree),
	nl, nl,
	parseSemantics(Actor, Object, Date, Env),
	write('The following semantic information was obtained: '), nl,
	write('     Actor    : '), write(Actor), nl,
	write('     Object   : '), write(Object), nl,
	write('     Date     : '), write(Date), nl,
	write('     Location : '), write(Env), nl, nl.
checkDetails(Response,_,_) :-
	(Response == 78
	; Response == 110), 
	!,
	nl, nl.
checkDetails(_, QuestionText, ParseTree) :-
	nl, nl,
	write('Would you like to see more details? (Y/N)  '),
	get_single_char(Details),
	checkDetails(Details, QuestionText, ParseTree).

	

