From 71c314e4a6d08b594b63ef4297088da879ed5898 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joel=20H=C3=A5kansson?= Date: Wed, 29 May 2013 06:53:04 +0000 Subject: [PATCH] Added integer to text capabilities and an implementation for Swedish --- .gitattributes | 9 ++ .../org.daisy.dotify.text.Integer2TextFactory | 1 + .../dotify/impl/text/SwedishInteger2Text.java | 89 ++++++++++++++++++ .../impl/text/SwedishInteger2TextFactory.java | 28 ++++++ .../org/daisy/dotify/text/Integer2Text.java | 22 +++++ .../dotify/text/Integer2TextFactory.java | 53 +++++++++++ .../dotify/text/Integer2TextFactoryMaker.java | 91 +++++++++++++++++++ .../daisy/dotify/text/IntegerOutOfRange.java | 25 +++++ .../impl/text/SwedishInteger2TextTest.java | 40 ++++++++ .../dotify/text/Integer2TextFactoryTest.java | 14 +++ 10 files changed, 372 insertions(+) create mode 100644 DotifyTranslator/src/META-INF/services/org.daisy.dotify.text.Integer2TextFactory create mode 100644 DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2Text.java create mode 100644 DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2TextFactory.java create mode 100644 DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.java create mode 100644 DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactory.java create mode 100644 DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactoryMaker.java create mode 100644 DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.java create mode 100644 DotifyTranslator/test/org/daisy/dotify/impl/text/SwedishInteger2TextTest.java create mode 100644 DotifyTranslator/test/org/daisy/dotify/text/Integer2TextFactoryTest.java diff --git a/.gitattributes b/.gitattributes index 28853a70..f56e5fad 100644 --- a/.gitattributes +++ b/.gitattributes @@ -346,6 +346,7 @@ DotifyTranslator/lib/junit-4.7.jar -text DotifyTranslator/lib/texhyphj.jar -text DotifyTranslator/logging.properties -text DotifyTranslator/src/META-INF/services/org.daisy.dotify.hyphenator.HyphenatorFactory -text +DotifyTranslator/src/META-INF/services/org.daisy.dotify.text.Integer2TextFactory -text DotifyTranslator/src/META-INF/services/org.daisy.dotify.translator.BrailleTranslatorFactory -text DotifyTranslator/src/META-INF/services/org.daisy.dotify.translator.UncontractedBrailleFilter -text DotifyTranslator/src/org/daisy/dotify/hyphenator/AbstractHyphenator.java -text @@ -505,6 +506,8 @@ DotifyTranslator/src/org/daisy/dotify/impl/hyphenator/latex/resource-files/utf8/ DotifyTranslator/src/org/daisy/dotify/impl/hyphenator/latex/resource-files/utf8/hyph-uk.tex -text DotifyTranslator/src/org/daisy/dotify/impl/hyphenator/latex/resource-files/utf8/hyph-zh-latn-pinyin.tex -text DotifyTranslator/src/org/daisy/dotify/impl/hyphenator/latex/resource-files/zh-latn.xml -text +DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2Text.java -text +DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2TextFactory.java -text DotifyTranslator/src/org/daisy/dotify/impl/translator/DefaultBypassTranslatorFactory.java -text DotifyTranslator/src/org/daisy/dotify/impl/translator/sv_SE/CapitalizationMarkers.java -text svneol=unset#text/plain DotifyTranslator/src/org/daisy/dotify/impl/translator/sv_SE/SwedishBrailleFilter.java -text svneol=unset#text/plain @@ -520,6 +523,10 @@ DotifyTranslator/src/org/daisy/dotify/text/CombinationFilter.java -text svneol=u DotifyTranslator/src/org/daisy/dotify/text/FilterFactory.java -text svneol=unset#text/plain DotifyTranslator/src/org/daisy/dotify/text/FilterLocale.java -text svneol=unset#text/plain DotifyTranslator/src/org/daisy/dotify/text/IdentityFilter.java -text +DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.java -text +DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactory.java -text +DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactoryMaker.java -text +DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.java -text DotifyTranslator/src/org/daisy/dotify/text/RegexFilter.java -text svneol=unset#text/plain DotifyTranslator/src/org/daisy/dotify/text/SimpleCharReplacer.java -text DotifyTranslator/src/org/daisy/dotify/text/SplitResult.java -text svneol=unset#text/plain @@ -541,10 +548,12 @@ DotifyTranslator/test/org/daisy/dotify/hyphenator/HyphenatorFactoryMakerTest.jav DotifyTranslator/test/org/daisy/dotify/hyphenator/SwedishHyphenation2Test.java -text DotifyTranslator/test/org/daisy/dotify/hyphenator/SwedishHyphenationTest.java -text DotifyTranslator/test/org/daisy/dotify/impl/hyphenator/latex/LatexHyphenatorFactoryTest.java -text +DotifyTranslator/test/org/daisy/dotify/impl/text/SwedishInteger2TextTest.java -text DotifyTranslator/test/org/daisy/dotify/impl/translator/DefaultBypassTranslatorTest.java -text DotifyTranslator/test/org/daisy/dotify/impl/translator/sv_SE/CapitalizationMarkersTest.java -text DotifyTranslator/test/org/daisy/dotify/impl/translator/sv_SE/SwedishBrailleFilterTest.java -text DotifyTranslator/test/org/daisy/dotify/impl/translator/sv_SE/SwedishBrailleTranslatorTest.java -text DotifyTranslator/test/org/daisy/dotify/text/BreakPointHandlerTest.java -text svneol=unset#text/plain DotifyTranslator/test/org/daisy/dotify/text/FilterLocaleTest.java -text svneol=unset#text/plain +DotifyTranslator/test/org/daisy/dotify/text/Integer2TextFactoryTest.java -text DotifyTranslator/test/org/daisy/dotify/translator/UncontractedBrailleFilterFactoryTest.java -text svneol=unset#text/plain diff --git a/DotifyTranslator/src/META-INF/services/org.daisy.dotify.text.Integer2TextFactory b/DotifyTranslator/src/META-INF/services/org.daisy.dotify.text.Integer2TextFactory new file mode 100644 index 00000000..539d1d63 --- /dev/null +++ b/DotifyTranslator/src/META-INF/services/org.daisy.dotify.text.Integer2TextFactory @@ -0,0 +1 @@ +org.daisy.dotify.impl.text.SwedishInteger2TextFactory \ No newline at end of file diff --git a/DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2Text.java b/DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2Text.java new file mode 100644 index 00000000..47ded94c --- /dev/null +++ b/DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2Text.java @@ -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) : ""); + } + } + +} diff --git a/DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2TextFactory.java b/DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2TextFactory.java new file mode 100644 index 00000000..a7966c52 --- /dev/null +++ b/DotifyTranslator/src/org/daisy/dotify/impl/text/SwedishInteger2TextFactory.java @@ -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(); + } + +} diff --git a/DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.java b/DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.java new file mode 100644 index 00000000..53f6351a --- /dev/null +++ b/DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.java @@ -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; + +} diff --git a/DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactory.java b/DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactory.java new file mode 100644 index 00000000..cbcde279 --- /dev/null +++ b/DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactory.java @@ -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; +} diff --git a/DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactoryMaker.java b/DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactoryMaker.java new file mode 100644 index 00000000..fac73d36 --- /dev/null +++ b/DotifyTranslator/src/org/daisy/dotify/text/Integer2TextFactoryMaker.java @@ -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 filters; + private final Map map; + private final Logger logger; + + protected Integer2TextFactoryMaker() { + logger = Logger.getLogger(Integer2TextFactoryMaker.class.getCanonicalName()); + filters = new ArrayList(); + Iterator i = ServiceRegistry.lookupProviders(Integer2TextFactory.class); + while (i.hasNext()) { + filters.add(i.next()); + } + this.map = new HashMap(); + } + + /** + * Creates a new integer2text factory maker. + * + * @return returns a new integer2text factory maker + */ + public static Integer2TextFactoryMaker newInstance() { + Iterator 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); + } + +} diff --git a/DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.java b/DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.java new file mode 100644 index 00000000..ccf0dfc1 --- /dev/null +++ b/DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.java @@ -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); + } + +} diff --git a/DotifyTranslator/test/org/daisy/dotify/impl/text/SwedishInteger2TextTest.java b/DotifyTranslator/test/org/daisy/dotify/impl/text/SwedishInteger2TextTest.java new file mode 100644 index 00000000..c80dc905 --- /dev/null +++ b/DotifyTranslator/test/org/daisy/dotify/impl/text/SwedishInteger2TextTest.java @@ -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)); + } + +} diff --git a/DotifyTranslator/test/org/daisy/dotify/text/Integer2TextFactoryTest.java b/DotifyTranslator/test/org/daisy/dotify/text/Integer2TextFactoryTest.java new file mode 100644 index 00000000..61a125a4 --- /dev/null +++ b/DotifyTranslator/test/org/daisy/dotify/text/Integer2TextFactoryTest.java @@ -0,0 +1,14 @@ +package org.daisy.dotify.text; + +import static org.junit.Assert.assertNotNull; + +import org.daisy.dotify.hyphenator.UnsupportedLocaleException; +import org.junit.Test; + +public class Integer2TextFactoryTest { + + @Test + public void testSwedishFactoryMaker() throws UnsupportedLocaleException { + assertNotNull(Integer2TextFactoryMaker.newInstance().newInteger2Text(FilterLocale.parse("sv-SE"))); + } +}