! Copyright (c) 2009 Samuel Tardieu.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs combinators kernel math math.functions
-math.parser math.text.utils memoize sequences ;
+math.parser math.text.utils sequences splitting ;
IN: math.text.french
<PRIVATE
-DEFER: basic ( n -- str )
+DEFER: basic
CONSTANT: literals
H{ { 0 "zéro" } { 1 "un" } { 2 "deux" } { 3 "trois" } { 4 "quatre" }
! 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 ;
-! Small numbers (below 100) use dashes between them unless they are
-! separated with "et". Pluralized prefixes must be unpluralized.
-: complete-small ( str n -- str )
+: dash-append ( str1 str2 -- str ) "-" glue ;
+
+! Numbers below 1000000 use dashes between them. Pluralized prefixes
+! must be unpluralized.
+: complete ( str n -- str )
{ { 0 [ ] }
- { 1 [ " et un" append ] }
+ { 1 [ "-et-un" append ] }
[ [ unpluralize ] dip basic "-" glue ] } case ;
: smaller-than-60 ( n -- str )
- dup 10 mod [ - ] keep [ basic ] dip complete-small ;
+ dup 10 mod [ - ] keep [ basic ] dip complete ;
-: base-onto ( n b -- str ) [ nip literals at ] [ - ] 2bi complete-small ;
+: base-onto ( n b -- str ) [ nip literals at ] [ - ] 2bi complete ;
: smaller-than-80 ( n -- str ) 60 base-onto ;
: smaller-than-100 ( n -- str ) 80 base-onto ;
-: if-zero ( n quot quot -- )
- [ dup zero? ] 2dip [ [ drop ] prepose ] dip if ; inline
-
-: complete ( str n -- newstr )
- [ ] [ basic space-append ] if-zero ;
-
: smaller-than-1000 ( n -- str )
100 /mod
- [ "cent" swap dup 1 = [ drop ] [ basic swap space-append ] if ]
- [ [ pluralize ] [ basic space-append ] if-zero ] bi* ;
+ [ "cent" swap dup 1 = [ drop ] [ basic swap dash-append ] if ]
+ [ [ pluralize ] [ basic dash-append ] if-zero ] bi* ;
: smaller-than-2000 ( n -- str ) "mille" swap 1000 - complete ;
: smaller-than-1000000 ( n -- str )
- 1000 /mod [ basic unpluralize " mille" append ] dip complete ;
+ 1000 /mod [ basic unpluralize "-mille" append ] dip complete ;
: n-units ( n unit -- str/f )
{
: over-1000000 ( n -- str )
3 digit-groups [ 1 + units nth n-units ] map-index sift
- reverse " " join ;
+ reverse join-words ;
-: decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip complete ;
+: decompose ( n -- str ) 1000000 /mod [ over-1000000 ] dip
+ dup 0 > [ basic space-append ] [ drop ] if ;
: basic ( n -- str )
{
[ 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>
-: number>text ( n -- str )
- dup abs 10 102 ^ >= [ number>string ] [ basic ] if ;
+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 ;