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 ;
9 DEFER: basic ( n -- str )
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 ) CHAR: s suffix ;
34 : space-append ( str1 str2 -- str ) " " glue ;
36 ! Small numbers (below 100) use dashes between them unless they are
37 ! separated with "et". Pluralized prefixes must be unpluralized.
38 : complete-small ( str n -- str )
40 { 1 [ " et un" append ] }
41 [ [ unpluralize ] dip basic "-" glue ] } case ;
43 : smaller-than-60 ( n -- str )
44 dup 10 mod [ - ] keep [ basic ] dip complete-small ;
46 : base-onto ( n b -- str ) [ nip literals at ] [ - ] 2bi complete-small ;
48 : smaller-than-80 ( n -- str ) 60 base-onto ;
50 : smaller-than-100 ( n -- str ) 80 base-onto ;
52 : if-zero ( n quot quot -- )
53 [ dup zero? ] 2dip [ [ drop ] prepose ] dip if ; inline
55 : complete ( str n -- newstr )
56 [ ] [ basic space-append ] if-zero ;
58 : smaller-than-1000 ( n -- str )
60 [ "cent" swap dup 1 = [ drop ] [ basic swap space-append ] if ]
61 [ [ pluralize ] [ basic space-append ] if-zero ] bi* ;
63 : smaller-than-2000 ( n -- str ) "mille" swap 1000 - complete ;
65 : smaller-than-1000000 ( n -- str )
66 1000 /mod [ basic unpluralize " mille" append ] dip complete ;
68 : n-units ( n unit -- str/f )
70 { [ over zero? ] [ 2drop f ] }
71 { [ over 1 = ] [ [ basic ] dip space-append ] }
72 [ [ basic ] dip space-append pluralize ]
75 : over-1000000 ( n -- str )
76 3 digit-groups [ 1 + units nth n-units ] map-index sift
79 : decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip complete ;
83 { [ dup literals key? ] [ literals at ] }
84 { [ dup 0 < ] [ abs basic "moins " swap append ] }
85 { [ dup 60 < ] [ smaller-than-60 ] }
86 { [ dup 80 < ] [ smaller-than-80 ] }
87 { [ dup 100 < ] [ smaller-than-100 ] }
88 { [ dup 1000 < ] [ smaller-than-1000 ] }
89 { [ dup 2000 < ] [ smaller-than-2000 ] }
90 { [ dup 1000000 < ] [ smaller-than-1000000 ] }
96 : number>text ( n -- str )
97 dup abs 10 102 ^ >= [ number>string ] [ basic ] if ;