Merged other
This commit is contained in:
commit
506cd53192
97
polymani.pl
97
polymani.pl
@ -72,7 +72,7 @@ simpoly(P, S) :-
|
||||
*/
|
||||
scalepoly(P1, C, S) :-
|
||||
is_polynomial_valid_in_predicate(P1, "scalepoly"),
|
||||
is_number_in_predicate(C, "scalepoly"),
|
||||
is_number_valid_in_predicate(C, "scalepoly"),
|
||||
scale_polynomial(P1, C, S),
|
||||
!.
|
||||
%% Tests:
|
||||
@ -137,14 +137,14 @@ is_polynomial_as_list_valid_in_predicate(L, F) :-
|
||||
%@ Invalid polynomial in Test: a*4+0*x
|
||||
%@ false.
|
||||
|
||||
%% is_number_in_predicate(+C:number, +F:string) is det
|
||||
%% is_number_valid_in_predicate(+C:number, +F:string) is det
|
||||
%
|
||||
% Validates that C is a number or prints F and it then it
|
||||
%
|
||||
is_number_in_predicate(C, _) :-
|
||||
is_number_valid_in_predicate(C, _) :-
|
||||
number(C),
|
||||
!.
|
||||
is_number_in_predicate(C, F) :-
|
||||
is_number_valid_in_predicate(C, F) :-
|
||||
%% Writes the argument and fails
|
||||
write("Invalid number in "),
|
||||
write(F),
|
||||
@ -152,58 +152,42 @@ is_number_in_predicate(C, F) :-
|
||||
write(C),
|
||||
fail.
|
||||
|
||||
|
||||
|
||||
/*******************************
|
||||
* NLP *
|
||||
*******************************/
|
||||
|
||||
/* DCG */
|
||||
separator --> ["and"].
|
||||
separator --> ["by"].
|
||||
|
||||
command --> ["show"].
|
||||
command --> ["multiply"].
|
||||
command --> ["simplify"].
|
||||
command --> ["add"].
|
||||
command --> ["forget"].
|
||||
expression(A,B,C):-writeln("oi"),writeln(A),writeln(B),writeln(C),writeln("bye").
|
||||
instruction(Left_expr, Right_expr) --> command, expression(Left_expr), separator, expression(Right_expr).
|
||||
|
||||
%% polyplay() is det
|
||||
%
|
||||
% Interactive prompt for the NLP Interface
|
||||
%
|
||||
polyplay :-
|
||||
write("> "),
|
||||
prompt(Old, '> '),
|
||||
read_string(user_input, "\n", "\r\t ", _, Stdin),
|
||||
split_string(Stdin, " ", "", R),
|
||||
prompt(_, Old),
|
||||
string_lower(Stdin, Stdin_lower),
|
||||
split_string(Stdin_lower, " ", "", LS),
|
||||
maplist(string_to_atom, LS, R),
|
||||
(
|
||||
R == ["bye"],
|
||||
R == [bye],
|
||||
write("See ya"),
|
||||
!
|
||||
;
|
||||
(
|
||||
nlp_understand(R, P, I),
|
||||
writeln("That's trivial:"),
|
||||
nlp_compute(P, I)
|
||||
nlp_handler(R, Z),
|
||||
writeln(Z)
|
||||
;
|
||||
writeln("I didn't understand what you want.")
|
||||
writeln("I didn't understand what you want.")
|
||||
),
|
||||
polyplay
|
||||
polyplay
|
||||
),
|
||||
!.
|
||||
|
||||
nlp_understand(R, P, I) :-
|
||||
(
|
||||
R == ["simplify", "x", "squared"],
|
||||
P = simplify,
|
||||
I = x^2
|
||||
;
|
||||
fail
|
||||
).
|
||||
|
||||
nlp_compute(simplify, P) :-
|
||||
!,
|
||||
simplify_polynomial(P, O),
|
||||
writeln(O).
|
||||
nlp_compute(_,_) :-
|
||||
fail.
|
||||
|
||||
%% nlp_number(?W:Atom, ?D:Int) is det
|
||||
%
|
||||
% Definition of a Alphabetical and Numerical relation
|
||||
%
|
||||
special_word_number(zero, 0, f).
|
||||
special_word_number(a, 1, f).
|
||||
special_word_number(one, 1, f).
|
||||
@ -237,6 +221,7 @@ special_word_number(hundred, 100, xfy).
|
||||
special_word_number(thousand, 1000, xfy).
|
||||
special_word_number(million, 1000000, xfy).
|
||||
|
||||
%% Entry point
|
||||
parse_number_explicit(void, void, T, [WN | R], NC) :-
|
||||
special_word_number(WN, N, P),
|
||||
member(P, [f, g, fy]),
|
||||
@ -245,14 +230,14 @@ parse_number_explicit(void, void, T, [WN | R], NC) :-
|
||||
parse_number_explicit(fy, NL, T, [WN | R], NC) :-
|
||||
special_word_number(WN, N, f),
|
||||
!,
|
||||
parse_number_explicit(P, op(+, NL, N), T, R, NC).
|
||||
parse_number_explicit(f, op(+, NL, N), T, R, NC).
|
||||
parse_number_explicit(xfy, TL, T, [WN | R], NC) :-
|
||||
TL \= void,
|
||||
special_word_number(WN, N, P),
|
||||
member(P, [f, g, fy]),
|
||||
!,
|
||||
parse_number_explicit(P, op(+, TL, N), T, R, NC).
|
||||
parse_number_explicit(P, TL, T, [WN | R], NC) :-
|
||||
parse_number_explicit(_, TL, T, [WN | R], NC) :-
|
||||
special_word_number(WN, N, xfy),
|
||||
TL \= void,
|
||||
!,
|
||||
@ -309,18 +294,18 @@ parse_number(T, SL, NC) :-
|
||||
%% ?- parse_number(T, [foo, five, million], NC).
|
||||
%@ false.
|
||||
|
||||
operations(times, *).
|
||||
operations(plus, +).
|
||||
|
||||
parse_operation(Op) --> [WOp], { operations(WOp, Op) }.
|
||||
parse_operation(+) --> [plus].
|
||||
parse_operation(*) --> [times].
|
||||
|
||||
parse_polynomial_operand(T) --> parse_number(T).
|
||||
parse_polynomial_operand(T) --> parse_power(T).
|
||||
parse_polynomial_operand(T) --> parse_stored_variable(T).
|
||||
|
||||
parse_stored_variable(op(load, P, void)) --> %% NOTE Not sure if it's better to load now or later
|
||||
:- dynamic polynomial_store/2.
|
||||
|
||||
parse_stored_variable(T) --> %% NOTE Not sure if it's better to load now or later
|
||||
[P],
|
||||
polynomial_store(P, T).
|
||||
{ polynomial_store(P, T) }.
|
||||
|
||||
parse_polynomial_variable(B) -->
|
||||
[B],
|
||||
@ -361,7 +346,9 @@ parse_polynomial(TLP-TL, TLP) -->
|
||||
!,
|
||||
{ TL \= void }.
|
||||
parse_polynomial(void-_, T) -->
|
||||
parse_polynomial_operand(T), { T \= void }.
|
||||
parse_polynomial_operand(T),
|
||||
!,
|
||||
{ T \= void }.
|
||||
%% Tests:
|
||||
%% ?- parse_polynomial(_-_, T, [], _).
|
||||
%@ false.
|
||||
@ -386,20 +373,20 @@ parse_polynomial(void-_, T) -->
|
||||
%@ T = op(+, op(+, 2, 2), op(*, 1, y)).
|
||||
|
||||
|
||||
parse_command(op(show, T, void)) --> %% NOTE Probably easier if the tree is always binary
|
||||
parse_command(show(T)) --> %% NOTE Probably easier if the tree is always binary
|
||||
[show],
|
||||
parse_polynomial(T).
|
||||
parse_command(op(show, op(store, P, T))) -->
|
||||
parse_command(show(store(P, T))) -->
|
||||
[show],
|
||||
parse_polynomial(T),
|
||||
[as],
|
||||
[P].
|
||||
parse_command(op(store, P, T)) -->
|
||||
parse_command(store(P, T)) -->
|
||||
[let],
|
||||
[P],
|
||||
[be],
|
||||
parse_polynomial(T).
|
||||
parse_command(T) -->
|
||||
parse_command(simplify(T)) -->
|
||||
[simplify],
|
||||
parse_polynomial(T).
|
||||
parse_command(op(*, TN, TP)) -->
|
||||
@ -824,8 +811,8 @@ simplify_polynomial_as_list(L, L13) :-
|
||||
%% Sort list converting back gives the result in the correct order
|
||||
sort(0, @=<, L11, L12),
|
||||
(
|
||||
%% If the list is empty, the result is a list with 0
|
||||
L12 = [], L13 = [0]
|
||||
%% If the list is empty, the result is a list with 0
|
||||
L12 = [], L13 = [0]
|
||||
;
|
||||
%% Otherwise, this is the result
|
||||
L13 = L12
|
||||
|
Reference in New Issue
Block a user