numerals: Convert numbers to number words

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Convert numbers to number words in a number of languages. Each language has its own module. The module name is based on one of the ISO 639 Alpha codes. Each module contains one or more cardinal and ordinal functions and a struct function. The cardinal functions directly convert cardinal numbers to a string-like representation of their spoken form. The ordinal functions do the same but for ordinal numbers. The struct functions convert numbers to a polymorphic representation of their grammatical structure.

The use of this package is best understood with some examples. First some English number names, both British and US variants:

>>> import qualified Text.Numeral.Language.EN as EN
>>> EN.uk_cardinal defaultInflection 123 :: Maybe Text
Just "one hundred and twenty-three"
>>> EN.us_cardinal defaultInflection (10^50 + 42) :: Maybe Text
Just "one hundred quindecillion forty-two"

French, which contains some traces of a base 20 system:

>>> import qualified Text.Numeral.Language.FR as FR
>>> FR.cardinal defaultInflection (-99) :: Maybe Text
Just "moins quatre-vingt-dix-neuf"

Conversions can fail. Alamblak, a language spoken by a few people in Papua New Guinea, has no representation for negative numbers:

>>> import qualified Text.Numeral.Language.AMP as AMP
>>> AMP.cardinal defaultInflection (-3) :: Maybe Text
Nothing

Some languages have multiple scripts and methods for writing number names. Take Chinese for example, which can be written using Han characters or transcribed to the Latin script using Pinyin.

Traditional Chinese characters:

>>> import qualified Text.Numeral.Language.ZH as ZH
>>> ZH.trad_cardinal defaultInflection 123456 :: Maybe Text
Just "十二萬三千四百五十六"

Simplified characters for use in financial contexts:

>>> ZH.finance_simpl_cardinal defaultInflection 123456 :: Maybe Text
Just "拾贰万参仟肆伯伍拾陆"

Transcribed using Pinyin:

>>> ZH.pinyin_cardinal defaultInflection 123456 :: Maybe Text
Just "shíèrwàn sānqiān sìbǎi wǔshí liù"

Using the struct functions you can see the grammatical structure of number names. Because the results of these functions are polymorphic you need to specify a specific type.

>>> import qualified Text.Numeral.Language.NL as NL
>>> NL.struct 123 :: Integer
123
>>> import Text.Numeral
>>> NL.struct 123 :: Exp
Add (Lit 100) (Add (Lit 3) (Mul (Lit 2) (Lit 10)))

Compare with:

>>> NL.cardinal defaultInflection 123 :: Maybe Text
Just "honderddrieëntwintig"

100 (honderd) + (3 (drie) + (ën) 2 (twin) * 10 (tig))


[Skip to Readme]

Properties

Versions 0.1, 0.3, 0.3.0.1, 0.4, 0.4, 0.4.1
Change log None available
Dependencies base (>=3.0.3.1 && <4.7), base-unicode-symbols (>=0.2.2 && <0.3), containers (>=0.4 && <0.6), containers-unicode-symbols (>=0.3 && <0.4), fingertree (>=0.0.1 && <0.2), integer-gmp (>=0.3 && <0.6), text (>=0.11 && <1.2) [details]
License BSD-3-Clause
Copyright 2009–2014 Roel van Dijk, Bas van Dijk
Author Roel van Dijk <vandijk.roel@gmail.com>, Bas van Dijk <v.dijk.bas@gmail.com>
Maintainer Roel van Dijk <vandijk.roel@gmail.com>
Category Natural Language Processing, Numerical, Text
Home page https://github.com/roelvandijk/numerals
Bug tracker https://github.com/roelvandijk/numerals/issues
Source repo head: git clone git://github.com/roelvandijk/numerals.git
Uploaded by RoelVanDijk at 2014-01-14T17:01:47Z

Modules

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for numerals-0.4

[back to package description]

Numerals

Convert numbers to numerals (number words) in a number of languages. Each language has its own module. The module name is based on one of the ISO 639 Alpha codes. Each module contains one or more functions to convert numerical values to numerals. Several types of numerals are supported. But not every type is supported by every language. Some because they do not occur in that language. Others because they are not yet defined in this package.

Inflection

In some languages number words are modified based on a number of grammatical categories such as gender or number. For instance, in Spanish, the numeral for the quantity '1' can be one of uno, un or una depending on whether it is of the neuter, masculine or feminine gender. In order to support this process every conversion function takes an inflection parameter which defines the grammatical state.

Inflections are not concrete types, but polymorphic parameters constrained by type classes. Use the reified inflection type provided by the numerals-base package to get a concrete value:

>>> import Text.Numeral.Grammar.Reified ( defaultInflection )

Numeral structure

The struct functions convert numbers to a polymorphic representation of their grammatical structure. They are found in every language module.

Examples

The use of this package is best understood with some examples. First some English number names, both British and US variants:

>>> import qualified Text.Numeral.Language.EN as EN
>>> EN.uk_cardinal defaultInflection 123 :: Maybe Text
Just "one hundred and twenty-three"
>>> EN.us_cardinal defaultInflection (10^50 + 42) :: Maybe Text
Just "one hundred quindecillion forty-two"

French, which contains some traces of a base 20 system:

>>> import qualified Text.Numeral.Language.FR as FR
>>> FR.cardinal defaultInflection (-99) :: Maybe Text
Just "moins quatre-vingt-dix-neuf"

Conversions can fail. Alamblak, a language spoken by a few people in Papua New Guinea, has no representation for negative numbers:

>>> import qualified Text.Numeral.Language.AMP as AMP
>>> AMP.cardinal defaultInflection (-3) :: Maybe Text
Nothing

Some languages have multiple scripts and methods for writing number names. Take Chinese for example, which can be written using Han characters or transcribed to the Latin script using Pinyin.

Traditional Chinese characters:

>>> import qualified Text.Numeral.Language.ZH as ZH
>>> ZH.trad_cardinal defaultInflection 123456 :: Maybe Text
Just "十二萬三千四百五十六"

Simplified characters for use in financial contexts:

>>> ZH.finance_simpl_cardinal defaultInflection 123456 :: Maybe Text
Just "拾贰万参仟肆伯伍拾陆"

Transcribed using Pinyin:

>>> ZH.pinyin_cardinal defaultInflection 123456 :: Maybe Text
Just "shíèrwàn sānqiān sìbǎi wǔshí liù"

In Spanish the word for the quantity '1' differs based on its gender. We convey the gender via the inflection parameter:

>>> import Text.Numeral.Grammar ( masculine, feminine, neuter )
>>> import Text.Numeral.Grammar.Reified ( defaultInflection )
>>> import qualified Text.Numeral.Language.ES as ES
>>> ES.cardinal (masculine defaultInflection) 1 :: Maybe Text
Just "un"
>>> ES.cardinal (feminine defaultInflection) 1 :: Maybe Text
Just "una"
>>> ES.cardinal (neuter defaultInflection) 1 :: Maybe Text
Just "uno"

Using the struct functions you can see the grammatical structure of number names. Because the results of these functions are polymorphic you need to specify a specific type.

>>> import qualified Text.Numeral.Language.NL as NL
>>> NL.struct 123 :: Integer
123
>>> import Text.Numeral.Exp.Reified ( Exp, showExp )
>>> showExp (NL.struct 123 :: Exp i)
Add (Lit 100) (Add (Lit 3) (Mul (Lit 2) (Lit 10)))

Compare with:

>>> NL.cardinal defaultInflection 123 :: Maybe Text
Just "honderddrieëntwintig"

100 (honderd) + (3 (drie) + (ën) 2 (twin) * 10 (tig))