]> gitweb.factorcode.org Git - factor.git/blob - extra/math/text/english/english.factor
Fix conflict
[factor.git] / extra / math / text / english / english.factor
1 ! Copyright (c) 2007, 2008 Aaron Schaefer.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: combinators.short-circuit grouping kernel math math.parser
4 math.text.utils namespaces sequences ;
5 IN: math.text.english
6
7 <PRIVATE
8
9 : small-numbers ( n -- str )
10     { "Zero" "One" "Two" "Three" "Four" "Five" "Six" "Seven" "Eight" "Nine"
11     "Ten" "Eleven" "Twelve" "Thirteen" "Fourteen" "Fifteen" "Sixteen"
12     "Seventeen" "Eighteen" "Nineteen" } nth ;
13
14 : tens ( n -- str )
15     { f f "Twenty" "Thirty" "Forty" "Fifty" "Sixty" "Seventy" "Eighty" "Ninety" } nth ;
16
17 : scale-numbers ( n -- str )  ! up to 10^99
18     { f "Thousand" "Million" "Billion" "Trillion" "Quadrillion" "Quintillion"
19     "Sextillion" "Septillion" "Octillion" "Nonillion" "Decillion" "Undecillion"
20     "Duodecillion" "Tredecillion" "Quattuordecillion" "Quindecillion"
21     "Sexdecillion" "Septendecillion" "Octodecillion" "Novemdecillion"
22     "Vigintillion" "Unvigintillion" "Duovigintillion" "Trevigintillion"
23     "Quattuorvigintillion" "Quinvigintillion" "Sexvigintillion"
24     "Septvigintillion" "Octovigintillion" "Novemvigintillion" "Trigintillion"
25     "Untrigintillion" "Duotrigintillion" } nth ;
26
27 SYMBOL: and-needed?
28 : set-conjunction ( seq -- )
29     first { [ 100 < ] [ 0 > ] } 1&& and-needed? set ;
30
31 : negative-text ( n -- str )
32     0 < "Negative " "" ? ;
33
34 : hundreds-place ( n -- str )
35     100 /mod over 0 = [
36         2drop ""
37     ] [
38         [ small-numbers " Hundred" append ] dip
39         0 = [ " and " append ] unless
40     ] if ;
41
42 : tens-place ( n -- str )
43     100 mod dup 20 >= [
44         10 /mod [ tens ] dip
45         dup 0 = [ drop ] [ small-numbers "-" glue ] if
46     ] [
47         dup 0 = [ drop "" ] [ small-numbers ] if
48     ] if ;
49
50 : 3digits>text ( n -- str )
51     [ hundreds-place ] [ tens-place ] bi append ;
52
53 : text-with-scale ( index seq -- str )
54     [ nth 3digits>text ] [ drop scale-numbers ] 2bi
55     [ " " glue ] unless-empty ;
56
57 : append-with-conjunction ( str1 str2 -- newstr )
58     over length 0 = [
59         nip
60     ] [
61         swap and-needed? get " and " ", " ?
62         glue and-needed? off
63     ] if ;
64
65 : (recombine) ( str index seq -- newstr )
66     2dup nth 0 = [
67         2drop
68     ] [
69         text-with-scale append-with-conjunction
70     ] if ;
71
72 : recombine ( seq -- str )
73     dup length 1 = [
74         first 3digits>text
75     ] [
76         [ set-conjunction "" ] [ length ] [ ] tri
77         [ (recombine) ] curry each
78     ] if ;
79
80 : (number>text) ( n -- str )
81     [ negative-text ] [ abs 3digit-groups recombine ] bi append ;
82
83 PRIVATE>
84
85 : number>text ( n -- str )
86     dup zero? [ small-numbers ] [ [ (number>text) ] with-scope ] if ;
87