]> gitweb.factorcode.org Git - factor.git/blob - extra/math/text/french/french.factor
Delete empty unit tests files, remove 1- and 1+, reorder IN: lines in a lot of places...
[factor.git] / extra / math / text / french / french.factor
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 ;
5 IN: math.text.french
6
7 <PRIVATE
8
9 DEFER: basic ( n -- str )
10
11 CONSTANT: literals
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" } }
21
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"
25       "quindéc" "sexdéc" }
26       [ [ "illion" append ] [ "illiard" append ] bi 2array ] map concat
27       "mille" prefix ;
28
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 ;
33
34 : space-append ( str1 str2 -- str ) " " glue ;
35
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 )
39     { { 0 [ ] }
40       { 1 [ " et un" append ] }
41       [ [ unpluralize ] dip basic "-" glue ] } case ;
42
43 : smaller-than-60 ( n -- str )
44     dup 10 mod [ - ] keep [ basic ] dip complete-small ;
45
46 : base-onto ( n b -- str ) [ nip literals at ] [ - ] 2bi complete-small ;
47
48 : smaller-than-80 ( n -- str ) 60 base-onto ;
49
50 : smaller-than-100 ( n -- str ) 80 base-onto ;
51
52 : if-zero ( n quot quot -- )
53     [ dup zero? ] 2dip [ [ drop ] prepose ] dip if ; inline
54
55 : complete ( str n -- newstr )
56     [ ] [ basic space-append ] if-zero ;
57
58 : smaller-than-1000 ( n -- str )
59     100 /mod
60     [ "cent" swap dup 1 = [ drop ] [ basic swap space-append ] if ]
61     [ [ pluralize ] [ basic space-append ] if-zero ] bi* ;
62
63 : smaller-than-2000 ( n -- str ) "mille" swap 1000 - complete ;
64
65 : smaller-than-1000000 ( n -- str )
66     1000 /mod [ basic unpluralize " mille" append ] dip complete ;
67
68 : n-units ( n unit -- str/f )
69     {
70         { [ over zero? ] [ 2drop f ] }
71         { [ over 1 = ] [ [ basic ] dip space-append ] }
72         [ [ basic ] dip space-append pluralize ]
73     } cond ;
74
75 : over-1000000 ( n -- str )
76     3 digit-groups [ 1 + units nth n-units ] map-index sift
77     reverse " " join ;
78
79 : decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip complete ;
80
81 : basic ( n -- str )
82     {
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 ] }
91         [ decompose ]
92     } cond ;
93
94 PRIVATE>
95
96 : number>text ( n -- str )
97     dup abs 10 102 ^ >= [ number>string ] [ basic ] if ;