This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added integer to text capabilities and an implementation for Swedish
- Loading branch information
Joel Håkansson
committed
May 29, 2013
1 parent
8bd4bee
commit 71c314e
Showing
10 changed files
with
372 additions
and
0 deletions.
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
1 change: 1 addition & 0 deletions
1
DotifyTranslator/src/META-INF/services/org.daisy.dotify.text.Integer2TextFactory
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 @@ | ||
org.daisy.dotify.impl.text.SwedishInteger2TextFactory |
89 changes: 89 additions & 0 deletions
89
DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2Text.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,89 @@ | ||
package org.daisy.dotify.impl.text; | ||
|
||
import org.daisy.dotify.text.Integer2Text; | ||
import org.daisy.dotify.text.IntegerOutOfRange; | ||
|
||
class SwedishInteger2Text implements Integer2Text { | ||
|
||
public String intToText(int value) throws IntegerOutOfRange { | ||
if (value < 0) return "minus " + intToText(-value); | ||
switch (value) { | ||
case 0: | ||
return "noll"; | ||
case 1: | ||
return "ett"; | ||
case 2: | ||
return "två"; | ||
case 3: | ||
return "tre"; | ||
case 4: | ||
return "fyra"; | ||
case 5: | ||
return "fem"; | ||
case 6: | ||
return "sex"; | ||
case 7: | ||
return "sju"; | ||
case 8: | ||
return "åtta"; | ||
case 9: | ||
return "nio"; | ||
case 10: | ||
return "tio"; | ||
case 11: | ||
return "elva"; | ||
case 12: | ||
return "tolv"; | ||
case 13: | ||
return "tretton"; | ||
case 14: | ||
return "fjorton"; | ||
case 15: | ||
return "femton"; | ||
case 16: | ||
return "sexton"; | ||
case 17: | ||
return "sjutton"; | ||
case 18: | ||
return "arton"; | ||
case 19: | ||
return "nitton"; | ||
case 20: | ||
return "tjugo"; | ||
case 30: | ||
return "trettio"; | ||
case 40: | ||
return "fyrtio"; | ||
case 50: | ||
return "femtio"; | ||
case 60: | ||
return "sextio"; | ||
case 70: | ||
return "sjuttio"; | ||
case 80: | ||
return "åttio"; | ||
case 90: | ||
return "nittio"; | ||
} | ||
String pre = ""; | ||
if (value >= 1000) { | ||
pre = intToText(value / 1000) + "tusen"; | ||
value = value % 1000; | ||
} | ||
if (value >= 100) { | ||
pre = pre + (value >= 200 ? intToText(value / 100) : "") + "hundra"; | ||
value = value % 100; | ||
} | ||
// replace three occurrences of the same character by two | ||
pre = pre.replaceAll("(\\w)(\\1{2})", "$2"); | ||
if (value == 0) return pre; | ||
if (value < 20) { | ||
return pre + intToText(value); | ||
} else { | ||
int t = value % 10; | ||
int r = (value / 10) * 10; | ||
return pre + intToText(r) + (t > 0 ? intToText(t) : ""); | ||
} | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2TextFactory.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,28 @@ | ||
package org.daisy.dotify.impl.text; | ||
|
||
import org.daisy.dotify.hyphenator.UnsupportedFeatureException; | ||
import org.daisy.dotify.hyphenator.UnsupportedLocaleException; | ||
import org.daisy.dotify.text.FilterLocale; | ||
import org.daisy.dotify.text.Integer2Text; | ||
import org.daisy.dotify.text.Integer2TextFactory; | ||
|
||
public class SwedishInteger2TextFactory implements Integer2TextFactory { | ||
private final static FilterLocale sv_SE = FilterLocale.parse("sv-SE"); | ||
|
||
public boolean supportsLocale(FilterLocale locale) { | ||
return locale.equals(sv_SE); | ||
} | ||
|
||
public Integer2Text newInteger2Text(FilterLocale locale) throws UnsupportedLocaleException { | ||
return new SwedishInteger2Text(); | ||
} | ||
|
||
public Object getFeature(String key) { | ||
return null; | ||
} | ||
|
||
public void setFeature(String key, Object value) throws UnsupportedFeatureException { | ||
throw new UnsupportedFeatureException(); | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.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,22 @@ | ||
package org.daisy.dotify.text; | ||
|
||
|
||
/** | ||
* Provides an integer2text. | ||
* | ||
* @author Joel Håkansson | ||
*/ | ||
public interface Integer2Text { | ||
|
||
/** | ||
* Converts the integer to text. | ||
* | ||
* @param value | ||
* the integer value | ||
* @throws IntegerOutOfRange | ||
* If value is out of range of the implementations | ||
* capabilities. | ||
*/ | ||
public String intToText(int value) throws IntegerOutOfRange; | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactory.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,53 @@ | ||
package org.daisy.dotify.text; | ||
|
||
import org.daisy.dotify.hyphenator.UnsupportedFeatureException; | ||
import org.daisy.dotify.hyphenator.UnsupportedLocaleException; | ||
|
||
/** | ||
* Provides a integer2text factory interface. This interface is used to retreive | ||
* a integer2text instance. | ||
* | ||
* @author Joel Håkansson | ||
* | ||
*/ | ||
public interface Integer2TextFactory { | ||
|
||
/** | ||
* Returns true if this instance can create instances for the specified locale. | ||
* @param locale | ||
* @return returns true if the specified locale is supported, false otherwise | ||
*/ | ||
public boolean supportsLocale(FilterLocale locale); | ||
|
||
/** | ||
* Returns a new integer2text configured for the specified locale. | ||
* | ||
* @param locale | ||
* the locale for the new integer2text | ||
* @return returns a new integer2text | ||
* @throws UnsupportedLocaleException | ||
* if the locale is not supported | ||
*/ | ||
public Integer2Text newInteger2Text(FilterLocale locale) throws UnsupportedLocaleException; | ||
|
||
/** | ||
* Gets the value of a integer2text feature. | ||
* | ||
* @param key | ||
* the feature to get the value for | ||
* @return returns the value, or null if not set | ||
*/ | ||
public Object getFeature(String key); | ||
|
||
/** | ||
* Sets the value of a integer2text feature. | ||
* | ||
* @param key | ||
* the feature to set the value for | ||
* @param value | ||
* the value for the feature | ||
* @throws UnsupportedFeatureException | ||
* if the feature is not supported | ||
*/ | ||
public void setFeature(String key, Object value) throws UnsupportedFeatureException; | ||
} |
91 changes: 91 additions & 0 deletions
91
DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactoryMaker.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,91 @@ | ||
package org.daisy.dotify.text; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.logging.Logger; | ||
|
||
import javax.imageio.spi.ServiceRegistry; | ||
|
||
import org.daisy.dotify.hyphenator.UnsupportedLocaleException; | ||
|
||
/** | ||
* Provides a integer2text factory maker. This is the entry point for | ||
* creating integer2text instances. | ||
* | ||
* @author Joel Håkansson | ||
*/ | ||
public class Integer2TextFactoryMaker { | ||
private final List<Integer2TextFactory> filters; | ||
private final Map<FilterLocale, Integer2TextFactory> map; | ||
private final Logger logger; | ||
|
||
protected Integer2TextFactoryMaker() { | ||
logger = Logger.getLogger(Integer2TextFactoryMaker.class.getCanonicalName()); | ||
filters = new ArrayList<Integer2TextFactory>(); | ||
Iterator<Integer2TextFactory> i = ServiceRegistry.lookupProviders(Integer2TextFactory.class); | ||
while (i.hasNext()) { | ||
filters.add(i.next()); | ||
} | ||
this.map = new HashMap<FilterLocale, Integer2TextFactory>(); | ||
} | ||
|
||
/** | ||
* Creates a new integer2text factory maker. | ||
* | ||
* @return returns a new integer2text factory maker | ||
*/ | ||
public static Integer2TextFactoryMaker newInstance() { | ||
Iterator<Integer2TextFactoryMaker> i = ServiceRegistry.lookupProviders(Integer2TextFactoryMaker.class); | ||
while (i.hasNext()) { | ||
return i.next(); | ||
} | ||
return new Integer2TextFactoryMaker(); | ||
} | ||
|
||
/** | ||
* Gets a Integer2TextFactory that supports the specified locale | ||
* | ||
* @param target | ||
* the target locale | ||
* @return returns a integer2text factory for the specified locale | ||
* @throws UnsupportedLocaleException | ||
* if the locale is not supported | ||
*/ | ||
public Integer2TextFactory getFactory(FilterLocale target) throws UnsupportedLocaleException { | ||
Integer2TextFactory template = map.get(target); | ||
if (template==null) { | ||
for (Integer2TextFactory h : filters) { | ||
if (h.supportsLocale(target)) { | ||
logger.fine("Found an integer2text factory for " + target + " (" + h.getClass() + ")"); | ||
map.put(target, h); | ||
template = h; | ||
break; | ||
} | ||
} | ||
} | ||
if (template==null) { | ||
throw new UnsupportedLocaleException("Cannot find integer2text factory for " + target); | ||
} | ||
return template; | ||
} | ||
|
||
/** | ||
* Creates a new integer2text. This is a convenience method for | ||
* getFactory(target).newInteger2Text(target). | ||
* Using this method excludes the possibility of setting features of the | ||
* integer2text factory. | ||
* | ||
* @param target | ||
* the target locale | ||
* @return returns a new integer2text | ||
* @throws UnsupportedLocaleException | ||
* if the locale is not supported | ||
*/ | ||
public Integer2Text newInteger2Text(FilterLocale target) throws UnsupportedLocaleException { | ||
return getFactory(target).newInteger2Text(target); | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.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,25 @@ | ||
package org.daisy.dotify.text; | ||
|
||
public class IntegerOutOfRange extends Exception { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = -3260267525562596727L; | ||
|
||
public IntegerOutOfRange() { | ||
} | ||
|
||
public IntegerOutOfRange(String message) { | ||
super(message); | ||
} | ||
|
||
public IntegerOutOfRange(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public IntegerOutOfRange(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
DotifyTranslator/test/org/daisy/dotify/impl/text/SwedishInteger2TextTest.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,40 @@ | ||
package org.daisy.dotify.impl.text; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.daisy.dotify.text.IntegerOutOfRange; | ||
import org.junit.Test; | ||
|
||
public class SwedishInteger2TextTest { | ||
|
||
@Test | ||
public void testNumber01() throws IntegerOutOfRange { | ||
SwedishInteger2Text t = new SwedishInteger2Text(); | ||
assertEquals("nittionio", t.intToText(99)); | ||
} | ||
|
||
@Test | ||
public void testNumber02() throws IntegerOutOfRange { | ||
SwedishInteger2Text t = new SwedishInteger2Text(); | ||
assertEquals("hundratrettiotvå", t.intToText(132)); | ||
} | ||
|
||
@Test | ||
public void testNumber03() throws IntegerOutOfRange { | ||
SwedishInteger2Text t = new SwedishInteger2Text(); | ||
assertEquals("ettusensjuhundrafemtioåtta", t.intToText(1758)); | ||
} | ||
|
||
@Test | ||
public void testNumber04() throws IntegerOutOfRange { | ||
SwedishInteger2Text t = new SwedishInteger2Text(); | ||
assertEquals("minus tolv", t.intToText(-12)); | ||
} | ||
|
||
@Test | ||
public void testNumber05() throws IntegerOutOfRange { | ||
SwedishInteger2Text t = new SwedishInteger2Text(); | ||
assertEquals("femton", t.intToText(15)); | ||
} | ||
|
||
} |
Oops, something went wrong.