Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gbmele/ae4ae2ee411ed018b74b1ea2e61cc557 to your computer and use it in GitHub Desktop.
Save gbmele/ae4ae2ee411ed018b74b1ea2e61cc557 to your computer and use it in GitHub Desktop.
A simple English-to-Prolog translator, based on http://stackoverflow.com/a/21196849/975097
%This is a basic English-to-Prolog translator. It uses a
%pathfinding algorithm to find the meaning of each sentence.
%For example, output([a,is,greater,than,B],X) means X = (a > b).
%The input is an English phrase. The output is a Prolog expression.
output(Input,Output) :-
is_output(Output),
means(Input,Output).
means(A,B) :-
walk(A,B,[]).
walk(A,B,V) :-
means_(A,X),
not(member(X,V)),
(B = X;
walk(X,B,[A|V])).
means_(A,B) :- edge(A,B);edge(B,A).
edge([A,equals,B],[A,'=',B]).
edge([A,equals,B], [A,is,equal,to,B]).
edge([A,does,not,equal,B],[A,is,not,equal,to,B]).
edge([A,is,not,more,than,B],[A,is,not,greater,than,B]).
edge([A,to,the,power,of,B],[A,'^',B]).
edge([A,equals,B],[A,is,the,same,as,B]).
edge([A,<,B],[B,>,A]).
edge((A,<,B),[B,>,A]).
edge([X,contains,Y],[Y,is,a,member,of,X]).
edge([X,contains,Y],member(Y,X)).
edge([A,is,less,than,B],[B,is,greater,than,A]).
edge([B,is,greater,than,A],[B,>,A]).
edge((B > A),[B,>,A]).
edge([translate,A,from,B,to,C],[translate,A,to,C,from,B]).
edge(greater,more).
edge(bigger,larger).
edge(smaller,tinier).
edge(big,large).
edge([A,equals,B],[B,equals,A]).
edge([X,is,not,the,same,as,Y],[X,does,not,equal,Y]).
edge([A,is,not,true],A = false).
edge([A,is,not,false],A = true).
edge([A,is,true],A = true).
edge([A,is,false],A = false).
edge([A,is,true],(A = true)).
edge([A,is,false],(A = false)).
edge([A,squared],[A,times,A]).
edge([X,times,Y],X*Y).
edge([X,plus,Y],X+Y).
edge(X*Y,Y*X).
edge([X,divided,by,Y],(X / Y)).
edge([X,cubed],X*X*X).
edge([the,sum,of,A,and,B],A+B).
edge(A+B,B+A).
edge([the,quotient,of,A,and,B],A / B).
edge([A,and,B],(A,B)).
edge([A,or,B],(A;B)).
edge([A,is,between,B,and,C],between(A,B,C)).
edge([A,=,B],(A=B)).
edge([A,is,more,than,B],[A,is,greater,than,B]).
edge([A,is,an,integer],integer(A)).
edge([A,is,a,number],number(A)).
edge([A,multiplied,by,B],[A,times,B]).
is_output(_,>,_).
is_output(_ < _).
is_output(_ > _).
is_output(_ + _).
is_output(_ * _).
is_output(integer(_)).
is_output(number(_)).
is_output(nonvar(_)).
is_output(_ * _ * _).
is_output(_ / _).
is_output(_ - _).
is_output(_ = false).
is_output(_ = true).
is_output(member(_,_)).
is_output((_,_)).
is_output((_;_)).
is_output(_ = _).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment