From 3f6438b1082e0d01a7f94c8a6a04ce41a8a168ab Mon Sep 17 00:00:00 2001 From: tabatad <91326381+tabatad@users.noreply.github.com> Date: Fri, 24 Nov 2023 20:56:52 +0900 Subject: [PATCH] Added Japanese support (#148) * add japanese test * Add Japanese integer support * Add Japanese long support * Add Japanese long support * Add JapaneseNumberChunkingTest * Add Japanese BigDecimal support * Fix JapaneseNumberChunkingTest * Fix README for Japanese support * Fix Container name --- README.md | 1 + .../tradukisto/LongValueConverters.java | 4 +- .../finance/tradukisto/MoneyConverters.java | 4 +- .../finance/tradukisto/ValueConverters.java | 3 +- .../tradukisto/internal/Container.java | 28 ++++ .../JapaneseNumberToWordsConverter.java | 38 ++++++ .../japanese/JapanesePluralForms.java | 23 ++++ .../JapaneseThousandToWordsConverter.java | 54 ++++++++ .../languages/japanese/JapaneseValues.java | 78 +++++++++++ .../support/JapaneseNumberChunking.java | 21 +++ .../tradukisto/LongValueConvertersTest.groovy | 2 + .../tradukisto/MoneyConvertersTest.groovy | 2 + .../tradukisto/ValueConvertersTest.groovy | 5 + .../japanese/JapaneseValuesTest.groovy | 129 ++++++++++++++++++ .../support/JapaneseNumberChunkingTest.groovy | 24 ++++ 15 files changed, 413 insertions(+), 3 deletions(-) create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseNumberToWordsConverter.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapanesePluralForms.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseThousandToWordsConverter.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValues.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunking.java create mode 100644 src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValuesTest.groovy create mode 100644 src/test/groovy/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunkingTest.groovy diff --git a/README.md b/README.md index 21857216..4d12e662 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,7 @@ With Tradukisto, you can now transform numbers into their word equivalents easy * 🇩🇪 German * 🇮🇳 Hindi * 🇮🇹 Italian +* 🇯🇵 Japanese (KANJI) * 🇰🇿 Kazakh * 🇱🇻 Latvian * 🇵🇱 Polish diff --git a/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java b/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java index 3f52bb4d..5927950a 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java @@ -8,6 +8,7 @@ import static pl.allegro.finance.tradukisto.internal.Container.polishContainer; import static pl.allegro.finance.tradukisto.internal.Container.hindiContainer; import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer; +import static pl.allegro.finance.tradukisto.internal.Container.japaneseKanjiContainer; public enum LongValueConverters { @@ -15,7 +16,8 @@ public enum LongValueConverters { ENGLISH_LONG(englishContainer().getLongConverter()), POLISH_LONG(polishContainer().getLongConverter()), HINDI_LONG(hindiContainer().getLongConverter()), - SWEDISH_LONG(swedishContainer().getLongConverter()); + SWEDISH_LONG(swedishContainer().getLongConverter()), + JAPANESE_KANJI_LONG(japaneseKanjiContainer().getLongConverter()); private final LongToStringConverter converter; diff --git a/src/main/java/pl/allegro/finance/tradukisto/MoneyConverters.java b/src/main/java/pl/allegro/finance/tradukisto/MoneyConverters.java index cbec2675..c61a4edc 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/MoneyConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/MoneyConverters.java @@ -14,6 +14,7 @@ import static pl.allegro.finance.tradukisto.internal.Container.frenchContainer; import static pl.allegro.finance.tradukisto.internal.Container.germanContainer; import static pl.allegro.finance.tradukisto.internal.Container.italianContainer; +import static pl.allegro.finance.tradukisto.internal.Container.japaneseKanjiContainer; import static pl.allegro.finance.tradukisto.internal.Container.kazakhContainer; import static pl.allegro.finance.tradukisto.internal.Container.latvianContainer; import static pl.allegro.finance.tradukisto.internal.Container.polishContainer; @@ -53,7 +54,8 @@ public enum MoneyConverters { DUTCH_BANKING_MONEY_VALUE(dutchContainer().getBankingMoneyConverter()), SPANISH_BANKING_MONEY_VALUE(spanishContainer().getBankingMoneyConverter()), HINDI_BANKING_MONEY_VALUE(Container.hindiContainer().getBankingMoneyConverter()), - SWEDISH_BANKING_MONEY_VALUE(swedishContainer().getBankingMoneyConverter()); + SWEDISH_BANKING_MONEY_VALUE(swedishContainer().getBankingMoneyConverter()), + JAPANESE_BANKING_MONEY_VALUE(japaneseKanjiContainer().getBankingMoneyConverter()); private final BigDecimalToStringConverter converter; diff --git a/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java b/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java index 5f84482a..33846aaf 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java @@ -33,7 +33,8 @@ public enum ValueConverters { SLOVENE_INTEGER(Container.sloveneContainer().getIntegerConverter(), "sl"), SPANISH_INTEGER(Container.spanishContainer().getIntegerConverter(), "es"), HINDI_INTEGER(Container.hindiContainer().getIntegerConverter(), "hi"), - SWEDISH_INTEGER(Container.swedishContainer().getIntegerConverter(), "sv"); + SWEDISH_INTEGER(Container.swedishContainer().getIntegerConverter(), "sv"), + JAPANESE_INTEGER(Container.japaneseKanjiContainer().getIntegerConverter(), "ja"); private final IntegerToStringConverter converter; private final List languageCodes; diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java b/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java index efbe4403..d9bf4566 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java @@ -25,6 +25,9 @@ import pl.allegro.finance.tradukisto.internal.languages.italian.ItalianIntegerToWordsConverter; import pl.allegro.finance.tradukisto.internal.languages.italian.ItalianThousandToWordsConverter; import pl.allegro.finance.tradukisto.internal.languages.italian.ItalianValues; +import pl.allegro.finance.tradukisto.internal.languages.japanese.JapaneseNumberToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.japanese.JapaneseThousandToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.japanese.JapaneseValues; import pl.allegro.finance.tradukisto.internal.languages.kazakh.KazakhValues; import pl.allegro.finance.tradukisto.internal.languages.latvian.LatvianValues; import pl.allegro.finance.tradukisto.internal.languages.polish.PolishValues; @@ -319,6 +322,31 @@ public static Container swedishContainer() { return new Container(swedishNumberToWordsConverter, swedishNumberToWordsConverter, swedishBigDecimalConverter); } + public static Container japaneseKanjiContainer() { + JapaneseValues japaneseValues = new JapaneseValues(); + + JapaneseThousandToWordsConverter japaneseThousandToWordsConverter = new JapaneseThousandToWordsConverter( + japaneseValues.baseNumbers() + ); + + IntegerToStringConverter integerToStringConverter = new JapaneseNumberToWordsConverter( + japaneseThousandToWordsConverter, + japaneseValues.pluralForms() + ); + + LongToStringConverter longToStringConverter = new JapaneseNumberToWordsConverter( + japaneseThousandToWordsConverter, + japaneseValues.pluralForms() + ); + + BigDecimalToBankingMoneyConverter bigDecimalToBankingMoneyConverter = new BigDecimalToBankingMoneyConverter( + integerToStringConverter, + japaneseValues.currency() + ); + + return new Container(integerToStringConverter, longToStringConverter, bigDecimalToBankingMoneyConverter); + } + private final IntegerToStringConverter integerConverter; private final LongToStringConverter longConverter; private final BigDecimalToStringConverter bigDecimalConverter; diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseNumberToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseNumberToWordsConverter.java new file mode 100644 index 00000000..014f0fdb --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseNumberToWordsConverter.java @@ -0,0 +1,38 @@ +package pl.allegro.finance.tradukisto.internal.languages.japanese; + +import pl.allegro.finance.tradukisto.internal.converters.NumberToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.PluralForms; +import pl.allegro.finance.tradukisto.internal.support.Assert; +import pl.allegro.finance.tradukisto.internal.support.JapaneseNumberChunking; +import pl.allegro.finance.tradukisto.internal.support.NumberChunking; + +import java.util.List; + +public class JapaneseNumberToWordsConverter extends NumberToWordsConverter { + + private final NumberChunking numberChunking = new JapaneseNumberChunking(); + + private final List pluralForms; + + public JapaneseNumberToWordsConverter(JapaneseThousandToWordsConverter japaneseThousandToWordsConverter, List pluralForms) { + super(japaneseThousandToWordsConverter, pluralForms); + this.pluralForms = pluralForms; + } + + @Override + public String asWords(Long value) { + Assert.isTrue(value >= 0, () -> String.format("can't convert negative numbers for value %d", value)); + + List valueChunks = numberChunking.chunk(value); + List formsToUse = getRequiredFormsInReversedOrder(valueChunks.size()); + + return joinValueChunksWithForms(valueChunks.iterator(), formsToUse.iterator()); + } + + @Override + protected String joinParts(List result) { + return result.isEmpty() + ? hundredsToWordsConverter.asWords(0, pluralForms.get(0).genderType()) + : String.join("", result).trim(); + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapanesePluralForms.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapanesePluralForms.java new file mode 100644 index 00000000..981c4f0e --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapanesePluralForms.java @@ -0,0 +1,23 @@ +package pl.allegro.finance.tradukisto.internal.languages.japanese; + +import pl.allegro.finance.tradukisto.internal.languages.GenderType; +import pl.allegro.finance.tradukisto.internal.languages.PluralForms; + +public class JapanesePluralForms implements PluralForms { + + private final String form; + + public JapanesePluralForms(String form) { + this.form = form; + } + + @Override + public String formFor(Integer value) { + return form; + } + + @Override + public GenderType genderType() { + return GenderType.NON_APPLICABLE; + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseThousandToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseThousandToWordsConverter.java new file mode 100644 index 00000000..5628b98d --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseThousandToWordsConverter.java @@ -0,0 +1,54 @@ +package pl.allegro.finance.tradukisto.internal.languages.japanese; + +import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; +import pl.allegro.finance.tradukisto.internal.languages.GenderForms; +import pl.allegro.finance.tradukisto.internal.languages.GenderType; +import pl.allegro.finance.tradukisto.internal.support.Range; + +import java.util.Map; + +import static java.lang.String.format; + +public class JapaneseThousandToWordsConverter implements IntegerToStringConverter { + + private final Map baseValues; + + public JapaneseThousandToWordsConverter(Map baseValues) { + this.baseValues = baseValues; + } + + @Override + public String asWords(Integer value) { + if (baseValues.containsKey(value)) { + return baseValues.get(value).formFor(GenderType.NON_APPLICABLE); + } + if (Range.closed(11, 99).contains(value)) { + return twoDigitsNumberAsString(value); + } + if (Range.closed(101, 999).contains(value)) { + return threeDigitsNumberAsString(value); + } + if (Range.closed(1001, 9999).contains(value)) { + return fourDigitsNumberAsString(value); + } + throw new IllegalArgumentException(format("Can't convert %d", value)); + } + + private String twoDigitsNumberAsString(Integer value) { + Integer units = value % 10; + Integer tens = value - units; + return format("%s%s", asWords(tens), asWords(units)); + } + + private String threeDigitsNumberAsString(Integer value) { + Integer units = value % 100; + Integer hundreds = value - units; + return format("%s%s", asWords(hundreds), asWords(units)); + } + + private String fourDigitsNumberAsString(Integer value) { + Integer units = value % 1000; + Integer thousands = value - units; + return format("%s%s", asWords(thousands), asWords(units)); + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValues.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValues.java new file mode 100644 index 00000000..cc104570 --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValues.java @@ -0,0 +1,78 @@ +package pl.allegro.finance.tradukisto.internal.languages.japanese; + +import pl.allegro.finance.tradukisto.internal.BaseValues; +import pl.allegro.finance.tradukisto.internal.languages.GenderForms; +import pl.allegro.finance.tradukisto.internal.languages.PluralForms; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static pl.allegro.finance.tradukisto.internal.support.BaseNumbersBuilder.baseNumbersBuilder; + +public class JapaneseValues implements BaseValues { + + @Override + public Map baseNumbers() { + return baseNumbersBuilder() + .put(0, "零") + .put(1, "一") + .put(2, "二") + .put(3, "三") + .put(4, "四") + .put(5, "五") + .put(6, "六") + .put(7, "七") + .put(8, "八") + .put(9, "九") + .put(10, "十") + .put(20, "二十") + .put(30, "三十") + .put(40, "四十") + .put(50, "五十") + .put(60, "六十") + .put(70, "七十") + .put(80, "八十") + .put(90, "九十") + .put(100, "百") + .put(200, "二百") + .put(300, "三百") + .put(400, "四百") + .put(500, "五百") + .put(600, "六百") + .put(700, "七百") + .put(800, "八百") + .put(900, "九百") + .put(1000, "千") + .put(2000, "二千") + .put(3000, "三千") + .put(4000, "四千") + .put(5000, "五千") + .put(6000, "六千") + .put(7000, "七千") + .put(8000, "八千") + .put(9000, "九千") + .build(); + } + + @Override + public List pluralForms() { + return Arrays.asList( + new JapanesePluralForms(""), + new JapanesePluralForms("万"), + new JapanesePluralForms("億"), + new JapanesePluralForms("兆"), + new JapanesePluralForms("京") + ); + } + + @Override + public String currency() { + return "円"; + } + + @Override + public char twoDigitsNumberSeparator() { + return ' '; + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunking.java b/src/main/java/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunking.java new file mode 100644 index 00000000..ee331b25 --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunking.java @@ -0,0 +1,21 @@ +package pl.allegro.finance.tradukisto.internal.support; + +import java.util.LinkedList; +import java.util.List; + +public class JapaneseNumberChunking extends NumberChunking { + + private static final int JAPANESE_SPLIT_FACTOR = 1_0000; + + @Override + public List chunk(Long value) { + LinkedList result = new LinkedList<>(); + + while (value > 0) { + result.addFirst((int) (value % JAPANESE_SPLIT_FACTOR)); + value /= JAPANESE_SPLIT_FACTOR; + } + + return result; + } +} diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/LongValueConvertersTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/LongValueConvertersTest.groovy index 9bdf3bcd..4fbb48cb 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/LongValueConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/LongValueConvertersTest.groovy @@ -7,6 +7,7 @@ import static pl.allegro.finance.tradukisto.LongValueConverters.ENGLISH_LONG import static pl.allegro.finance.tradukisto.LongValueConverters.HINDI_LONG import static pl.allegro.finance.tradukisto.LongValueConverters.POLISH_LONG import static pl.allegro.finance.tradukisto.LongValueConverters.SWEDISH_LONG +import static pl.allegro.finance.tradukisto.LongValueConverters.JAPANESE_KANJI_LONG class LongValueConvertersTest extends Specification { @@ -21,6 +22,7 @@ class LongValueConvertersTest extends Specification { "Polish" | POLISH_LONG || "jeden trylion" "Hindi" | HINDI_LONG || "दस शंख" "Swedish" | SWEDISH_LONG || "en triljon" + "Japanese"| JAPANESE_KANJI_LONG || "百京" } def "should throw exception when null given"() { diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/MoneyConvertersTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/MoneyConvertersTest.groovy index 04e4440e..f8e11da0 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/MoneyConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/MoneyConvertersTest.groovy @@ -12,6 +12,7 @@ import static pl.allegro.finance.tradukisto.MoneyConverters.FRENCH_BANKING_MONEY import static pl.allegro.finance.tradukisto.MoneyConverters.GERMAN_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.HINDI_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.ITALIAN_BANKING_MONEY_VALUE +import static pl.allegro.finance.tradukisto.MoneyConverters.JAPANESE_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.KAZAKH_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.LATVIAN_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.POLISH_BANKING_MONEY_VALUE @@ -44,6 +45,7 @@ class MoneyConvertersTest extends Specification { "French" | FRENCH_BANKING_MONEY_VALUE || "mille deux cent trente-quatre € 56/100" "German" | GERMAN_BANKING_MONEY_VALUE || "eintausendzweihundertvierunddreißig € 56/100" "Italian" | ITALIAN_BANKING_MONEY_VALUE || "milleduecentotrentaquattro € 56/100" + "Japanese" | JAPANESE_BANKING_MONEY_VALUE || "千二百三十四 円 56/100" "Kazakh" | KAZAKH_BANKING_MONEY_VALUE || "бір мың екі жүз отыз төрт KZT 56/100" "Latvian" | LATVIAN_BANKING_MONEY_VALUE || "viens tūkstotis divi simti trīsdesmit četri EUR 56/100" "Polish" | POLISH_BANKING_MONEY_VALUE || "jeden tysiąc dwieście trzydzieści cztery PLN 56/100" diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy index 4931f6a7..131fd5cc 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy @@ -11,6 +11,7 @@ import static pl.allegro.finance.tradukisto.ValueConverters.FRENCH_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.GERMAN_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.HINDI_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.ITALIAN_INTEGER +import static pl.allegro.finance.tradukisto.ValueConverters.JAPANESE_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.KAZAKH_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.LATVIAN_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.POLISH_INTEGER @@ -44,6 +45,7 @@ class ValueConvertersTest extends Specification { "French" | FRENCH_INTEGER || "mille deux cent trente-quatre" "German" | GERMAN_INTEGER || "eintausendzweihundertvierunddreißig" "Italian" | ITALIAN_INTEGER || "milleduecentotrentaquattro" + "Japanese" | JAPANESE_INTEGER || "千二百三十四" "Kazakh" | KAZAKH_INTEGER || "бір мың екі жүз отыз төрт" "Latvian" | LATVIAN_INTEGER || "viens tūkstotis divi simti trīsdesmit četri" "Polish" | POLISH_INTEGER || "jeden tysiąc dwieście trzydzieści cztery" @@ -103,6 +105,8 @@ class ValueConvertersTest extends Specification { new Locale("nl") || DUTCH_INTEGER new Locale("hi") || HINDI_INTEGER new Locale("sv") || SWEDISH_INTEGER + new Locale("ja") || JAPANESE_INTEGER + Locale.JAPANESE || JAPANESE_INTEGER } def "should return supplied default converter when locale is unknown"() { @@ -152,6 +156,7 @@ class ValueConvertersTest extends Specification { "uk" || UKRAINIAN_INTEGER "hi" || HINDI_INTEGER "sv" || SWEDISH_INTEGER + "ja" || JAPANESE_INTEGER } def "should return supplied default converter when languageCode is unknown"() { diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValuesTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValuesTest.groovy new file mode 100644 index 00000000..05b3b319 --- /dev/null +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/japanese/JapaneseValuesTest.groovy @@ -0,0 +1,129 @@ +package pl.allegro.finance.tradukisto.internal.languages.japanese + +import spock.lang.Ignore +import spock.lang.Specification +import spock.lang.Unroll + +import static pl.allegro.finance.tradukisto.internal.Container.japaneseKanjiContainer + +class JapaneseValuesTest extends Specification { + + static intConverter = japaneseKanjiContainer().getIntegerConverter() + static longConverter = japaneseKanjiContainer().getLongConverter() + + @Unroll + def "should convert #value to '#words' in Japanese"() { + expect: + intConverter.asWords(value) == words + + where: + value | words + 0 | "零" + 1 | "一" + 2 | "二" + 3 | "三" + 4 | "四" + 5 | "五" + 6 | "六" + 7 | "七" + 8 | "八" + 9 | "九" + + 11 | "十一" + 12 | "十二" + 13 | "十三" + 14 | "十四" + 15 | "十五" + 16 | "十六" + 17 | "十七" + 18 | "十八" + 19 | "十九" + + 10 | "十" + 20 | "二十" + 30 | "三十" + 40 | "四十" + 50 | "五十" + 60 | "六十" + 70 | "七十" + 80 | "八十" + 90 | "九十" + + 21 | "二十一" + 37 | "三十七" + 43 | "四十三" + 58 | "五十八" + 69 | "六十九" + 76 | "七十六" + 82 | "八十二" + 95 | "九十五" + + 100 | "百" + 200 | "二百" + 300 | "三百" + 400 | "四百" + 500 | "五百" + 600 | "六百" + 700 | "七百" + 800 | "八百" + 900 | "九百" + + 111 | "百十一" + 272 | "二百七十二" + 387 | "三百八十七" + 448 | "四百四十八" + 569 | "五百六十九" + 625 | "六百二十五" + 782 | "七百八十二" + 895 | "八百九十五" + 999 | "九百九十九" + + 1_000 | "千" + 2_000 | "二千" + 3_000 | "三千" + 4_000 | "四千" + 5_000 | "五千" + 7_634 | "七千六百三十四" + 11_000 | "一万千" + 15_000 | "一万五千" + 21_000 | "二万千" + 24_190 | "二万四千百九十" + 653_000 | "六十五万三千" + 123_454 | "十二万三千四百五十四" + 700_000 | "七十万" + 999_999 | "九十九万九千九百九十九" + + 1_000_000 | "百万" + 2_000_000 | "二百万" + 5_000_000 | "五百万" + 23_437_219 | "二千三百四十三万七千二百十九" + 100_000_000 | "一億" + 123_456_789 | "一億二千三百四十五万六千七百八十九" + 322_089_890 | "三億二千二百八万九千八百九十" + + 1_000_000_000 | "十億" + 2_147_483_647 | "二十一億四千七百四十八万三千六百四十七" + } + + @Unroll + def "should convert long #value to '#words' in Japanese"() { + expect: + longConverter.asWords(value) == words + + where: + value | words + 5_000_000_000 | "五十億" + + 1_000_000_000_000 | "一兆" + 2_000_000_000_000 | "二兆" + 5_000_000_000_000 | "五兆" + + 1_000_000_000_000_000 | "千兆" + 2_000_000_000_000_000 | "二千兆" + 5_000_000_000_000_000 | "五千兆" + + 1_000_000_000_000_000_000 | "百京" + 2_000_000_000_000_000_000 | "二百京" + Long.MAX_VALUE | "九百二十二京三千三百七十二兆三百六十八億五千四百七十七万五千八百七" + } +} \ No newline at end of file diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunkingTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunkingTest.groovy new file mode 100644 index 00000000..f600bc54 --- /dev/null +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/support/JapaneseNumberChunkingTest.groovy @@ -0,0 +1,24 @@ +package pl.allegro.finance.tradukisto.internal.support + +import spock.lang.Specification + +class JapaneseNumberChunkingTest extends Specification { + + def splitter = new JapaneseNumberChunking() + + def "should split number to four digit parts as per japanese standards"() { + expect: + splitter.chunk(value) == chunks + + where: + value | chunks + 0 | [] + 123 | [123] + 1234 | [1234] + 123000789 | [1, 2300, 789] + 123456789 | [1, 2345, 6789] + 123000000 | [1, 2300, 0000] + 12300000 | [1230, 0000] + Long.MAX_VALUE | [922, 3372, 368, 5477, 5807] + } +}