]> gitweb.factorcode.org Git - factor.git/blob - extra/math/text/french/french.factor
factor: trim using lists
[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 sequences splitting ;
5 IN: math.text.french
6
7 <PRIVATE
8
9 DEFER: basic
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 ) dup "s" tail? [ CHAR: s suffix ] unless ;
33
34 : space-append ( str1 str2 -- str ) " " glue ;
35
36 : dash-append ( str1 str2 -- str ) "-" glue ;
37
38 ! Numbers below 1000000 use dashes between them. Pluralized prefixes
39 ! must be unpluralized.
40 : complete ( str n -- str )
41     { { 0 [ ] }
42       { 1 [ "-et-un" append ] }
43       [ [ unpluralize ] dip basic "-" glue ] } case ;
44
45 : smaller-than-60 ( n -- str )
46     dup 10 mod [ - ] keep [ basic ] dip complete ;
47
48 : base-onto ( n b -- str ) [ nip literals at ] [ - ] 2bi complete ;
49
50 : smaller-than-80 ( n -- str ) 60 base-onto ;
51
52 : smaller-than-100 ( n -- str ) 80 base-onto ;
53
54 : smaller-than-1000 ( n -- str )
55     100 /mod
56     [ "cent" swap dup 1 = [ drop ] [ basic swap dash-append ] if ]
57     [ [ pluralize ] [ basic dash-append ] if-zero ] bi* ;
58
59 : smaller-than-2000 ( n -- str ) "mille" swap 1000 - complete ;
60
61 : smaller-than-1000000 ( n -- str )
62     1000 /mod [ basic unpluralize "-mille" append ] dip complete ;
63
64 : n-units ( n unit -- str/f )
65     {
66         { [ over zero? ] [ 2drop f ] }
67         { [ over 1 = ] [ [ basic ] dip space-append ] }
68         [ [ basic ] dip space-append pluralize ]
69     } cond ;
70
71 : over-1000000 ( n -- str )
72     3 digit-groups [ 1 + units nth n-units ] map-index sift
73     reverse join-words ;
74
75 : decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip
76     dup 0 > [ basic space-append ] [ drop ] if ;
77
78 : basic ( n -- str )
79     {
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 ] }
88         [ decompose ]
89     } cond ;
90
91 : ieme ( str -- str )
92     dup "ts" tail? [ but-last ] when
93     dup "e" tail? [ but-last ] when
94     dup "q" tail? [ CHAR: u suffix ] when
95     "ième" append ;
96
97 : divisor ( n -- str )
98     {
99         { 2 [ "demi" ] }
100         { 3 [ "tiers" ] }
101         { 4 [ "quart" ] }
102         [ basic ieme ]
103     } case ;
104
105 PRIVATE>
106
107 GENERIC: number>text ( n -- str )
108
109 M: integer number>text
110     dup abs 102 10^ >= [ number>string ] [ basic ] if ;
111
112 M: ratio number>text
113     >fraction [ [ number>text ] keep ] [ divisor ] bi*
114     swap abs 1 > [ pluralize ] when
115     space-append ;