Giter Site home page Giter Site logo

p6-lingua-number's Introduction

Actions Status

NAME

Lingua::Number - Write cardinal and ordinal numbers with words in over fifty languages

SYNOPSIS

use Lingua::Number;

my $number = 451;
my $language = 'en';
say cardinal($number, $language);	# "four hundred fifty-one"
say ordinal($number, $language);	# "four hundred fifty-first"
say ordinal-digits($number, $language) 	# "451st"

say cardinal 764013, 'ja';			# "七十六万四千十三"
say cardinal 54321, 'es', gender => 'F';	# "cincuenta y cuatro mil tres­cientas veintiuna"
say cardinal 1.23;				# English is default, prints "one point two three"

say roman-numeral Date.today.year;		# "MMXIII" when I wrote this
say rule2text 'en', 'spellout-numbering-year', Date.today.year;   # "twenty thirteen"

DESCRIPTION

This module takes an integer input, and translates it into a natural language. English is the default language, but 60 other languages are supported. The module currently tests for 'en', 'es', and 'ja' (kanji) translations, but any of the languages available in the lib/Lingua/Number/rbnf-xml directory are available for use.

EXPORTED SUBROUTINES

cardinal

cardinal ($number, $language = 'en', :$gender = '', :$slang = '')

Returns the number written as a cardinal (counting) number. Gender options include 'masculine', 'feminine', and 'neuter', though really any string beginning in 'm', 'f', or 'n' will dwim. Slangs will vary by language; see Lingua-Number-rulesets to look at them.

ordinal

ordinal ($number, $language = 'en', :$gender = '', :$slang = '')

Returns the number written as an ordinal (ranking) number.

roman-numeral

roman-numeral ($number)

Returns the number in classy roman numerals.

rule2text

rule2text (Str $lingua, Str $ruletype, $number)

This is the function which does most of the work for cardinal and ordinal. What it does not do is figure out which rule to call. You need to figure that out yourself, by looking at the XML files or calling Lingua-Number-rulesets. Note that private rules are prefixed with '%' in the internal data, if you want to use them for some reason. Also note that the arguments are reversed, because, well, who knows.

Anyway, this is mainly exported to aid in developing new rules.

Lingua-Number-rulesets

Lingua-Number-rulesets (Str $lingua)

Returns an array of rulesets available to use by rule2text in the given language. Mostly for debugging purposes. Rulesets beginning in '%' are subrules, and usually should not be used to format full numbers.

USAGE NOTES

Note that whenever you use a language for the first time, it will take much longer to load. This is normal, because we need to parse the XML files. If you want to preload a language for some reason, just call cardinal(0, $language).

TODO

  • Handle Inf/NaN cases.

  • Write tests for some more languages, especially for gender.

  • Write some English fractional rules.

AUTHOR

Brent Laabs

COPYRIGHT AND LICENSE

Copyright 2013 - 2018 Brent Laabs

Copyright 2024 Raku Community

The code under the same terms as Raku; see the LICENSE file for details.

Rule-Based Number Format XML data from the Unicode CLDR project is licensed under the Unicode License; see unicode-license.txt for details. These files are from CLDR 22.1; translations to JSON are included. Modified files: "ja.xml" to add a romaji translation.

p6-lingua-number's People

Contributors

altai-man avatar colomon avatar labster avatar lizmat avatar zoffixznet avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

p6-lingua-number's Issues

Does not produce correct results in v2021.03.

This package does not produce correct results in Raku version 2021.03.

For example:

‌‌say cardinal(1234, 'bg');
# Invalid ruleset spellout-cardinal for language bg.
# ...

‌‌say cardinal(1234, 'en'); 
# thousand

say ordinal(1234, 'en');
#  thousand
‌‌
say ordinal(1234);
#  thousand

Failing tests

Test Summary Report
-------------------
t/00-basic.t     (Wstat: 1280 Tests: 5 Failed: 5)
  Failed tests:  1-5
  Non-zero exit status: 5
  Parse errors: Bad plan.  You planned 16 tests but ran 5.
t/01-ordinal-en.t (Wstat: 3328 Tests: 13 Failed: 13)
  Failed tests:  1-13
  Non-zero exit status: 13
  Parse errors: No plan found in TAP output

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.