Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,25 @@ public SwedishBrailleFilter(String locale) {
this(locale, false);
}

/**
* Creates a new Swedish braille filter.
* @param locale the locale
* @param strict if true the result is braille only, if false the result
* contains break point characters such as space, dash and soft hyphen.
*/
public SwedishBrailleFilter(String locale, boolean strict) {
this(locale, strict, false);
}

/**
* Creates a new Swedish braille filter with the specified mode.
* @param locale the locale
* @param strict if true the result is braille only, if false the result
* contains break point characters such as space, dash and soft hyphen.
* @param useContractedBraille Whether or not the string should be translated to contracted braille.
*
*/
public SwedishBrailleFilter(String locale, boolean strict) {
public SwedishBrailleFilter(String locale, boolean strict, boolean useContractedBraille) {
filters = new CombinationFilter();
// Remove zero width space
filters.add(new RegexFilter("\\u200B", ""));
Expand All @@ -42,6 +54,12 @@ public SwedishBrailleFilter(String locale, boolean strict) {
filters.add(new CapitalizationMarkers());

Locale l = FilterLocale.parse(locale).toLocale();

if (useContractedBraille) {
// Text to braille, shorthand format
filters.add(new SwedishContractedBrailleFilter());
}

// Text to braille, Pas 1
filters.add(new UCharFilter(getResource("sv_SE-pas1.xml"), l));
// Text to braille, Pas 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,16 @@ public BrailleFilter newFilter(String locale, String mode) throws TranslatorConf
throw new SwedishFilterConfigurationException(e);
}
return new DefaultBrailleFilter(new SwedishBrailleFilter(loc.get(), true), loc.get(), sap, hyphenatorService);
}
} else if (loc.isPresent() && mode.equals(TranslatorType.CONTRACTED.toString())) {

DefaultMarkerProcessor sap;
try {
sap = new SwedishMarkerProcessorFactory().newMarkerProcessor(loc.get(), mode);
} catch (SwedishMarkerProcessorConfigurationException e) {
throw new SwedishFilterConfigurationException(e);
}
return new DefaultBrailleFilter(new SwedishBrailleFilter(loc.get(), true, true), loc.get(), sap, hyphenatorService);
}
throw new SwedishFilterConfigurationException("Factory does not support " + locale + "/" + mode);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public SwedishBrailleFilterFactoryService() {
this.specs = new ArrayList<>();
String displayName = RESOURCE_BUNDLE.getString("uncontracted-6-dot");
String desc = RESOURCE_BUNDLE.getString("uncontracted-description");
String contractedDisplayName = RESOURCE_BUNDLE.getString("contracted-6-dot");
String contractedDesc = RESOURCE_BUNDLE.getString("contracted-description");
specs.add(new TranslatorSpecification("sv", TranslatorMode.Builder
.withType(TranslatorType.UNCONTRACTED)
.displayName(displayName)
Expand All @@ -46,11 +48,27 @@ public SwedishBrailleFilterFactoryService() {
.displayName(displayName)
.description(desc)
.build()));
}

@Override
public boolean supportsSpecification(String locale, String mode) {
return ("sv".equalsIgnoreCase(locale) || "sv-SE".equalsIgnoreCase(locale)) && mode.equals(TranslatorType.UNCONTRACTED.toString());
specs.add(new TranslatorSpecification("sv", TranslatorMode.Builder
.withType(TranslatorType.CONTRACTED)
.displayName(contractedDisplayName)
.description(contractedDesc)
.build()));
specs.add(new TranslatorSpecification("sv-SE", TranslatorMode.Builder
.withType(TranslatorType.CONTRACTED)
.displayName(contractedDisplayName)
.description(contractedDesc)
.build()));
}

@Override
public boolean supportsSpecification(String locale, String mode) {
if (("sv".equalsIgnoreCase(locale) || "sv-SE".equalsIgnoreCase(locale)) && mode.equals(TranslatorType.UNCONTRACTED.toString())) {
return true;
} else if (("sv".equalsIgnoreCase(locale) || "sv-SE".equalsIgnoreCase(locale)) && mode.equals(TranslatorType.CONTRACTED.toString())) {
return true;
} else {
return false;
}
}

@Override
Expand All @@ -74,7 +92,7 @@ public void setHyphenator(HyphenatorFactoryMakerService hyphenator) {
public void unsetHyphenator(HyphenatorFactoryMakerService hyphenator) {
this.hyphenator = null;
}

@Override
public Collection<TranslatorSpecification> listSpecifications() {
return specs;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ public BrailleTranslator newTranslator(String locale, String mode) throws Transl
return new SimpleBrailleTranslator(
new DefaultBrailleFilter(new SwedishBrailleFilter(loc.get()), loc.get(), sap, hyphenatorService),
new DefaultBrailleFinalizer(), mode);
} else if (loc.isPresent() && mode.equals(TranslatorType.CONTRACTED.toString())) {

DefaultMarkerProcessor sap;
try {
sap = new SwedishMarkerProcessorFactory().newMarkerProcessor(loc.get(), mode);
} catch (SwedishMarkerProcessorConfigurationException e) {
throw new SwedishTranslatorConfigurationException(e);
}

return new SimpleBrailleTranslator(
new DefaultBrailleFilter(new SwedishBrailleFilter(loc.get(), false, true), loc.get(), sap, hyphenatorService),
new DefaultBrailleFinalizer(), mode);
} else if (loc.isPresent() && mode.equals(TranslatorType.PRE_TRANSLATED.toString())) {
return new SimpleBrailleTranslator(
new PreTranslatedBrailleFilter(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,21 @@ public class SwedishBrailleTranslatorFactoryService implements
public SwedishBrailleTranslatorFactoryService() {
this.specs = new ArrayList<>();
String uncontracted = RESOURCE_BUNDLE.getString("uncontracted-6-dot");
String contracted = RESOURCE_BUNDLE.getString("contracted-6-dot");
String preTranslated = RESOURCE_BUNDLE.getString("pre-translated");
String descUncontracted = RESOURCE_BUNDLE.getString("uncontracted-description");
String descContracted = RESOURCE_BUNDLE.getString("contracted-description");
String descPreTranslated = RESOURCE_BUNDLE.getString("pre-translated-description");
specs.add(new TranslatorSpecification("sv", TranslatorMode.Builder
.withType(TranslatorType.UNCONTRACTED)
.displayName(uncontracted)
.description(descUncontracted)
.build()));
specs.add(new TranslatorSpecification("sv", TranslatorMode.Builder
.withType(TranslatorType.CONTRACTED)
.displayName(contracted)
.description(descContracted)
.build()));
specs.add(new TranslatorSpecification("sv", TranslatorMode.Builder
.withType(TranslatorType.PRE_TRANSLATED)
.displayName(preTranslated)
Expand All @@ -53,6 +60,11 @@ public SwedishBrailleTranslatorFactoryService() {
.displayName(uncontracted)
.description(descUncontracted)
.build()));
specs.add(new TranslatorSpecification("sv-SE", TranslatorMode.Builder
.withType(TranslatorType.CONTRACTED)
.displayName(contracted)
.description(descContracted)
.build()));
specs.add(new TranslatorSpecification("sv-SE", TranslatorMode.Builder
.withType(TranslatorType.PRE_TRANSLATED)
.displayName(preTranslated)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.daisy.dotify.translator.impl.sv_SE;

import org.daisy.dotify.common.text.StringFilter;

import java.io.IOException;
import java.net.URL;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
*
*/
public class SwedishContractedBrailleFilter implements StringFilter {

private HashMap<String, String> contractedBrailleMap;
private static final String CONTRACTED_BRAILLE_TABLE_PATH = "sv_SE-single-character-contracted-words.xml";

public static final String CAPITAL_CHAR_MARKER = "\u2820";
public static final String SOFT_HYPHEN = "\u00AD";

/**
* Todo: add support for different grades of contraction.
* For now only single character contracted words is supported
*/
public SwedishContractedBrailleFilter() {
this.contractedBrailleMap = new HashMap<>();
this.loadTable();
}

/**
* Search and replace with contracted braille.
* First split the string on space character and go through all words and look for a match in the contractin table.
*
* @param str - The string that should be filterd
* @return the filtered string
*/
@Override
public String filter(String str) {

String[] words = str.split("\\s");
// Handle Edge case with no words.
if (words.length == 0) {
return str;
}
// Strip string from
Pattern pattern = Pattern.compile(CAPITAL_CHAR_MARKER + "*([\\p{javaUpperCase}\\p{javaLowerCase}"+SOFT_HYPHEN+"]+)");
StringBuilder sb = new StringBuilder();
String key, replace;

for (String word: words) {
Matcher matcher = pattern.matcher(word);
if (matcher.find()) {
key = matcher.group(1).toLowerCase().replace(SOFT_HYPHEN, "");
if (this.contractedBrailleMap.containsKey(key)) {
replace = this.contractedBrailleMap.get(key);
word = word.substring(0, matcher.start(1)) + replace + word.substring(matcher.end(1));
}
}
sb.append(word);
sb.append(" ");
}
str = sb.toString().trim();
return str;
}

/**
* Loads a table using the Properties class.
*/
private void loadTable() {
URL tableURL = this.getClass().getResource(CONTRACTED_BRAILLE_TABLE_PATH);
Properties props = new Properties();
try {
props.loadFromXML(tableURL.openStream());
} catch (IOException e) {
e.printStackTrace();
return;
}
Set<?> keys = props.keySet();
for (Iterator<?> it = keys.iterator(); it.hasNext(); ) {
String key = (String) it.next();
contractedBrailleMap.put(key, props.getProperty(key));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class SwedishMarkerProcessorFactory {

public DefaultMarkerProcessor newMarkerProcessor(String locale, String mode) throws SwedishMarkerProcessorConfigurationException {
if (FilterLocale.parse(locale).equals(sv)||FilterLocale.parse(locale).equals(sv_SE)) {
if (mode.equals(TranslatorType.UNCONTRACTED.toString())) {
if (mode.equals(TranslatorType.UNCONTRACTED.toString()) || mode.equals(TranslatorType.CONTRACTED.toString())) {

// Svenska skrivregler för punktskrift 2009, page 34
RegexMarkerDictionary strong = new RegexMarkerDictionary.Builder().
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
uncontracted-6-dot=Uncontracted (6-dot)
uncontracted-6-dot=Uncontracted (6-dot)
uncontracted-description=Uncontracted Swedish braille
contracted-6-dot=Contracted (6-dot)
contracted-description=Contracted Swedish braille
pre-translated=Pre-translated
pre-translated-description=Pre-translated braille
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Braille translation table for Swedish short script words.</comment>

<entry key="att">a</entry>
<entry key="bli">b</entry>
<entry key="och">c</entry>
<entry key="där">d</entry>
<entry key="eller">e</entry>
<entry key="från">f</entry>
<entry key="genom">g</entry>
<entry key="han">h</entry>
<entry key="jag">j</entry>
<entry key="kan">k</entry>
<entry key="lika">l</entry>
<entry key="man">m</entry>
<entry key="när">n</entry>
<entry key="på">p</entry>
<entry key="under">q</entry>
<entry key="har">r</entry>
<entry key="som">s</entry>
<entry key="till">t</entry>
<entry key="utan">u</entry>
<entry key="vid">v</entry>
<entry key="vad">w</entry>
<entry key="över">x</entry>
<entry key="mycket">y</entry>
<entry key="efter">z</entry>
<entry key="är">ä</entry>

<entry key="en">ê</entry>
<entry key="med">î</entry>
<entry key="er">û</entry>
<entry key="ett">§</entry>
<entry key="för">ë</entry>
<entry key="inte">ü</entry>
<entry key="de">ô</entry>
<entry key="det">è</entry>
<entry key="den">\</entry>
<entry key="var">à</entry>
</properties>
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,44 @@ public void testFractions_001() {
public void testCapitalIWithDot() {
assertEquals("⠠⠈⠊", filter.filter("İ"));
}

@Test
public void testFilterAppliesContractedBrailleFilterCorrectly(){
SwedishBrailleFilter filter_1 = new SwedishBrailleFilter("sv-SE", false, true);
String test_string = "jag vet att";
String filtered_string = filter_1.filter(test_string);
assertEquals("⠚ ⠧⠑⠞ ⠁", filtered_string);
}

@Test
public void testFilterAppliesContractedBrailleFilterCorrectlyWithCharCapitalization() {
SwedishBrailleFilter filter_1 = new SwedishBrailleFilter("sv-SE", false, true);
String test_string = "Hon hade många husdjur";
String filtered_string = filter_1.filter(test_string);
assertEquals("⠠⠓⠕⠝ ⠓⠁⠙⠑ ⠍⠡⠝⠛⠁ ⠓⠥⠎⠙⠚⠥⠗", filtered_string);
}

@Test
public void testFilterDoesNotCrashWhensContractedBrailleFilterIsAppliedForEmptyStrings() {
SwedishBrailleFilter filter_1 = new SwedishBrailleFilter("sv-SE", false, true);
String test_string = "";
String filtered_string = filter_1.filter(test_string);
assertEquals("", filtered_string);
}

@Test
public void testFilterAppliesContractedBrailleFilterCorrectlyWithWordCapitalization(){
SwedishBrailleFilter filter_1 = new SwedishBrailleFilter("sv-SE", false, true);
String test_string = "HEY jag heter java";
String filtered_string = filter_1.filter(test_string);
assertEquals("⠠⠠⠓⠑⠽ ⠚ ⠓⠑⠞⠑⠗ ⠚⠁⠧⠁", filtered_string);
}

@Test
public void testFilterAppliesContractedBrailleFilterCorrectlyWithContractionWithSequentialCaptalLetters(){
SwedishBrailleFilter filter_1 = new SwedishBrailleFilter("sv-SE", false, true);
String test_string = "JAG SAKNAR HONOM MYCKET";
String filtered_string = filter_1.filter(test_string);
assertEquals("⠠⠠⠠⠚ ⠎⠁⠅⠝⠁⠗ ⠓⠕⠝⠕⠍ ⠽⠱", filtered_string);
}
}
Loading