-
Notifications
You must be signed in to change notification settings - Fork 92
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Swedish support for number to word conversion #139
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...allegro/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 SwedishHundredToWordsConverter implements GenderAwareIntegerToStringConverter { | ||
|
||
private final Map<Integer, GenderForms> swedishBaseValues; | ||
|
||
public SwedishHundredToWordsConverter(Map<Integer, GenderForms> 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); | ||
} | ||
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)); | ||
} | ||
} |
77 changes: 77 additions & 0 deletions
77
src/main/java/pl/allegro/finance/tradukisto/internal/languages/swedish/SwedishValues.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
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<Integer, GenderForms> 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> 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 | ||
new RegularPluralForms("biljard", "biljarder", GenderType.FEMININE), // quadrillion | ||
new RegularPluralForms("triljon", "triljoner", GenderType.FEMININE)); // quintillion | ||
} | ||
|
||
public String currency() { | ||
return "kr"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...o/finance/tradukisto/internal/languages/swedish/SwedishHundredToWordsConverterTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
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 SwedishHundredToWordsConverterTest extends Specification { | ||
|
||
def "should convert a single value from the list of base values"() { | ||
given: | ||
def converter = new SwedishHundredToWordsConverter([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 SwedishHundredToWordsConverter([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 SwedishHundredToWordsConverter([900: genderForm("nio hundra"), | ||
90 : genderForm("nittio"), | ||
9 : genderForm("nio")]) | ||
|
||
expect: | ||
converter.asWords(999, GenderType.NON_APPLICABLE) == "nio hundra och nittionio" | ||
} | ||
|
||
|
||
def "should throw IllegalArgumentException when given number is not supported"() { | ||
given: | ||
def converter = new SwedishHundredToWordsConverter([1: genderForm("ett")]) | ||
|
||
when: | ||
converter.asWords(2, GenderType.NON_APPLICABLE) | ||
|
||
then: | ||
def exception = thrown(IllegalArgumentException) | ||
exception.message == "Can't convert 2" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for explaination, it is really helpful