Building a Prolog query and response system

I am studying the prologue and I am stuck with a problem. I ask a question and an answer. For example, when I type "Vehicle color blue". The program will say "OK" and add this new rule, so when asked, "What is the color of the car?" He will answer in blue.
If I say that "the color of the car is green", he will answer "It is not." But whenever I type "Car color blue" it returns true, false for the question version. Can anyone guide where to go next? I don't know how to get the program to say "its blue" or something

 input :-
    read_line_to_codes(user_input, Input),
    string_to_atom(Input,Atoms),
    atomic_list_concat(Alist, ' ', Atoms),
    phrase(sentence(S), Alist),    
    process(S).

statement(Statement) --> np(Description), np(N), ap(A),
{ Statement =.. [Description, N, A]}.

query(Fact) -->  qStart, np(A), np(N),
 { Fact =.. [A, N, X]}.


np(Noun) --> det, [Noun], prep.
np(Noun) --> det, [Noun].

ap(Adj) --> verb, [Adj].
qStart --> adjective, verb.

vp --> det, verb.   

adjective --> [what].
det --> [the].

prep --> [of].

verb -->[is].


%% Combine grammar rules into one sentence
sentence(statement(S)) --> statement(S).
sentence(query(Q)) --> query(Q).
process(statement(S)) :- asserta(S).
process(query(Q))     :- Q.

      

+3


source to share


1 answer


You are really very close. Take a look at this:

?- phrase(sentence(Q), [what,is,the,color,of,the,car]).
Q = query(color(car, _6930)) ;
false.

      

You have successfully parsed the offer in your request. Now, let's process it:

?- phrase(sentence(Q), [what,is,the,color,of,the,car]), process(Q).
Q = query(color(car, 'blue.')) ;
false.

      

As you can see, you have merged correctly. When you finished, you didn't do anything about it. I think all you have to do is pipe the result process/1

to something to display the result:



display(statement(S)) :- format('~w added to database~n', [S]).
display(query(Q)) :- Q =.. [Rel, N, X], format('the ~w has ~w ~w~n', [N, Rel, X]).

      

And change input/0

to go to the predicate display/1

:

input :-
    read_line_to_codes(user_input, Input),
    string_to_atom(Input,Atoms),
    atomic_list_concat(Alist, ' ', Atoms),
    phrase(sentence(S), Alist),    
    process(S),
    display(S).

      

Now you get some results when you use it:

?- phrase(sentence(Q), [what,is,the,color,of,the,car]), process(Q), display(Q).
the car has color blue.
Q = query(color(car, 'blue.')) ;
false.

?- phrase(sentence(Q), [the,siding,of,the,car,is,steel]), process(Q), display(Q).
siding(car,steel) added to database
Q = statement(siding(car, steel)) ;
false.

?- phrase(sentence(Q), [what,is,the,siding,of,the,car]), process(Q), display(Q).
the car has siding steel
Q = query(siding(car, steel)) ;
false.

      

+1


source







All Articles