getUserReply(R):- readSentence(R).
readSentence(Words) :- get0(C), read_rest(C,Words). read_rest(46,['.']) :- !. read_rest(63,['?']) :- !.
read_rest(C,Words) :- ( C=32 ; C=10 ) , !, get0(C1), read_rest(C1,Words).
read_rest(44,[','|Words]) :- !, get0(C1), read_rest(C1,Words).
read_rest(C,[Word|Words]) :- lower_case(C,LC), % we will need this because we concat_atoms and we will end up with variables unless we use lower case read_word(LC,Chars,Next), name(Word,Chars), read_rest(Next,Words).
read_word(C,[],C) :- ( C=32 ; C=44 ; C=10 ; C=46 ; C=63 ) , !.
read_word(C,[LC|Chars],Last) :- lower_case(C,LC), get0(Next), read_word(Next,Chars,Last).
lower_case(C,C) :- ( C < 65 ; C > 90 ) , !. lower_case(C,LC) :- LC is C + 32.
/* * * default answers of the expert systems * 1) no * 2) yes * 3) I understand * */
:- op(100, xfx, 'is_a'). % this operator will be used to store data from the statements in the knowledge database :- op(100, xfx, 'is_really'). % this operator will be used for evaluating the questions
Who is_a What :- %writeln('who = '),writeln(Who), %writeln('what= '),writeln(What), ( (is_list(Who) -> concat_atom(Who,'_' ,K) ; K=Who), (is_list(What) -> concat_atom(What,'_',V) ; V=Who) ), Fact =.. [fact,V,K], system_mode(on), redefine_system_predicate('$fact'(_,_)), dynamic('fact'/2), assertz(Fact).
Who is_really What:-% this where the question is evaluated and an answer in the form YES/NO is given writeln('who = '),writeln(Who), writeln('what= '),writeln(What), ( (is_list(Who) -> concat_atom(Who,'_' ,K) ; K=Who), (is_list(What) -> concat_atom(What,'_',V) ; V=What) ), Fact =.. [fact,K,V],
Fact1 =.. [fact,K,X], % transitivity Fact2 =.. [fact,X,V], write('checking -> '),write(Fact), dynamic('fact'/2), ( ( ( call(Fact) ; ( call(Fact1),call(Fact2),asserta(Fact) )%some memoization ), writeln('YES'),!,fail ); writeln('NO'),!,fail ).
s --> object(N,M) , [is] , [a] ,object(X,Y) , [.] ,{[N,M] is_a [X,Y]}. s --> object(Who) , [is] , [a] ,object(X,Y) , [.] ,{Who is_a [X,Y]}. s --> object(A,B) , [is] , det(_) , noun([Y]) , [of] , object(C,D) , [.] , { [A,B] is_a [Y,C,D] }. s --> object(Who) , [is] , [a] ,object(What) , [.] ,{Who is_a What}. s --> object(N,M) , [is] , [a] , level(X) , noun(Y),[.],{ [level,X] is_a [N,M] , write('last rule') }. s --> [all] , num(X) , [courses] , [are] , adj([P]) , [courses] , [.] , { [P,course] is_a [level,X] }.
q --> [is] , object(N,M) , det(X) , adj([P]) , noun([Q]) , [?] , { [N,M] is_really [P,Q]}. q --> [does] , object(N,M) , [have] , [a] , noun([Q]) , [?] , { [Q,N,M] is_really H }.
object(X,Y) --> [X] , [Y] , { adj(X) , noun(Y) }. object(X,Y) --> [X] , [Y] , { number(Y) }. num(X) --> [X], { number(X) }.
object(X) --> [X].
det([X]) --> [X] , { det(X) }. noun([X]) --> [X] , { noun(X) }. adj([X]) --> [X] , { adj(X) }.
level(X) --> [X] , [level] , { number(X) }.
adj(advanced). adj(basic). adj(computing). noun(course). noun(courses). noun(level). verb(is). verb(are). verb(have). det(the). det(a). det(an). %number % predicate to be used when in need of numeral noun(prerequisite). pronoun(all).
% % to end the conversation with the expert system use "end." % % the system will write messages if what the user has written was a question or statement % and it will reply nothing otherwise(error) %
start:- repeat, getUserReply(X), write(X), ( (X =@= [ 'end','.' ] , !); (last(X,'.'),s(X,[]),writeln('[I understand]')); (last(X,'?'),q(X,[])) ), fail.
%statement tests test1:- s([com,400,is,a,computing,course,.],[]). test2:- s([com,401,is,a,400,level,course,.],[]). test3:- s([com,270,is,the,prerequisite,of,com,401,.],[]). test4:- s([all,400,courses,are,advanced,courses,.],[]).
%question tests test5:- test1, q([is,com,401,an,advanced,course,?],[]).
test6:- test2, test4, q([is,com,401,an,advanced,course,?],[]). test7:- test3, q([does,com,401,have,a,prerequisite,?],[]).
|