From fcfe03bfcbc863a05a5550573e6eb52ac2b14ddc Mon Sep 17 00:00:00 2001 From: Adam Ross <14985050+R055A@users.noreply.github.com> Date: Mon, 2 Oct 2023 04:55:17 +1300 Subject: [PATCH 1/4] Add Swedish support for number to word conversion --- .../tradukisto/LongValueConverters.java | 4 +- .../finance/tradukisto/MoneyConverters.java | 5 +- .../finance/tradukisto/ValueConverters.java | 3 +- .../tradukisto/internal/Container.java | 24 +++ .../SwedishIntegerToWordsConverter.java | 24 +++ .../swedish/SwedishLongToWordsConverter.java | 30 ++++ .../SwedishThousandToWordsConverter.java | 54 +++++++ .../languages/swedish/SwedishValues.java | 75 +++++++++ .../support/SwedishNumberChunking.java | 26 +++ .../tradukisto/LongValueConvertersTest.groovy | 2 + .../tradukisto/MoneyConvertersTest.groovy | 2 + .../tradukisto/ValueConvertersTest.groovy | 4 + .../swedish/SwedishIntegerValuesTest.groovy | 152 ++++++++++++++++++ ...SwedishThousandToWordsConverterTest.groovy | 88 ++++++++++ 14 files changed, 490 insertions(+), 3 deletions(-) create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java create mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java create mode 100644 src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy create mode 100644 src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy diff --git a/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java b/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java index 45f6faff..3f52bb4d 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/LongValueConverters.java @@ -7,13 +7,15 @@ import static pl.allegro.finance.tradukisto.internal.Container.englishContainer; 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; public enum LongValueConverters { CROATIAN_LONG(croatianContainer().getLongConverter()), ENGLISH_LONG(englishContainer().getLongConverter()), POLISH_LONG(polishContainer().getLongConverter()), - HINDI_LONG(hindiContainer().getLongConverter()); + HINDI_LONG(hindiContainer().getLongConverter()), + SWEDISH_LONG(swedishContainer().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 d2f5ef15..0b7032e2 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/MoneyConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/MoneyConverters.java @@ -21,10 +21,12 @@ import static pl.allegro.finance.tradukisto.internal.Container.serbianCyrillicContainer; import static pl.allegro.finance.tradukisto.internal.Container.serbianContainer; import static pl.allegro.finance.tradukisto.internal.Container.slovakContainer; +import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer; import static pl.allegro.finance.tradukisto.internal.Container.turkishContainer; import static pl.allegro.finance.tradukisto.internal.Container.ukrainianContainer; import static pl.allegro.finance.tradukisto.internal.Container.bulgarianContainer; import static pl.allegro.finance.tradukisto.internal.Container.dutchContainer; + public enum MoneyConverters { BRAZILIAN_PORTUGUESE_BANKING_MONEY_VALUE(brazilianPortugueseContainer().getBankingMoneyConverter()), @@ -46,7 +48,8 @@ public enum MoneyConverters { FRENCH_BANKING_MONEY_VALUE(frenchContainer().getBankingMoneyConverter()), BULGARIAN_BANKING_MONEY_VALUE(bulgarianContainer().getBankingMoneyConverter()), DUTCH_BANKING_MONEY_VALUE(dutchContainer().getBankingMoneyConverter()), - HINDI_BANKING_MONEY_VALUE(Container.hindiContainer().getBankingMoneyConverter()); + HINDI_BANKING_MONEY_VALUE(Container.hindiContainer().getBankingMoneyConverter()), + SWEDISH_BANKING_MONEY_VALUE(swedishContainer().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 26442659..aaea2847 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java +++ b/src/main/java/pl/allegro/finance/tradukisto/ValueConverters.java @@ -30,7 +30,8 @@ public enum ValueConverters { FRENCH_INTEGER(Container.frenchContainer().getIntegerConverter(), "fr"), TURKISH_INTEGER(Container.turkishContainer().getIntegerConverter(), "tr"), DUTCH_INTEGER(Container.dutchContainer().getIntegerConverter(), "nl"), - HINDI_INTEGER(Container.hindiContainer().getIntegerConverter(), "hi"); + HINDI_INTEGER(Container.hindiContainer().getIntegerConverter(), "hi"), + SWEDISH_INTEGER(Container.swedishContainer().getIntegerConverter(), "sv"); 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 22fddd5b..c9a214bd 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java @@ -37,6 +37,10 @@ import pl.allegro.finance.tradukisto.internal.languages.serbian.SerbianValues; import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValues; import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValuesForSmallNumbers; +import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishIntegerToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishLongToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishThousandToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishValues; import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishBigDecimalToBankingMoneyConverter; import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishIntegerToWordsConverter; import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishSmallNumbersToWordsConverter; @@ -245,6 +249,26 @@ public static Container hindiContainer() { return new Container(integerToStringConverter, longValueConverters, bigDecimalConverter); } + public static Container swedishContainer() { + SwedishValues swedishValues = new SwedishValues(); + SwedishThousandToWordsConverter swedishThousandToWordsConverter = new SwedishThousandToWordsConverter( + swedishValues.baseNumbers()); + + IntegerToStringConverter integerToStringConverter = new SwedishIntegerToWordsConverter( + new NumberToWordsConverter(swedishThousandToWordsConverter, swedishValues.pluralForms()), + swedishThousandToWordsConverter); + + BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter = new BigDecimalToBankingMoneyConverter( + integerToStringConverter, + swedishValues.currency()); + + NumberToWordsConverter longValueConverters = new SwedishLongToWordsConverter( + swedishThousandToWordsConverter, + swedishValues.pluralForms()); + + return new Container(integerToStringConverter, longValueConverters, bigDecimalBankingMoneyValueConverter); + } + private final IntegerToStringConverter integerConverter; private final LongToStringConverter longConverter; private final BigDecimalToStringConverter bigDecimalConverter; diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java new file mode 100644 index 00000000..711aca4a --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java @@ -0,0 +1,24 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish; + +import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter; +import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; +import pl.allegro.finance.tradukisto.internal.NumberProcessor; + +public class SwedishIntegerToWordsConverter implements IntegerToStringConverter { + + private final IntegerToStringConverter swedishBigIntegersConverter; + private final GenderAwareIntegerToStringConverter swedishSmallIntegersConverter; + + public SwedishIntegerToWordsConverter(IntegerToStringConverter swedishBigIntegersConverter, + GenderAwareIntegerToStringConverter swedishSmallIntegersConverter) { + this.swedishBigIntegersConverter = swedishBigIntegersConverter; + this.swedishSmallIntegersConverter = swedishSmallIntegersConverter; + } + + @Override + public String asWords(Integer value) { + Integer bigNum = value / 1000000; + Integer smallNum = value % 1000000; + return new NumberProcessor(this.swedishBigIntegersConverter, this.swedishSmallIntegersConverter).process(bigNum, smallNum); + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java new file mode 100644 index 00000000..cef71478 --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java @@ -0,0 +1,30 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish; + +import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter; +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.NumberChunking; +import pl.allegro.finance.tradukisto.internal.support.SwedishNumberChunking; + +import java.util.List; + +public class SwedishLongToWordsConverter extends NumberToWordsConverter { + + private final NumberChunking swedishNumberChunking = new SwedishNumberChunking(); + + public SwedishLongToWordsConverter(GenderAwareIntegerToStringConverter hundredsToWordsConverter, + List pluralForms) { + super(hundredsToWordsConverter, pluralForms); + } + + @Override + public String asWords(Long value) { + Assert.isTrue(value >= 0, () -> String.format("can't convert negative numbers for value %d", value)); + + List valueChunks = swedishNumberChunking.chunk(value); + List formsToUse = getRequiredFormsInReversedOrder(valueChunks.size()); + + return joinValueChunksWithForms(valueChunks.iterator(), formsToUse.iterator()); + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java new file mode 100644 index 00000000..74f6d547 --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java @@ -0,0 +1,54 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish; + +import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter; +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 SwedishThousandToWordsConverter implements GenderAwareIntegerToStringConverter { + + private final Map swedishBaseValues; + + public SwedishThousandToWordsConverter(Map swedishBaseValues) { + this.swedishBaseValues = swedishBaseValues; + } + + @Override + public String asWords(Integer value, GenderType swedishGenderType) { + if (this.swedishBaseValues.containsKey(value)) { + return this.swedishBaseValues.get(value).formFor(swedishGenderType); + } else if (Range.closed(21, 99).contains(value)) { + return twoDigitsNumberAsString(value, swedishGenderType); + } else if (Range.closed(101, 999).contains(value)) { + return threeDigitsNumberAsString(value, swedishGenderType); + } else if (Range.closed(1000, 999999).contains(value)) { + return thousandsAsString(value, swedishGenderType); + } + throw new IllegalArgumentException(format("Can't convert %d", value)); + } + + private String twoDigitsNumberAsString(Integer value, GenderType genderType) { + Integer units = value % 10; + Integer tens = value - units; + return format("%s%s", asWords(tens, genderType), asWords(units, genderType)); + } + + private String threeDigitsNumberAsString(Integer value, GenderType genderType) { + Integer tensWithUnits = value % 100; + Integer hundreds = value - tensWithUnits; + return format("%s och %s", asWords(hundreds, genderType), asWords(tensWithUnits, genderType)); + } + + private String thousandsAsString(Integer value, GenderType genderType) { + Integer thousands = value / 1000; + Integer other = value % 1000; + + if (other == 0) + return format("%s tusen", asWords(thousands, genderType)); + return format("%s tusen %s", asWords(thousands, genderType), asWords(other, genderType)); + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java new file mode 100644 index 00000000..8755e145 --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java @@ -0,0 +1,75 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish; + +import pl.allegro.finance.tradukisto.internal.languages.GenderForms; +import pl.allegro.finance.tradukisto.internal.languages.GenderType; +import pl.allegro.finance.tradukisto.internal.languages.PluralForms; +import pl.allegro.finance.tradukisto.internal.languages.RegularPluralForms; + +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms; +import static pl.allegro.finance.tradukisto.internal.support.BaseNumbersBuilder.baseNumbersBuilder; + +public class SwedishValues { + + // Note: Swedish uses 'common' and 'neuter' gender types, but these seem to transfer nicely for numbers to existing + // masculine and feminine gender types for common, and neuter and non-applicable gender types for neuter. + // Regarding base numbers, only 1 has multiple gender types: 1 and 100 being neuter ('ett'), otherwise common ('en') + public Map baseNumbers() { + return baseNumbersBuilder() + .put(0, "noll") + .put(1, genderForms("en", "en", "ett", "ett")) + .put(2, "två") + .put(3, "tre") + .put(4, "fyra") + .put(5, "fem") + .put(6, "sex") + .put(7, "sju") + .put(8, "åtta") + .put(9, "nio") + .put(10, "tio") + .put(11, "elva") + .put(12, "tolv") + .put(13, "tretton") + .put(14, "fjorton") + .put(15, "femton") + .put(16, "sexton") + .put(17, "sjutton") + .put(18, "arton") + .put(19, "nitton") + .put(20, "tjugo") + .put(30, "trettio") + .put(40, "fyrtio") + .put(50, "femtio") + .put(60, "sextio") + .put(70, "sjuttio") + .put(80, "åttio") + .put(90, "nittio") + .put(100, "ett hundra") + .put(200, "två hundra") + .put(300, "tre hundra") + .put(400, "fyra hundra") + .put(500, "fem hundra") + .put(600, "sex hundra") + .put(700, "sju hundra") + .put(800, "åtta hundra") + .put(900, "nio hundra") + .build(); + } + + public List pluralForms() { + // Note: in Swedish there are common and neuter gender types. In this case, common is equivalent to feminine + return Arrays.asList( + new RegularPluralForms("miljon", "miljoner", GenderType.FEMININE), // million + new RegularPluralForms("miljard", "miljarder", GenderType.FEMININE), // billion + new RegularPluralForms("biljon", "biljoner", GenderType.FEMININE), // trillion + new RegularPluralForms("biljard", "biljarder", GenderType.FEMININE), // quadrillion + new RegularPluralForms("triljon", "triljoner", GenderType.FEMININE)); // quintillion + } + + public String currency() { + return "kr"; + } +} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java b/src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java new file mode 100644 index 00000000..cad83925 --- /dev/null +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java @@ -0,0 +1,26 @@ +package pl.allegro.finance.tradukisto.internal.support; + +import java.util.LinkedList; +import java.util.List; + +public class SwedishNumberChunking extends NumberChunking { + + static final int THOUSANDS_SPLIT_FACTOR = 1_000_000; + static final int SPLIT_FACTOR = 1_000; + + public List chunk(Long value) { + LinkedList result = new LinkedList<>(); + + Integer thousands = (int) (value % THOUSANDS_SPLIT_FACTOR); + + while (value > 1000000000000L) + value /= SPLIT_FACTOR; + + while (value > 0) { + result.addFirst((int) (value % SPLIT_FACTOR)); + value /= 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 3837e8f1..9bdf3bcd 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/LongValueConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/LongValueConvertersTest.groovy @@ -6,6 +6,7 @@ import spock.lang.Unroll 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 class LongValueConvertersTest extends Specification { @@ -19,6 +20,7 @@ class LongValueConvertersTest extends Specification { "English" | ENGLISH_LONG || "one quintillion" "Polish" | POLISH_LONG || "jeden trylion" "Hindi" | HINDI_LONG || "दस शंख" + "Swedish" | SWEDISH_LONG || "en triljon" } 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 656bc083..6e9bf608 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/MoneyConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/MoneyConvertersTest.groovy @@ -19,6 +19,7 @@ import static pl.allegro.finance.tradukisto.MoneyConverters.RUSSIAN_BANKING_MONE import static pl.allegro.finance.tradukisto.MoneyConverters.SERBIAN_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.SERBIAN_CYRILLIC_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.SLOVAK_BANKING_MONEY_VALUE +import static pl.allegro.finance.tradukisto.MoneyConverters.SWEDISH_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.TURKISH_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.UKRAINIAN_BANKING_MONEY_VALUE import static pl.allegro.finance.tradukisto.MoneyConverters.DUTCH_BANKING_MONEY_VALUE @@ -48,6 +49,7 @@ class MoneyConvertersTest extends Specification { "Serbian Latin" | SERBIAN_BANKING_MONEY_VALUE || "jedna hiljada dvesta trideset četiri RSD 56/100" "Slovak" | SLOVAK_BANKING_MONEY_VALUE || "jeden tisíc dvesto tridsať štyri € 56/100" "Russian" | RUSSIAN_BANKING_MONEY_VALUE || "одна тысяча двести тридцать четыре руб. 56/100" + "Swedish" | SWEDISH_BANKING_MONEY_VALUE || "ett tusen två hundra och trettiofyra kr 56/100" "Turkish" | TURKISH_BANKING_MONEY_VALUE || "BinİkiYüzOtuzDörtTL,ElliAltıKr." "Ukrainian" | UKRAINIAN_BANKING_MONEY_VALUE || "одна тисяча двісті тридцять чотири ₴ 56/100" "Hindi" | HINDI_BANKING_MONEY_VALUE || "एक हजार दो सौ चौंतीस ₹,छप्पन p" diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy index aaa7193a..613d9e39 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/ValueConvertersTest.groovy @@ -18,6 +18,7 @@ import static pl.allegro.finance.tradukisto.ValueConverters.RUSSIAN_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.SERBIAN_CYRILLIC_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.SERBIAN_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.SLOVAK_INTEGER +import static pl.allegro.finance.tradukisto.ValueConverters.SWEDISH_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.TURKISH_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.UKRAINIAN_INTEGER import static pl.allegro.finance.tradukisto.ValueConverters.DUTCH_INTEGER @@ -48,6 +49,7 @@ class ValueConvertersTest extends Specification { "Serbian Latin" | SERBIAN_INTEGER || "jedna hiljada dvesta trideset četiri" "Slovak" | SLOVAK_INTEGER || "jeden tisíc dvesto tridsať štyri" "Russian" | RUSSIAN_INTEGER || "одна тысяча двести тридцать четыре" + "Swedish" | SWEDISH_INTEGER || "ett tusen två hundra och trettiofyra" "Turkish" | TURKISH_INTEGER || "Bin İki Yüz Otuz Dört" "Ukrainian" | UKRAINIAN_INTEGER || "одна тисяча двісті тридцять чотири" "Hindi" | HINDI_INTEGER || "एक हजार दो सौ चौंतीस" @@ -94,6 +96,7 @@ class ValueConvertersTest extends Specification { new Locale("uk") || UKRAINIAN_INTEGER new Locale("nl") || DUTCH_INTEGER new Locale("hi") || HINDI_INTEGER + new Locale("sv") || SWEDISH_INTEGER } def "should return supplied default converter when locale is unknown"() { @@ -140,6 +143,7 @@ class ValueConvertersTest extends Specification { "tr" || TURKISH_INTEGER "uk" || UKRAINIAN_INTEGER "hi" || HINDI_INTEGER + "sv" || SWEDISH_INTEGER } def "should return supplied default converter when languageCode is unknown"() { diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy new file mode 100644 index 00000000..e4f1f29c --- /dev/null +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy @@ -0,0 +1,152 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish + +import spock.lang.Specification; +import spock.lang.Unroll; + +import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer; + +class SwedishIntegerValuesTest extends Specification { + + static intConverter = swedishContainer().getIntegerConverter(); + + @Unroll + def "should convert #value to '#words' in Swedish"() { + expect: + intConverter.asWords(value) == words; + + where: + value | words + 0 | "noll" + 1 | "ett" + 2 | "två" + 3 | "tre" + 4 | "fyra" + 5 | "fem" + 6 | "sex" + 7 | "sju" + 8 | "åtta" + 9 | "nio" + + 11 | "elva" + 12 | "tolv" + 13 | "tretton" + 14 | "fjorton" + 15 | "femton" + 16 | "sexton" + 17 | "sjutton" + 18 | "arton" + 19 | "nitton" + + 10 | "tio" + 20 | "tjugo" + 30 | "trettio" + 40 | "fyrtio" + 50 | "femtio" + 60 | "sextio" + 70 | "sjuttio" + 80 | "åttio" + 90 | "nittio" + + 21 | "tjugoett" + 32 | "trettiotvå" + 43 | "fyrtiotre" + 54 | "femtiofyra" + 65 | "sextiofem" + 76 | "sjuttiosex" + 87 | "åttiosju" + 98 | "nittioåtta" + + 100 | "ett hundra" + 200 | "två hundra" + 300 | "tre hundra" + 400 | "fyra hundra" + 500 | "fem hundra" + 600 | "sex hundra" + 700 | "sju hundra" + 800 | "åtta hundra" + 900 | "nio hundra" + + 109 | "ett hundra och nio" + 218 | "två hundra och arton" + 327 | "tre hundra och tjugosju" + 436 | "fyra hundra och trettiosex" + 545 | "fem hundra och fyrtiofem" + 654 | "sex hundra och femtiofyra" + 763 | "sju hundra och sextiotre" + 872 | "åtta hundra och sjuttiotvå" + 981 | "nio hundra och åttioett" + + 1000 | "ett tusen" + 2000 | "två tusen" + 3000 | "tre tusen" + 4000 | "fyra tusen" + 5000 | "fem tusen" + 6000 | "sex tusen" + 7000 | "sju tusen" + 8000 | "åtta tusen" + 9000 | "nio tusen" + + 1911 | "ett tusen nio hundra och elva" + 2890 | "två tusen åtta hundra och nittio" + 3789 | "tre tusen sju hundra och åttionio" + 4678 | "fyra tusen sex hundra och sjuttioåtta" + 5567 | "fem tusen fem hundra och sextiosju" + 6456 | "sex tusen fyra hundra och femtiosex" + 7345 | "sju tusen tre hundra och fyrtiofem" + 8234 | "åtta tusen två hundra och trettiofyra" + 9123 | "nio tusen ett hundra och tjugotre" + + 10000 | "tio tusen" + 11000 | "elva tusen" + 12000 | "tolv tusen" + 13000 | "tretton tusen" + 14000 | "fjorton tusen" + 15000 | "femton tusen" + 16000 | "sexton tusen" + 17000 | "sjutton tusen" + 18000 | "arton tusen" + 19000 | "nitton tusen" + + 20000 | "tjugo tusen" + 30000 | "trettio tusen" + 40000 | "fyrtio tusen" + 50000 | "femtio tusen" + 60000 | "sextio tusen" + 70000 | "sjuttio tusen" + 80000 | "åttio tusen" + 90000 | "nittio tusen" + + 100000 | "ett hundra tusen" + 200000 | "två hundra tusen" + 300000 | "tre hundra tusen" + 400000 | "fyra hundra tusen" + 500000 | "fem hundra tusen" + 600000 | "sex hundra tusen" + 700000 | "sju hundra tusen" + 800000 | "åtta hundra tusen" + + 1000000 | "en miljon" + 2000000 | "två miljoner" + 3000000 | "tre miljoner" + 4000000 | "fyra miljoner" + 5000000 | "fem miljoner" + 6000000 | "sex miljoner" + 7000000 | "sju miljoner" + 8000000 | "åtta miljoner" + 9000000 | "nio miljoner" + + 1111111 | "en miljon ett hundra och elva tusen ett hundra och elva" + 2222222 | "två miljoner två hundra och tjugotvå tusen två hundra och tjugotvå" + 3333333 | "tre miljoner tre hundra och trettiotre tusen tre hundra och trettiotre" + 4444444 | "fyra miljoner fyra hundra och fyrtiofyra tusen fyra hundra och fyrtiofyra" + 5555555 | "fem miljoner fem hundra och femtiofem tusen fem hundra och femtiofem" + 6666666 | "sex miljoner sex hundra och sextiosex tusen sex hundra och sextiosex" + 7777777 | "sju miljoner sju hundra och sjuttiosju tusen sju hundra och sjuttiosju" + 8888888 | "åtta miljoner åtta hundra och åttioåtta tusen åtta hundra och åttioåtta" + 9999999 | "nio miljoner nio hundra och nittionio tusen nio hundra och nittionio" + + 1000000000 | "en miljard" + 2000000000 | "två miljarder" + 2147000000 | "två miljarder ett hundra och fyrtiosju miljoner" // maximum Integer value + } +} diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy new file mode 100644 index 00000000..a77c62d5 --- /dev/null +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy @@ -0,0 +1,88 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish; + +import pl.allegro.finance.tradukisto.internal.languages.GenderType; +import spock.lang.Specification; + +import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForm; +import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms; + +class SwedishThousandToWordsConverterTest extends Specification { + + def "should convert a single value from the list of base values"() { + given: + def converter = new SwedishThousandToWordsConverter([1: genderForms("en", + "en", + "ett", + "ett")]) + + expect: + converter.asWords(1, GenderType.NON_APPLICABLE) == "ett" + } + + def "should convert a two digit number from the list of base values"() { + given: + def converter = new SwedishThousandToWordsConverter([20: genderForm("tjugo"), + 1: genderForms("en", + "en", + "ett", + "ett")]) + + expect: + converter.asWords(21, GenderType.NON_APPLICABLE) == "tjugoett" + } + + def "should convert a three digit number from the list of base values"() { + given: + def converter = new SwedishThousandToWordsConverter([900: genderForm("nio hundra"), + 90: genderForm("nittio"), + 9: genderForm("nio")]) + + expect: + converter.asWords(999, GenderType.NON_APPLICABLE) == "nio hundra och nittionio" + } + + def "should convert a four digit number from the list of base values"() { + given: + def converter = new SwedishThousandToWordsConverter([600: genderForm("sex hundra"), + 3: genderForm("tre"), + 2: genderForm("två")]) + + expect: + converter.asWords(3602, GenderType.NON_APPLICABLE) == "tre tusen sex hundra och två" + } + + def "should convert a five digit number from the list of base values"() { + given: + def converter = new SwedishThousandToWordsConverter([400: genderForm("fyra hundra"), + 88: genderForm("åttioåtta"), + 80: genderForm("åttio"), + 7: genderForm("sju")]) + + expect: + converter.asWords(88487, GenderType.NON_APPLICABLE) == "åttioåtta tusen fyra hundra och åttiosju" + } + + def "should convert a six digit number from the list of base values"() { + given: + def converter = new SwedishThousandToWordsConverter([900: genderForm("nio hundra"), + 200: genderForm("två hundra"), + 13: genderForm("tretton"), + 5: genderForm("fem")]) + + expect: + converter.asWords(905213, GenderType.NON_APPLICABLE) == "nio hundra och fem tusen två hundra och tretton" + } + + + def "should throw IllegalArgumentException when given number is not supported"() { + given: + def converter = new SwedishThousandToWordsConverter([1: genderForm("ett")]) + + when: + converter.asWords(2, GenderType.NON_APPLICABLE) + + then: + def exception = thrown(IllegalArgumentException) + exception.message == "Can't convert 2" + } +} From 3380c1c197439f830a22145cba46b67eb8ca0656 Mon Sep 17 00:00:00 2001 From: Adam Ross <14985050+R055A@users.noreply.github.com> Date: Mon, 2 Oct 2023 05:17:48 +1300 Subject: [PATCH 2/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e5d0328b..75d800d4 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ With Tradukisto, you can now transform numbers into their word equivalents easy * 🇵🇱 Polish * 🇷🇺 Russian * 🇸🇰 Slovak +* 🇸🇪 Swedish * 🇺🇦 Ukrainian * 🇷🇸 Serbian (Latin) * 🇷🇸 Serbian (Cyrillic) From 8bc73891496cb165764f91885b519bc6ca4b1213 Mon Sep 17 00:00:00 2001 From: Adam Ross <14985050+R055A@users.noreply.github.com> Date: Mon, 2 Oct 2023 17:00:39 +1300 Subject: [PATCH 3/4] Fix long number conversion to Swedish words --- .../tradukisto/internal/Container.java | 27 ++++---- ...va => SwedishHundredToWordsConverter.java} | 15 +---- .../SwedishIntegerToWordsConverter.java | 24 ------- .../swedish/SwedishLongToWordsConverter.java | 30 --------- .../languages/swedish/SwedishValues.java | 2 + .../support/SwedishNumberChunking.java | 26 -------- .../swedish/SwedishIntegerValuesTest.groovy | 14 ++-- .../swedish/SwedishLongValuesTest.groovy | 65 +++++++++++++++++++ ...SwedishThousandToWordsConverterTest.groovy | 54 ++++----------- 9 files changed, 98 insertions(+), 159 deletions(-) rename src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/{SwedishThousandToWordsConverter.java => SwedishHundredToWordsConverter.java} (71%) delete mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java delete mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java delete mode 100644 src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java create mode 100644 src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongValuesTest.groovy 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 c9a214bd..9692c650 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/Container.java @@ -37,9 +37,7 @@ import pl.allegro.finance.tradukisto.internal.languages.serbian.SerbianValues; import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValues; import pl.allegro.finance.tradukisto.internal.languages.slovak.SlovakValuesForSmallNumbers; -import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishIntegerToWordsConverter; -import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishLongToWordsConverter; -import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishThousandToWordsConverter; +import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishHundredToWordsConverter; import pl.allegro.finance.tradukisto.internal.languages.swedish.SwedishValues; import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishBigDecimalToBankingMoneyConverter; import pl.allegro.finance.tradukisto.internal.languages.turkish.TurkishIntegerToWordsConverter; @@ -250,23 +248,20 @@ public static Container hindiContainer() { } public static Container swedishContainer() { - SwedishValues swedishValues = new SwedishValues(); - SwedishThousandToWordsConverter swedishThousandToWordsConverter = new SwedishThousandToWordsConverter( - swedishValues.baseNumbers()); + SwedishValues swedishBaseValues = new SwedishValues(); - IntegerToStringConverter integerToStringConverter = new SwedishIntegerToWordsConverter( - new NumberToWordsConverter(swedishThousandToWordsConverter, swedishValues.pluralForms()), - swedishThousandToWordsConverter); + SwedishHundredToWordsConverter swedishHundredsToStringConverter = + new SwedishHundredToWordsConverter(swedishBaseValues.baseNumbers()); - BigDecimalToStringConverter bigDecimalBankingMoneyValueConverter = new BigDecimalToBankingMoneyConverter( - integerToStringConverter, - swedishValues.currency()); + NumberToWordsConverter swedishNumberToWordsConverter = new NumberToWordsConverter( + swedishHundredsToStringConverter, + swedishBaseValues.pluralForms()); - NumberToWordsConverter longValueConverters = new SwedishLongToWordsConverter( - swedishThousandToWordsConverter, - swedishValues.pluralForms()); + BigDecimalToStringConverter swedishBigDecimalConverter = new BigDecimalToBankingMoneyConverter( + swedishNumberToWordsConverter, + swedishBaseValues.currency()); - return new Container(integerToStringConverter, longValueConverters, bigDecimalBankingMoneyValueConverter); + return new Container(swedishNumberToWordsConverter, swedishNumberToWordsConverter, swedishBigDecimalConverter); } private final IntegerToStringConverter integerConverter; diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverter.java similarity index 71% rename from src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java rename to src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverter.java index 74f6d547..9ad86233 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverter.java +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverter.java @@ -9,11 +9,11 @@ import static java.lang.String.format; -public class SwedishThousandToWordsConverter implements GenderAwareIntegerToStringConverter { +public class SwedishHundredToWordsConverter implements GenderAwareIntegerToStringConverter { private final Map swedishBaseValues; - public SwedishThousandToWordsConverter(Map swedishBaseValues) { + public SwedishHundredToWordsConverter(Map swedishBaseValues) { this.swedishBaseValues = swedishBaseValues; } @@ -25,8 +25,6 @@ public String asWords(Integer value, GenderType swedishGenderType) { return twoDigitsNumberAsString(value, swedishGenderType); } else if (Range.closed(101, 999).contains(value)) { return threeDigitsNumberAsString(value, swedishGenderType); - } else if (Range.closed(1000, 999999).contains(value)) { - return thousandsAsString(value, swedishGenderType); } throw new IllegalArgumentException(format("Can't convert %d", value)); } @@ -42,13 +40,4 @@ private String threeDigitsNumberAsString(Integer value, GenderType genderType) { Integer hundreds = value - tensWithUnits; return format("%s och %s", asWords(hundreds, genderType), asWords(tensWithUnits, genderType)); } - - private String thousandsAsString(Integer value, GenderType genderType) { - Integer thousands = value / 1000; - Integer other = value % 1000; - - if (other == 0) - return format("%s tusen", asWords(thousands, genderType)); - return format("%s tusen %s", asWords(thousands, genderType), asWords(other, genderType)); - } } diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java deleted file mode 100644 index 711aca4a..00000000 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerToWordsConverter.java +++ /dev/null @@ -1,24 +0,0 @@ -package pl.allegro.finance.tradukisto.internal.languages.swedish; - -import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter; -import pl.allegro.finance.tradukisto.internal.IntegerToStringConverter; -import pl.allegro.finance.tradukisto.internal.NumberProcessor; - -public class SwedishIntegerToWordsConverter implements IntegerToStringConverter { - - private final IntegerToStringConverter swedishBigIntegersConverter; - private final GenderAwareIntegerToStringConverter swedishSmallIntegersConverter; - - public SwedishIntegerToWordsConverter(IntegerToStringConverter swedishBigIntegersConverter, - GenderAwareIntegerToStringConverter swedishSmallIntegersConverter) { - this.swedishBigIntegersConverter = swedishBigIntegersConverter; - this.swedishSmallIntegersConverter = swedishSmallIntegersConverter; - } - - @Override - public String asWords(Integer value) { - Integer bigNum = value / 1000000; - Integer smallNum = value % 1000000; - return new NumberProcessor(this.swedishBigIntegersConverter, this.swedishSmallIntegersConverter).process(bigNum, smallNum); - } -} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java deleted file mode 100644 index cef71478..00000000 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongToWordsConverter.java +++ /dev/null @@ -1,30 +0,0 @@ -package pl.allegro.finance.tradukisto.internal.languages.swedish; - -import pl.allegro.finance.tradukisto.internal.GenderAwareIntegerToStringConverter; -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.NumberChunking; -import pl.allegro.finance.tradukisto.internal.support.SwedishNumberChunking; - -import java.util.List; - -public class SwedishLongToWordsConverter extends NumberToWordsConverter { - - private final NumberChunking swedishNumberChunking = new SwedishNumberChunking(); - - public SwedishLongToWordsConverter(GenderAwareIntegerToStringConverter hundredsToWordsConverter, - List pluralForms) { - super(hundredsToWordsConverter, pluralForms); - } - - @Override - public String asWords(Long value) { - Assert.isTrue(value >= 0, () -> String.format("can't convert negative numbers for value %d", value)); - - List valueChunks = swedishNumberChunking.chunk(value); - List formsToUse = getRequiredFormsInReversedOrder(valueChunks.size()); - - return joinValueChunksWithForms(valueChunks.iterator(), formsToUse.iterator()); - } -} diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java index 8755e145..a06986c9 100644 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java +++ b/src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java @@ -62,6 +62,8 @@ public Map baseNumbers() { public List pluralForms() { // Note: in Swedish there are common and neuter gender types. In this case, common is equivalent to feminine return Arrays.asList( + new RegularPluralForms("", "", GenderType.NON_APPLICABLE), + new RegularPluralForms("tusen", "tusen", GenderType.NEUTER), // thousand new RegularPluralForms("miljon", "miljoner", GenderType.FEMININE), // million new RegularPluralForms("miljard", "miljarder", GenderType.FEMININE), // billion new RegularPluralForms("biljon", "biljoner", GenderType.FEMININE), // trillion diff --git a/src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java b/src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java deleted file mode 100644 index cad83925..00000000 --- a/src/main/java/pl/allegro/finance/tradukisto/internal/support/SwedishNumberChunking.java +++ /dev/null @@ -1,26 +0,0 @@ -package pl.allegro.finance.tradukisto.internal.support; - -import java.util.LinkedList; -import java.util.List; - -public class SwedishNumberChunking extends NumberChunking { - - static final int THOUSANDS_SPLIT_FACTOR = 1_000_000; - static final int SPLIT_FACTOR = 1_000; - - public List chunk(Long value) { - LinkedList result = new LinkedList<>(); - - Integer thousands = (int) (value % THOUSANDS_SPLIT_FACTOR); - - while (value > 1000000000000L) - value /= SPLIT_FACTOR; - - while (value > 0) { - result.addFirst((int) (value % SPLIT_FACTOR)); - value /= SPLIT_FACTOR; - } - - return result; - } -} diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy index e4f1f29c..3b78f9f9 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishIntegerValuesTest.groovy @@ -1,18 +1,18 @@ package pl.allegro.finance.tradukisto.internal.languages.swedish -import spock.lang.Specification; -import spock.lang.Unroll; +import spock.lang.Specification +import spock.lang.Unroll -import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer; +import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer class SwedishIntegerValuesTest extends Specification { - static intConverter = swedishContainer().getIntegerConverter(); + static intConverter = swedishContainer().getIntegerConverter() @Unroll - def "should convert #value to '#words' in Swedish"() { + def "should convert Integer #value to '#words' in Swedish"() { expect: - intConverter.asWords(value) == words; + intConverter.asWords(value) == words where: value | words @@ -147,6 +147,6 @@ class SwedishIntegerValuesTest extends Specification { 1000000000 | "en miljard" 2000000000 | "två miljarder" - 2147000000 | "två miljarder ett hundra och fyrtiosju miljoner" // maximum Integer value + 2147483647 | "två miljarder ett hundra och fyrtiosju miljoner fyra hundra och åttiotre tusen sex hundra och fyrtiosju" } } diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongValuesTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongValuesTest.groovy new file mode 100644 index 00000000..1445d39c --- /dev/null +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishLongValuesTest.groovy @@ -0,0 +1,65 @@ +package pl.allegro.finance.tradukisto.internal.languages.swedish + +import spock.lang.Specification +import spock.lang.Unroll + +import static pl.allegro.finance.tradukisto.internal.Container.swedishContainer + +class SwedishLongValuesTest extends Specification { + + static longConverter = swedishContainer().getLongConverter() + + @Unroll + def "should convert Long #value to '#words' in Swedish"() { + expect: + longConverter.asWords(value) == words + + where: + value | words + 2147483648 | "två miljarder ett hundra och fyrtiosju miljoner fyra hundra och åttiotre tusen sex hundra och fyrtioåtta" + 3000000000 | "tre miljarder" + 4000000000 | "fyra miljarder" + 5000000000 | "fem miljarder" + 6000000000 | "sex miljarder" + 7000000000 | "sju miljarder" + 8000000000 | "åtta miljarder" + 9000000000 | "nio miljarder" + 9999999999 | "nio miljarder nio hundra och nittionio miljoner nio hundra och nittionio tusen nio hundra och nittionio" + + 1000000000000 | "en biljon" + 2000000000000 | "två biljoner" + 2222222222222 | "två biljoner två hundra och tjugotvå miljarder två hundra och tjugotvå miljoner två hundra och tjugotvå tusen två hundra och tjugotvå" + 3000000000000 | "tre biljoner" + 4000000000000 | "fyra biljoner" + 5000000000000 | "fem biljoner" + 6000000000000 | "sex biljoner" + 7000000000000 | "sju biljoner" + 8000000000000 | "åtta biljoner" + 9000000000000 | "nio biljoner" + 9999999999999 | "nio biljoner nio hundra och nittionio miljarder nio hundra och nittionio miljoner nio hundra och nittionio tusen nio hundra och nittionio" + + 1000000000000000 | "en biljard" + 2000000000000000 | "två biljarder" + 3000000000000000 | "tre biljarder" + 3333333333333333 | "tre biljarder tre hundra och trettiotre biljoner tre hundra och trettiotre miljarder tre hundra och trettiotre miljoner tre hundra och trettiotre tusen tre hundra och trettiotre" + 4000000000000000 | "fyra biljarder" + 5000000000000000 | "fem biljarder" + 6000000000000000 | "sex biljarder" + 7000000000000000 | "sju biljarder" + 8000000000000000 | "åtta biljarder" + 9000000000000000 | "nio biljarder" + 9999999999999999 | "nio biljarder nio hundra och nittionio biljoner nio hundra och nittionio miljarder nio hundra och nittionio miljoner nio hundra och nittionio tusen nio hundra och nittionio" + + 1000000000000000000 | "en triljon" + 2000000000000000000 | "två triljoner" + 3000000000000000000 | "tre triljoner" + 4000000000000000000 | "fyra triljoner" + 4444444444444444444 | "fyra triljoner fyra hundra och fyrtiofyra biljarder fyra hundra och fyrtiofyra biljoner fyra hundra och fyrtiofyra miljarder fyra hundra och fyrtiofyra miljoner fyra hundra och fyrtiofyra tusen fyra hundra och fyrtiofyra" + 5000000000000000000 | "fem triljoner" + 6000000000000000000 | "sex triljoner" + 7000000000000000000 | "sju triljoner" + 8000000000000000000 | "åtta triljoner" + 9000000000000000000 | "nio triljoner" + 9223272036854775807 | "nio triljoner två hundra och tjugotre biljarder två hundra och sjuttiotvå biljoner trettiosex miljarder åtta hundra och femtiofyra miljoner sju hundra och sjuttiofem tusen åtta hundra och sju" + } +} diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy index a77c62d5..6a43d124 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy @@ -1,16 +1,16 @@ package pl.allegro.finance.tradukisto.internal.languages.swedish; -import pl.allegro.finance.tradukisto.internal.languages.GenderType; -import spock.lang.Specification; +import pl.allegro.finance.tradukisto.internal.languages.GenderType +import spock.lang.Specification -import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForm; -import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms; +import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForm +import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms class SwedishThousandToWordsConverterTest extends Specification { def "should convert a single value from the list of base values"() { given: - def converter = new SwedishThousandToWordsConverter([1: genderForms("en", + def converter = new SwedishHundredToWordsConverter([1: genderForms("en", "en", "ett", "ett")]) @@ -21,8 +21,8 @@ class SwedishThousandToWordsConverterTest extends Specification { def "should convert a two digit number from the list of base values"() { given: - def converter = new SwedishThousandToWordsConverter([20: genderForm("tjugo"), - 1: genderForms("en", + def converter = new SwedishHundredToWordsConverter([20: genderForm("tjugo"), + 1 : genderForms("en", "en", "ett", "ett")]) @@ -33,50 +33,18 @@ class SwedishThousandToWordsConverterTest extends Specification { def "should convert a three digit number from the list of base values"() { given: - def converter = new SwedishThousandToWordsConverter([900: genderForm("nio hundra"), - 90: genderForm("nittio"), - 9: genderForm("nio")]) + def converter = new SwedishHundredToWordsConverter([900: genderForm("nio hundra"), + 90 : genderForm("nittio"), + 9 : genderForm("nio")]) expect: converter.asWords(999, GenderType.NON_APPLICABLE) == "nio hundra och nittionio" } - def "should convert a four digit number from the list of base values"() { - given: - def converter = new SwedishThousandToWordsConverter([600: genderForm("sex hundra"), - 3: genderForm("tre"), - 2: genderForm("två")]) - - expect: - converter.asWords(3602, GenderType.NON_APPLICABLE) == "tre tusen sex hundra och två" - } - - def "should convert a five digit number from the list of base values"() { - given: - def converter = new SwedishThousandToWordsConverter([400: genderForm("fyra hundra"), - 88: genderForm("åttioåtta"), - 80: genderForm("åttio"), - 7: genderForm("sju")]) - - expect: - converter.asWords(88487, GenderType.NON_APPLICABLE) == "åttioåtta tusen fyra hundra och åttiosju" - } - - def "should convert a six digit number from the list of base values"() { - given: - def converter = new SwedishThousandToWordsConverter([900: genderForm("nio hundra"), - 200: genderForm("två hundra"), - 13: genderForm("tretton"), - 5: genderForm("fem")]) - - expect: - converter.asWords(905213, GenderType.NON_APPLICABLE) == "nio hundra och fem tusen två hundra och tretton" - } - def "should throw IllegalArgumentException when given number is not supported"() { given: - def converter = new SwedishThousandToWordsConverter([1: genderForm("ett")]) + def converter = new SwedishHundredToWordsConverter([1: genderForm("ett")]) when: converter.asWords(2, GenderType.NON_APPLICABLE) From 09c01344f13800b32046fae8324f08b2ca90b2bd Mon Sep 17 00:00:00 2001 From: Adam Ross <14985050+R055A@users.noreply.github.com> Date: Mon, 2 Oct 2023 17:33:45 +1300 Subject: [PATCH 4/4] Refactor test name to match Swedish conversion fix --- ...terTest.groovy => SwedishHundredToWordsConverterTest.groovy} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/{SwedishThousandToWordsConverterTest.groovy => SwedishHundredToWordsConverterTest.groovy} (96%) diff --git a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverterTest.groovy similarity index 96% rename from src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy rename to src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverterTest.groovy index 6a43d124..e8e1c186 100644 --- a/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishThousandToWordsConverterTest.groovy +++ b/src/test/groovy/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverterTest.groovy @@ -6,7 +6,7 @@ import spock.lang.Specification import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForm import static pl.allegro.finance.tradukisto.internal.languages.GenderForms.genderForms -class SwedishThousandToWordsConverterTest extends Specification { +class SwedishHundredToWordsConverterTest extends Specification { def "should convert a single value from the list of base values"() { given: