diff --git a/numberals.pl b/numberals.pl index 3a91d03..f46e512 100644 --- a/numberals.pl +++ b/numberals.pl @@ -126,7 +126,7 @@ number_to_name(Num, Name) :- ground(Name), % a non-empty group name must be in Name - append([PrefixName," ", Group_Name, _], Name), power_name(Group, Group_Name), Group_Name \= "", + append([PrefixName," ", Group_Name, _], Name), power_name(Group, Group_Name), Group > 0, number_to_name_prefix(FirstPrefix, PrefixName), FirstPrefix \= [0, 0, 0], PrefixNumL is Group + 3, length(PrefixNum, PrefixNumL), @@ -145,8 +145,7 @@ add_space_nextname(NameNext, NameNextS), append([Group_Prefix, Space, Group_Name, NameNextS], Name). number_to_name_group([H,T,O|NumNext], Name, Group) :- - ground(Name), Group >= 0, GroupNext is Group - 3, - %write(Group),nl, + ground(Name), Group > 0, GroupNext is Group - 3, ( (power_name(Group, Group_Name), (Group_Name == "" -> Space = ""; Space = " "), append([Group_Prefix, Space, Group_Name, NameNextS], Name), @@ -155,24 +154,31 @@ append([Group_Prefix, Group_Name, NameNextS], Name), number_to_name_prefix([H,T,O], Group_Prefix)) ), - %writef('%s:%s:%s:%w\n', [Group_Prefix, Group_Name, NameNextS, [[H,T,O]]]), - add_space_nextname(NameNext, NameNextS), NameNext \= [32|_], + remove_space(NameNextS, NameNext), number_to_name_group(NumNext, NameNext, GroupNext). +number_to_name_group([H,T,O], Name, 0) :- number_to_name_prefix([H,T,O], Name). number_to_name_group([], "", _). -add_space_nextname("", ""). -add_space_nextname([32|NextName], [32|NextName]). -add_space_nextname([First|NextName], [32,First|NextName]) :- First \= 32. + +add_space_nextname("", ""). % empty string +add_space_nextname([32|NextName], [32|NextName]). % already has a space +add_space_nextname([First|NextName], [32,First|NextName]) :- First \= 32. % add space + +remove_space("", ""). +remove_space([32,First|NextName], [First|NextName]) :- First \= 32. +remove_space([First|NextName], [First|NextName]) :- [First|NextName] \= "", First \= 32. % }}} % hundred helpers {{{ number_to_name_prefix([ 0, 0, 0], "") :- !. % cut to prevent other unifications below number_to_name_prefix([ 0, 0, Ones], Name) :- ground(Ones), Ones \= 0, number_to_name( [Ones], Name). number_to_name_prefix([ 0, Tens, Ones], Name) :- ground(Tens), Tens \= 0, number_to_name( [Tens, Ones], Name). -number_to_name_prefix([Hundreds, Tens, Ones], Name) :- ground([Hundreds, Tens]), [Hundreds, Tens] \= [0, 0], number_to_name( [Hundreds, Tens, Ones], Name). -number_to_name_prefix([ 0, Ones], Name) :- number_to_name( [Ones], Name). -number_to_name_prefix([Tens, Ones], Name) :- Tens \= 0, number_to_name( [Tens, Ones], Name). -number_to_name_prefix([One], Name) :- number_to_name([One], Name). +number_to_name_prefix([Hundreds, Tens, Ones], Name) :- ground([Hundreds, Tens, Ones]), [Hundreds, Tens, Ones] \= [0, 0, 0], number_to_name( [Hundreds, Tens, Ones], Name). +number_to_name_prefix([ 0, Ones], Name) :- ground(Ones), number_to_name( [Ones], Name). +number_to_name_prefix([Tens, Ones], Name) :- ground([Tens, Ones]), Tens \= 0, number_to_name( [Tens, Ones], Name). +number_to_name_prefix([One], Name) :- ground([One]), number_to_name([One], Name). -number_to_name_prefix([Hundreds, Tens, Ones], Name) :- ground(Name), member(NumLen, [1,2,3]), length(Num, NumLen), number_to_name( Num, Name), +number_to_name_prefix([Hundreds, Tens, Ones], Name) :- ground(Name), + \+ (append([_, GroupName, _],Name), GroupName \= "", power_name(_, GroupName)), + member(NumLen, [1,2,3]), length(Num, NumLen), number_to_name( Num, Name), number_to_name_add_prefix(Num, [Hundreds, Tens, Ones]). hundred_build( [Tens, Ones], Rest_Name) :- diff --git a/numberals.plt b/numberals.plt index 481458f..e050fe0 100644 --- a/numberals.plt +++ b/numberals.plt @@ -69,28 +69,23 @@ test_both_ways_test([1,0,0,0,0,0,0], "one million"). test_both_ways_test([-,1,0,0,0,0,0,0], "negative one million"). test_both_ways_test([2,0,0,0,0,0,0], "two million"). test_both_ways_test([-,2,0,0,0,0,0,0], "negative two million"). -test_both_ways_test_f([2,0,0,0,9,0,0], "two million nine hundred").%_ -%%%---------------------|-----|-----] -test_both_ways_test_f([2,0,0,0,0,9,0], "two million ninety").%_ -%%%---------------------|-----|-----] -test_both_ways_test_f([2,0,0,0,0,0,9], "two million nine").%_ -%%%---------------------|-----|-----] +test_both_ways_test([2,0,0,0,9,0,0], "two million nine hundred"). +test_both_ways_test([2,0,0,0,0,9,0], "two million ninety"). +test_both_ways_test([2,0,0,0,0,0,9], "two million nine"). test_both_ways_test([1,0,2,0,0,0,0,0,0], "one hundred and two million"). test_both_ways_test([-,1,0,2,0,0,0,0,0,0], "negative one hundred and two million"). -test_both_ways_test_f([1,0,2,0,0,0,0,0,9], "one hundred and two million nine"). -test_both_ways_test_f([2,0,0,1,0,0,0,1,0,9], "two billion one million one hundred and nine").%_ -%%%---------------------|-----|-----|-----] +test_both_ways_test([1,0,2,0,0,0,0,0,9], "one hundred and two million nine"). +test_both_ways_test([2,0,0,1,0,0,0,1,0,9], "two billion one million one hundred and nine"). test_both_ways_test([2,0,0,1,1,0,0,1,0,9], "two billion one million one hundred thousand one hundred and nine"). test_both_ways_test([2,0,0,1,1,2,0,1,0,9], "two billion one million one hundred and twenty thousand one hundred and nine"). test_both_ways_test([2,0,0,1,1,2,3,1,0,9], "two billion one million one hundred and twenty-three thousand one hundred and nine"). -test_both_ways_test_f([2,0,0,0,0,0,1,0,0,0,1,0,9], "two trillion one million one hundred and nine").%_ -%%%---------------------|-----|-----|-----|-----] +test_both_ways_test([2,0,0,0,0,0,1,0,0,0,1,0,9], "two trillion one million one hundred and nine"). test(test_both_ways, [nondet, forall( test_both_ways_test(Num, Name))]) :- test_both_ways(Num, Name). -test(test_both_ways, [nondet, forall( test_both_ways_test_f(Num, Name))]) :- - \+ test_both_ways(Num, Name). +%test(test_both_ways, [nondet, forall( test_both_ways_test_f(Num, Name))]) :- + %\+ test_both_ways(Num, Name). %test(test_to_name, [nondet, forall( test_to_name_test(Num, Name) )]) :- %%number_to_name(Num, Test_Name), Test_Name = Name.