1 ! Copyright (c) 2009 Samuel Tardieu.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: arrays assocs combinators kernel math math.functions
4 math.parser math.text.utils memoize sequences splitting ;
12 H{ { 0 "zéro" } { 1 "un" } { 2 "deux" } { 3 "trois" } { 4 "quatre" }
13 { 5 "cinq" } { 6 "six" } { 7 "sept" } { 8 "huit" } { 9 "neuf" }
14 { 10 "dix" } { 11 "onze" } { 12 "douze" } { 13 "treize" }
15 { 14 "quatorze" } { 15 "quinze" } { 16 "seize" } { 17 "dix-sept" }
16 { 18 "dix-huit" } { 19 "dix-neuf" } { 20 "vingt" } { 30 "trente" }
17 { 40 "quarante" } { 50 "cinquante" } { 60 "soixante" }
18 { 71 "soixante et onze" } { 80 "quatre-vingts" }
19 { 81 "quatre-vingt-un" }
20 { 100 "cent" } { 1000 "mille" } }
22 MEMO: units ( -- seq ) ! up to 10^99
23 { "m" "b" "tr" "quadr" "quint" "sext" "sept" "oct"
24 "non" "déc" "unodéc" "duodéc" "trédéc" "quattuordéc"
26 [ [ "illion" append ] [ "illiard" append ] bi 2array ] map concat
29 ! The only plurals we have to remove are "quatre-vingts" and "cents",
30 ! which are also the only strings ending with "ts".
31 : unpluralize ( str -- newstr ) dup "ts" tail? [ but-last ] when ;
32 : pluralize ( str -- newstr ) dup "s" tail? [ CHAR: s suffix ] unless ;
34 : space-append ( str1 str2 -- str ) " " glue ;
36 : dash-append ( str1 str2 -- str ) "-" glue ;
38 ! Numbers below 1000000 use dashes between them. Pluralized prefixes
39 ! must be unpluralized.
40 : complete ( str n -- str )
42 { 1 [ "-et-un" append ] }
43 [ [ unpluralize ] dip basic "-" glue ] } case ;
45 : smaller-than-60 ( n -- str )
46 dup 10 mod [ - ] keep [ basic ] dip complete ;
48 : base-onto ( n b -- str ) [ nip literals at ] [ - ] 2bi complete ;
50 : smaller-than-80 ( n -- str ) 60 base-onto ;
52 : smaller-than-100 ( n -- str ) 80 base-onto ;
54 : smaller-than-1000 ( n -- str )
56 [ "cent" swap dup 1 = [ drop ] [ basic swap dash-append ] if ]
57 [ [ pluralize ] [ basic dash-append ] if-zero ] bi* ;
59 : smaller-than-2000 ( n -- str ) "mille" swap 1000 - complete ;
61 : smaller-than-1000000 ( n -- str )
62 1000 /mod [ basic unpluralize "-mille" append ] dip complete ;
64 : n-units ( n unit -- str/f )
66 { [ over zero? ] [ 2drop f ] }
67 { [ over 1 = ] [ [ basic ] dip space-append ] }
68 [ [ basic ] dip space-append pluralize ]
71 : over-1000000 ( n -- str )
72 3 digit-groups [ 1 + units nth n-units ] map-index sift
75 : decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip
76 dup 0 > [ basic space-append ] [ drop ] if ;
80 { [ dup literals key? ] [ literals at ] }
81 { [ dup 0 < ] [ abs basic "moins " swap append ] }
82 { [ dup 60 < ] [ smaller-than-60 ] }
83 { [ dup 80 < ] [ smaller-than-80 ] }
84 { [ dup 100 < ] [ smaller-than-100 ] }
85 { [ dup 1000 < ] [ smaller-than-1000 ] }
86 { [ dup 2000 < ] [ smaller-than-2000 ] }
87 { [ dup 1000000 < ] [ smaller-than-1000000 ] }
92 dup "ts" tail? [ but-last ] when
93 dup "e" tail? [ but-last ] when
94 dup "q" tail? [ CHAR: u suffix ] when
97 : divisor ( n -- str )
107 GENERIC: number>text ( n -- str )
109 M: integer number>text
110 dup abs 102 10^ >= [ number>string ] [ basic ] if ;
113 >fraction [ [ number>text ] keep ] [ divisor ] bi*
114 swap abs 1 > [ pluralize ] when