Help language development. Donate to The Perl Foundation
This repository provides a Raku package with functions for the generation, parsing, and interpretation of numeric word forms in different languages.
The initial versions of the code in this repository can be found in the GitHub repository [AAr1].
The Raku package
provides word forms (cardinal, ordinal, etc.) generation in many languages.
(But at least for one language the produced forms are incorrect.)
The Raku package
also provides word forms (cardinal, ordinal, etc.) generation in English.
The parsers and interpreters of this package can be seen as complementary to the functions in [BL1, SS1].
Remark: Maybe a more complete version of this package should be merged with
Remark: I can judge the quality of the results only of the languages: Bulgarian, English, and Russian. The numeric word form interpreters for the rest of the languages pass testing, but they might have potentially many deficiencies. (That are easily detected by people who have mastered those languages.)
1. Install Raku (Perl 6) : https://raku.org/downloads .
2. Make sure you have Zef Module Installer.
zef --versionin the command line.
3. Open a command line program. (E.g. Terminal on Mac OS X.)
4. Run the command:
zef install https://github.com/antononcube/Raku-Lingua-NumericWordForms.git
Generation of numeric word forms:
use Lingua::NumericWordForms; say to-numeric-word-form(8093); say to-numeric-word-form(8093, 'Bulgarian'); # not implemented yet say to-numeric-word-form(8093, 'Russian'); # not implemented yet
Here is a screenshot of the results:
The first argument of
to-numeric-word-form can be:
Here are examples of the latter two:
say to-numeric-word-form('123; 232; 898_934').join('; '); # one hundred twenty three; two hundred thirty two; eight hundred ninety eight thousand, nine hundred thirty four say to-numeric-word-form([321, '992', 100_904]).join('; '); # three hundred twenty one; nine hundred ninety two; one hundred thousand, nine hundred four
Interpretation of numeric word forms:
use Lingua::NumericWordForms; say from-numeric-word-form('one thousand and twenty three'); say from-numeric-word-form('хиляда двадесет и три', 'Bulgarian'); say from-numeric-word-form('tysiąc dwadzieścia trzy', 'Polish'); say from-numeric-word-form('одна тысяча двадцать три', 'Russian'); say from-numeric-word-form('mil veintitrés', 'Spanish');
Here is a screenshot of the results:
from-numeric-word-form can take as a first argument:
A string that is a numeric word form
A string comprised of numeric word forms separated by ";"
A list or an array of strings
Here are corresponding examples:
say from-numeric-word-form('twenty six'); # 26 say from-numeric-word-form(['mil veintitrés', 'dos mil setenta y dos']); # (1023 2072) say from-numeric-word-form('two hundred and five; триста четиридесет и две; 二十万六十五'):p; # (english => 205 bulgarian => 342 japanese => 200065)
For more examples see the file NumericWordForms-examples.raku.
The returned result can be an
Int object or a
Str object -- that is controlled with
number (which by default is
True.) Here is an example:
my $res = from-numeric-word-form('one thousand and twenty three'); say $res, ' ', $res.WHAT; # 1023 (Int) $res = from-numeric-word-form('one thousand and twenty three', :!number); say $res, ' ', $res.WHAT; # 1023 (Str)
Automatic language detection is invoked if the second argument is 'Automatic' or not specified:
say from-numeric-word-form('tysiąc dwadzieścia trzy', 'Automatic'):p; # polish => 1023 say from-numeric-word-form(['tysiąc dwadzieścia trzy', 'twenty three']):p; # (polish => 1023 english => 23)
:p specifies whether the result should be a
Pair object or a
with the detected languages as keys.
Translation from one language to another:
say translate-numeric-word-form('хиляда двадесет и три', 'Bulgarian' => 'English');
(Currently that function translates to English only.)
This package provides (exports) roles that can be used in grammars or roles in other packages, applications, etc.
For example, see the roles:
A grammar or role that does the roles above should use the rule:
For code examples see the file Parsing-examples.raku.
Remark: The role
Lingua::NumericWordForms::Roles::WordedNumberSpec and the corresponding
Lingua::NumericWordForms::Actions::WordedNumberSpec are "abstract".
They were introduced in order to have simpler roles and actions code
(and non-duplicated implementations.) Hence, that role and class should not be used in
grammars and roles outside of this package.
The following TODO items are ordered by priority, the most important are on top.
[ ] Expand parsing beyond trillions
[X] Automatic determination of the language
[ ] Word form generation:
[ ] Documentation of the general programming approach.
[ ] Full, consistent Persian numbers parsing.
[X] General strategy for parsing and interpretation of numeric word forms of East Asia languages
[ ] Implement parsing of ordinal numeric word forms
[ ] Implement parsing of year "shortcut" word forms, like "twenty o three"
[ ] Implement parsing of numeric word forms for rationals, like "five twelfths"
[X] Translation function (from one language to another)
The main rule is that the main branch should always be installable and pass all of its tests.
From the main rule it follows that new features are developed in separate branches or forks.
The easiest way to collaborate is to create and commit new test files or corrections to existing test files.
Please use Conventional Commits (CC).
Here is the CC short form stencil (in Raku):
<type> ['(' <scope> ')']? ':' <description>.
See the recent commits in this repository for examples.
Here are additional examples of CC messages (each line is a separate message):
feat:Implemented the parsing of Danish numeric word forms. docs:Added documentation of right-to-left word forms parsing. fix(Persian):Corrected tests for numbers larger that 1000. test:Added new corner cases tests. test(Ukrainian):Added new tests.
[AAr1] Anton Antonov, Raku::DSL::Shared.
[BL1] Brent "Labster" Laabs,
[SS1] Larry Wall, Steve Schulze, Lingua::EN::Numbers.