From 040f3bd8595a0894d7a843982a970c2909445e5a Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 10 Dec 2018 01:13:58 +0000 Subject: [PATCH] Simplified the way number parsing works --- polymani.pl | 108 +++++++++++++++++++--------------------------------- 1 file changed, 39 insertions(+), 69 deletions(-) diff --git a/polymani.pl b/polymani.pl index 307954f..7afc85c 100644 --- a/polymani.pl +++ b/polymani.pl @@ -220,85 +220,55 @@ special_word_number(hundred, 100, xfy). special_word_number(thousand, 1000, xfy). special_word_number(million, 1000000, xfy). -word_tree([W], N) :- - special_word_number(W, 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), +parse_number(void, T, [WN | R], NC) :- + special_word_number(WN, N, P), member(P, [f, fy]), - special_word_number(W2, N2, xfy). -word_tree([W, W2, W3], op(*, op(+, N, N2), N3)) :- - special_word_number(W, N, fy), - special_word_number(W2, N2, f), - special_word_number(W3, N3, xfy). - -join_trees(Op, L, void, L) :- !. -join_trees(Op, L, R, op(Op, L, R)). -%% ?- join_trees(+, op(+, 3, 2), void, T). -%@ T = op(+, 3, 2). -%% ?- join_trees(+, op(+, 3, 2), op(*, 3, 4), T). -%@ T = op(+, op(+, 3, 2), op(*, 3, 4)). - -parse_number([WN, WN2, WN3 | R], NC, T) :- - parse_number(R, NC, TR), - word_tree([WN, WN2, WN3], TL), - %% Red cut? If we can consume three words, do so - !, - join_trees(+, TL, TR, 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, _)). + parse_number(N, T, R, NC), + !. +parse_number(TL, T, [WN | R], NC) :- + special_word_number(WN, N, P), + member(P, [f, fy]), + parse_number(op(+, TL, N), T, R, NC), + !. +parse_number(TL, T, [WN | R], NC) :- + special_word_number(WN, N, xfy), + TL \= void, + parse_number(op(*, TL, N), T, R, NC), + !. +parse_number(TL, T, [and, WN | R], NC) :- + parse_number(TL, T, [WN | R], NC), + !. +parse_number(T, T, [WN | R], [WN | R]) :- + not(word_tree(WN, _)), + !. +parse_number(T, T, [], []) :- + !. %% Tests: -%% ?- parse_number([two], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [two], _). %@ T = 2. -%% ?- parse_number([twenty], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [twenty], _). %@ T = 20. -%% ?- parse_number([twenty, one], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [twenty, one], _). %@ T = op(+, 20, 1). -%% ?- parse_number([hundred], NC, T). -%@ NC = [hundred], -%@ T = void. -%% ?- parse_number([three, hundred], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [hundred], _). +%@ false. +%% ?- parse_number(void, T, [three, hundred], _). %@ T = op(*, 3, 100). -%% ?- parse_number([twenty, hundred], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [twenty, hundred], _). %@ T = op(*, 20, 100). -%% ?- parse_number([twenty, one, hundred], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [twenty, one, hundred], _). %@ T = op(*, op(+, 20, 1), 100). -%% ?- parse_number([two, hundred, and, one], NC, T). -%@ NC = [], +%% ?- parse_number(void, T, [two, hundred, and, one], _). %@ T = op(+, op(*, 2, 100), 1). -%% ?- parse_number([twenty, one, hundred, and, twenty, one], NC, T). -%@ NC = [], -%@ T = op(+, op(*, op(+, 20, 1), 100), op(+, 20, 1)). -%% ?- parse_number([twenty, one, hundred, and, twenty, one, foo, bar, blah], NC, T). -%@ NC = [foo, bar, blah], -%@ 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], +%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one], _). +%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1). +%% ?- parse_number(void, T, [twenty, one, hundred, and, twenty, one, foo, bar, blah], _). +%@ T = op(+, op(+, op(*, op(+, 20, 1), 100), 20), 1). +%% ?- parse_number(void, T, [twenty, one, hundred, and, bleg, twenty, quux, one, foo, bar], _). %@ T = op(*, op(+, 20, 1), 100). +%% ?- parse_number(void, T, [two, hundred, thousand], _). +%@ T = op(*, op(*, 2, 100), 1000). +