Simplified the way number parsing works
This commit is contained in:
parent
75cae0b25f
commit
040f3bd859
108
polymani.pl
108
polymani.pl
@ -220,85 +220,55 @@ special_word_number(hundred, 100, xfy).
|
|||||||
special_word_number(thousand, 1000, xfy).
|
special_word_number(thousand, 1000, xfy).
|
||||||
special_word_number(million, 1000000, xfy).
|
special_word_number(million, 1000000, xfy).
|
||||||
|
|
||||||
word_tree([W], N) :-
|
parse_number(void, T, [WN | R], NC) :-
|
||||||
special_word_number(W, N, P),
|
special_word_number(WN, N, P),
|
||||||
member(P, [f, fy]).
|
|
||||||
word_tree([W, W2], op(+, N, N2)) :-
|
|
||||||
special_word_number(W, N, fy),
|
|
||||||
special_word_number(W2, N2, f).
|
|
||||||
word_tree([W, W2], op(*, N, N2)) :-
|
|
||||||
special_word_number(W, N, P),
|
|
||||||
member(P, [f, fy]),
|
member(P, [f, fy]),
|
||||||
special_word_number(W2, N2, xfy).
|
parse_number(N, T, R, NC),
|
||||||
word_tree([W, W2, W3], op(*, op(+, N, N2), N3)) :-
|
!.
|
||||||
special_word_number(W, N, fy),
|
parse_number(TL, T, [WN | R], NC) :-
|
||||||
special_word_number(W2, N2, f),
|
special_word_number(WN, N, P),
|
||||||
special_word_number(W3, N3, xfy).
|
member(P, [f, fy]),
|
||||||
|
parse_number(op(+, TL, N), T, R, NC),
|
||||||
join_trees(Op, L, void, L) :- !.
|
!.
|
||||||
join_trees(Op, L, R, op(Op, L, R)).
|
parse_number(TL, T, [WN | R], NC) :-
|
||||||
%% ?- join_trees(+, op(+, 3, 2), void, T).
|
special_word_number(WN, N, xfy),
|
||||||
%@ T = op(+, 3, 2).
|
TL \= void,
|
||||||
%% ?- join_trees(+, op(+, 3, 2), op(*, 3, 4), T).
|
parse_number(op(*, TL, N), T, R, NC),
|
||||||
%@ T = op(+, op(+, 3, 2), op(*, 3, 4)).
|
!.
|
||||||
|
parse_number(TL, T, [and, WN | R], NC) :-
|
||||||
parse_number([WN, WN2, WN3 | R], NC, T) :-
|
parse_number(TL, T, [WN | R], NC),
|
||||||
parse_number(R, NC, TR),
|
!.
|
||||||
word_tree([WN, WN2, WN3], TL),
|
parse_number(T, T, [WN | R], [WN | R]) :-
|
||||||
%% Red cut? If we can consume three words, do so
|
not(word_tree(WN, _)),
|
||||||
!,
|
!.
|
||||||
join_trees(+, TL, TR, T).
|
parse_number(T, T, [], []) :-
|
||||||
parse_number([WN, WN2 | R], NC, T) :-
|
!.
|
||||||
parse_number(R, NC, TR),
|
|
||||||
word_tree([WN, WN2], TL),
|
|
||||||
!,
|
|
||||||
join_trees(+, TL, TR, T).
|
|
||||||
parse_number([WN | R], NC, T) :-
|
|
||||||
parse_number(R, NC, TR),
|
|
||||||
word_tree([WN], TL),
|
|
||||||
!,
|
|
||||||
join_trees(+, TL, TR, T).
|
|
||||||
parse_number([and, WN | R], NC, T) :-
|
|
||||||
parse_number(R, NC, TR),
|
|
||||||
word_tree([WN], TL),
|
|
||||||
!,
|
|
||||||
join_trees(+, TL, TR, T).
|
|
||||||
parse_number(R, R, void) :-
|
|
||||||
not(word_tree(R, _)).
|
|
||||||
%% Tests:
|
%% Tests:
|
||||||
%% ?- parse_number([two], NC, T).
|
%% ?- parse_number(void, T, [two], _).
|
||||||
%@ NC = [],
|
|
||||||
%@ T = 2.
|
%@ T = 2.
|
||||||
%% ?- parse_number([twenty], NC, T).
|
%% ?- parse_number(void, T, [twenty], _).
|
||||||
%@ NC = [],
|
|
||||||
%@ T = 20.
|
%@ T = 20.
|
||||||
%% ?- parse_number([twenty, one], NC, T).
|
%% ?- parse_number(void, T, [twenty, one], _).
|
||||||
%@ NC = [],
|
|
||||||
%@ T = op(+, 20, 1).
|
%@ T = op(+, 20, 1).
|
||||||
%% ?- parse_number([hundred], NC, T).
|
%% ?- parse_number(void, T, [hundred], _).
|
||||||
%@ NC = [hundred],
|
%@ false.
|
||||||
%@ T = void.
|
%% ?- parse_number(void, T, [three, hundred], _).
|
||||||
%% ?- parse_number([three, hundred], NC, T).
|
|
||||||
%@ NC = [],
|
|
||||||
%@ T = op(*, 3, 100).
|
%@ T = op(*, 3, 100).
|
||||||
%% ?- parse_number([twenty, hundred], NC, T).
|
%% ?- parse_number(void, T, [twenty, hundred], _).
|
||||||
%@ NC = [],
|
|
||||||
%@ T = op(*, 20, 100).
|
%@ T = op(*, 20, 100).
|
||||||
%% ?- parse_number([twenty, one, hundred], NC, T).
|
%% ?- parse_number(void, T, [twenty, one, hundred], _).
|
||||||
%@ NC = [],
|
|
||||||
%@ T = op(*, op(+, 20, 1), 100).
|
%@ T = op(*, op(+, 20, 1), 100).
|
||||||
%% ?- parse_number([two, hundred, and, one], NC, T).
|
%% ?- parse_number(void, T, [two, hundred, and, one], _).
|
||||||
%@ NC = [],
|
|
||||||
%@ T = op(+, op(*, 2, 100), 1).
|
%@ T = op(+, op(*, 2, 100), 1).
|
||||||
%% ?- parse_number([twenty, one, hundred, and, twenty, one], NC, T).
|
%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one], _).
|
||||||
%@ NC = [],
|
%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1).
|
||||||
%@ T = op(+, op(*, op(+, 20, 1), 100), op(+, 20, 1)).
|
%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one, foo, bar, blah], _).
|
||||||
%% ?- parse_number([twenty, one, hundred, and, twenty, one, foo, bar, blah], NC, T).
|
%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1).
|
||||||
%@ NC = [foo, bar, blah],
|
%% ?- parse_number(void, T, [twenty, one, hundred, and, bleg, twenty, quux, one, foo, bar], _).
|
||||||
%@ T = op(+, op(*, op(+, 20, 1), 100), op(+, 20, 1)).
|
|
||||||
%% ?- parse_number([twenty, one, hundred, and, bleg, twenty, quux, one, foo, bar, blah], NC, T).
|
|
||||||
%@ NC = [and, bleg, twenty, quux, one, foo, bar, blah],
|
|
||||||
%@ T = op(*, op(+, 20, 1), 100).
|
%@ T = op(*, op(+, 20, 1), 100).
|
||||||
|
%% ?- parse_number(void, T, [two, hundred, thousand], _).
|
||||||
|
%@ T = op(*, op(*, 2, 100), 1000).
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user