Skip to content
This repository has been archived by the owner on Nov 9, 2018. It is now read-only.

Commit

Permalink
Added integer to text capabilities and an implementation for Swedish
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Håkansson committed May 29, 2013
1 parent 8bd4bee commit 71c314e
Show file tree
Hide file tree
Showing 10 changed files with 372 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.daisy.dotify.impl.text.SwedishInteger2TextFactory
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) : "");
}
}

}
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 DotifyTranslator/src/org/daisy/dotify/text/Integer2Text.java
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;

}
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;
}
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 DotifyTranslator/src/org/daisy/dotify/text/IntegerOutOfRange.java
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);
}

}
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));
}

}
Loading

0 comments on commit 71c314e

Please sign in to comment.