Created
November 1, 2011 22:15
-
-
Save schlundd/1332116 to your computer and use it in GitHub Desktop.
math.text.german
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! Copyright (C) 2011 Dominik Schlund. | |
! See http://factorcode.org/license.txt for BSD license. | |
USING: tools.test math.text.german ; | |
IN: math.text.german.tests | |
[ "null" ] [ 0 number>text ] unit-test | |
[ "eins" ] [ 1 number>text ] unit-test | |
[ "einundzwanzig" ] [ 21 number>text ] unit-test | |
[ "zweiundzwanzig" ] [ 22 number>text ] unit-test | |
[ "zweitausend" ] [ 2000 number>text ] unit-test | |
[ "einundsechzig" ] [ 61 number>text ] unit-test | |
[ "zweiundsechzig" ] [ 62 number>text ] unit-test | |
[ "zweihundert" ] [ 200 number>text ] unit-test | |
[ "eintausendzweihundert" ] [ 1200 number>text ] unit-test | |
[ "eintausendzweihundertachtzig" ] [ 1280 number>text ] unit-test | |
[ "eine Billion zweihundertzwanzig Milliarden achtzig Millionen dreihundertachtzigtausendzweihundert" ] [ 1220080380200 number>text ] unit-test | |
[ "eine Million" ] [ 1000000 number>text ] unit-test | |
[ "eine Million eins" ] [ 1000001 number>text ] unit-test | |
[ "zwei Millionen eins" ] [ 2000001 number>text ] unit-test | |
[ "minus zwanzig" ] [ -20 number>text ] unit-test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! Copyright (C) 2011 Dominik Schlund. | |
! See http://factorcode.org/license.txt for BSD license. | |
USING: kernel assocs math math.text.utils unicode.normalize sequences combinators ; | |
IN: math.text.german | |
<PRIVATE | |
CONSTANT: small-numbers | |
H{ { 1 "ein" } { 2 "zwei" } { 3 "drei" } { 4 "vier" } { 5 "fünf" } { 6 "sechs" } { 7 "sieben" } { 8 "acht" } { 9 "neun" } { 10 "zehn" } { 11 "elf" } { 12 "zwölf" } { 13 "dreizehn" } { 14 "vierzehn" } { 15 "fünfzehn" } { 16 "sechzehn" } { 17 "siebzehn" } { 18 "achtzehn" } { 19 "neunzehn" } } | |
CONSTANT: tens | |
H{ { 2 "zwanzig" } { 3 "dreißig" } { 4 "vierzig" } { 5 "fünfzig" } { 6 "sechzig" } { 7 "siebzig" } { 8 "achtzig" } { 9 "neunzig" } } | |
: postfix-singular ( n -- str ) | |
{ "" "tausend" " Million" " Milliarde" " Billion" " Billiarde" " Trillion" " Trilliarde" " Quadrillion" " Quadrillarde" " Quintillion" " Quinitilliarde" " Sextillion" " Sextilliarde" " Septillion" " Septilliarde" " Oktillion" " Oktilliarde" " Nonillion" " Nonilliarde" " Dezillion" " Dezilliarde" " Undezillion" " Undezilliarde" " Duodezillion" " Duodezilliarde" " Tredezillion" " Tredezilliarde" " Quattuordezillion" " Quattuordezilliarde" " Quindezillion" " Quindezilliarde" " Sexdezillion" " Sexdezilliarde"} | |
nth | |
; | |
: postfix-plural ( n -- str ) | |
{ | |
{ 0 [ "" ] } | |
{ 1 [ "tausend" ] } | |
[ dup even? [ postfix-singular "en" append ] [ postfix-singular "n" append ] if ] | |
} case ; | |
: two-digits ( n -- str ) | |
dup 20 < | |
[ small-numbers at ] | |
[ 10 /mod [ tens at ] [ small-numbers at ] bi* dup [ swap "und" glue ] [ drop ] if ] | |
if ; | |
: three-digits ( n -- str ) | |
dup 100 < | |
[ two-digits ] | |
[ 100 /mod [ small-numbers at ] [ two-digits ] bi* "hundert" glue ] | |
if | |
; | |
: one-at-position ( i -- str ) | |
{ { 0 [ "eins" ] } { 1 [ "ein" ] } [ drop "eine" ] } case | |
; | |
: three-digits-at-position ( i n -- str ) | |
swap | |
{ | |
{ 0 [ drop "" ] } | |
{ 1 [ [ one-at-position ] keep postfix-singular append ] } | |
[ three-digits swap postfix-plural append ] | |
} case | |
; | |
: build-string ( seq -- str ) | |
reverse "" [ [ ] [ " " glue ] if-empty ] reduce 1 tail | |
; | |
: arrange-numbers ( seq -- str ) | |
dup length 1 > | |
[ dup [ second ] [ first ] bi append swap 2 tail swap prefix ] | |
[ ] | |
if | |
build-string | |
; | |
: (number>text) ( n -- str ) | |
3 digit-groups [ three-digits-at-position ] map-index | |
arrange-numbers | |
; | |
PRIVATE> | |
: number>text ( n -- str ) | |
{ | |
{ [ dup 0 < ] [ abs number>text "minus " swap append ] } | |
{ [ dup 0 = ] [ drop "null" ] } | |
[ (number>text) ] | |
} cond | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment