More concise number parsing

This commit is contained in:
Hugo Sales 2018-12-10 17:33:35 +00:00
parent 040f3bd859
commit ea80d57ef9

View File

@ -187,38 +187,38 @@ nlp_compute(simplify, P) :-
nlp_compute(_,_) :-
fail.
special_word_number(zero, 0, f).
special_word_number(a, 1, f).
special_word_number(one, 1, f).
special_word_number(two, 2, f).
special_word_number(three, 3, f).
special_word_number(four, 4, f).
special_word_number(five, 5, f).
special_word_number(six, 6, f).
special_word_number(seven, 7, f).
special_word_number(eight, 8, f).
special_word_number(nine, 9, f).
special_word_number(ten, 10, f).
special_word_number(eleven, 11, f).
special_word_number(twelve, 12, f).
special_word_number(thirteen, 13, f).
special_word_number(fourteen, 14, f).
special_word_number(fifteen, 15, f).
special_word_number(sixteen, 16, f).
special_word_number(seventeen, 17, f).
special_word_number(eighteen, 18, f).
special_word_number(nineteen, 19, f).
special_word_number(twenty, 20, fy).
special_word_number(thirty, 30, fy).
special_word_number(forty, 40, fy).
special_word_number(fifty, 50, fy).
special_word_number(sixty, 60, fy).
special_word_number(seventy, 70, fy).
special_word_number(eighty, 80, fy).
special_word_number(ninety, 90, fy).
special_word_number(hundred, 100, xfy).
special_word_number(thousand, 1000, xfy).
special_word_number(million, 1000000, xfy).
special_word_number(zero, 0, f).
special_word_number(a, 1, f).
special_word_number(one, 1, f).
special_word_number(two, 2, f).
special_word_number(three, 3, f).
special_word_number(four, 4, f).
special_word_number(five, 5, f).
special_word_number(six, 6, f).
special_word_number(seven, 7, f).
special_word_number(eight, 8, f).
special_word_number(nine, 9, f).
special_word_number(ten, 10, f).
special_word_number(eleven, 11, f).
special_word_number(twelve, 12, f).
special_word_number(thirteen, 13, f).
special_word_number(fourteen, 14, f).
special_word_number(fifteen, 15, f).
special_word_number(sixteen, 16, f).
special_word_number(seventeen, 17, f).
special_word_number(eighteen, 18, f).
special_word_number(nineteen, 19, f).
special_word_number(twenty, 20, fy).
special_word_number(thirty, 30, fy).
special_word_number(forty, 40, fy).
special_word_number(fifty, 50, fy).
special_word_number(sixty, 60, fy).
special_word_number(seventy, 70, fy).
special_word_number(eighty, 80, fy).
special_word_number(ninety, 90, fy).
special_word_number(hundred, 100, xfy).
special_word_number(thousand, 1000, xfy).
special_word_number(million, 1000000, xfy).
parse_number(void, T, [WN | R], NC) :-
special_word_number(WN, N, P),
@ -236,12 +236,15 @@ parse_number(TL, T, [WN | R], NC) :-
parse_number(op(*, TL, N), T, R, NC),
!.
parse_number(TL, T, [and, WN | R], NC) :-
special_word_number(WN, _, _),
parse_number(TL, T, [WN | R], NC),
!.
parse_number(T, T, [WN | R], [WN | R]) :-
not(word_tree(WN, _)),
T \= void,
not(special_word_number(WN, _, _)),
!.
parse_number(T, T, [], []) :-
T \= void,
!.
%% Tests:
%% ?- parse_number(void, T, [two], _).
@ -262,14 +265,47 @@ parse_number(T, T, [], []) :-
%@ T = op(+, op(*, 2, 100), 1).
%% ?- 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, [twenty, one, hundred, and, twenty, one, foo, bar, blah], NC).
%@ 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], NC).
%@ T = op(*, op(+, 20, 1), 100),
%@ NC = [and, bleg, twenty, quux, one, foo, bar].
%% ?- parse_number(void, T, [two, hundred, thousand], _).
%@ T = op(*, op(*, 2, 100), 1000).
%% ?- parse_number(void, T, [twenty, one, hundred, thousand], _).
%@ T = op(*, op(*, op(+, 20, 1), 100), 1000).
%% ?- parse_number(void, T, [thirty, five, million], _).
%@ T = op(*, op(+, 30, 5), 1000000).
%% ?- parse_number(void, T, [foo, five, million], NC).
%@ false.
operations(times, *).
operations(plus, +).
parse_operation(Op) --> [WOp], { operations(WOp, Op) }.
parse_expression(TLP-TL, op(Op, TLP-TL, TR)) --> parse_number(void, TL),
parse_operation(Op),
parse_expression(op(Op, TL, TRP)-TRP, TR).
parse_expression(void, T) --> parse_number(void, T), { T \= void }.
parse_expression(TL, TL-TR) --> parse_number(void, TR), { TR \= void, TL \= void }.
%% Tests:
%% ?- parse_expression(T, [], _).
%@ false.
%% ?- parse_expression(T, [two], _).
%@ T = 2.
%% ?- parse_expression(void, T, [two, times, two], _).
%@ T = 2 ;
%@ false.
%@ T = 2 ;
%@ false.
%@ T = op(*, 2, 2).
%% ?- parse_expression(T, [two, times, three, plus, two], _).
%@ T = op(*, 2, 3).
%@ T = op(*, 2, op(+, 3, 2)).
%% ?- parse_expression(T, [two, times, times, two], _).
%@ false.