Skip to content

Commit

Permalink
Add French number>text support for ratios
Browse files Browse the repository at this point in the history
  • Loading branch information
samueltardieu committed Mar 23, 2017
1 parent 5707acd commit 30f73d6
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
12 changes: 12 additions & 0 deletions extra/math/text/french/french-tests.factor
Expand Up @@ -20,3 +20,15 @@ USING: math math.functions math.parser math.text.french sequences tools.test ;
{ 104 } [ -1 10 102 ^ - number>text length ] unit-test
! Check that we do not exhaust stack
{ 1484 } [ 10 100 ^ 1 - number>text length ] unit-test
{ "un demi" } [ 1/2 number>text ] unit-test
{ "trois demis" } [ 3/2 number>text ] unit-test
{ "un tiers" } [ 1/3 number>text ] unit-test
{ "deux tiers" } [ 2/3 number>text ] unit-test
{ "un quart" } [ 1/4 number>text ] unit-test
{ "un cinquième" } [ 1/5 number>text ] unit-test
{ "un seizième" } [ 1/16 number>text ] unit-test
{ "mille cent-vingt-septièmes" } [ 1000/127 number>text ] unit-test
{ "mille-cent vingt-septièmes" } [ 1100/27 number>text ] unit-test
{ "mille-cent-dix-neuf septièmes" } [ 1119/7 number>text ] unit-test
{ "moins un quatre-vingtième" } [ -1/80 number>text ] unit-test
{ "moins dix-neuf quatre-vingtièmes" } [ -19/80 number>text ] unit-test
21 changes: 20 additions & 1 deletion extra/math/text/french/french.factor
Expand Up @@ -29,7 +29,7 @@ MEMO: units ( -- seq ) ! up to 10^99
! The only plurals we have to remove are "quatre-vingts" and "cents",
! which are also the only strings ending with "ts".
: unpluralize ( str -- newstr ) dup "ts" tail? [ but-last ] when ;
: pluralize ( str -- newstr ) CHAR: s suffix ;
: pluralize ( str -- newstr ) dup "s" tail? [ CHAR: s suffix ] unless ;

: space-append ( str1 str2 -- str ) " " glue ;

Expand Down Expand Up @@ -88,9 +88,28 @@ MEMO: units ( -- seq ) ! up to 10^99
[ decompose ]
} cond ;

: ieme ( str -- str )
dup "ts" tail? [ but-last ] when
dup "e" tail? [ but-last ] when
dup "q" tail? [ CHAR: u suffix ] when
"ième" append ;

: divisor ( n -- str )
{
{ 2 [ "demi" ] }
{ 3 [ "tiers" ] }
{ 4 [ "quart" ] }
[ basic ieme ]
} case ;

PRIVATE>

GENERIC: number>text ( n -- str )

M: integer number>text
dup abs 102 10^ >= [ number>string ] [ basic ] if ;

M: ratio number>text
>fraction [ [ number>text ] keep ] [ divisor ] bi*
swap abs 1 > [ pluralize ] when
space-append ;

0 comments on commit 30f73d6

Please sign in to comment.