1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-04 17:27:24 +01:00

Done the recommended edits, added more testcases, and fixed a bug.

This commit is contained in:
Utsob Roy 2018-09-01 00:38:15 +06:00
parent 98e747c5f5
commit 6a293bd40a
2 changed files with 63 additions and 51 deletions

View File

@ -1,13 +1,13 @@
package nodomain.freeyourgadget.gadgetbridge.util; package nodomain.freeyourgadget.gadgetbridge.util;
import java.util.Hashtable; import java.util.HashMap;
import java.util.regex.*; import java.util.regex.*;
// What's the reason to extending LanguageUtils? // What's the reason to extending LanguageUtils?
// Just doing it because already done in the previous code. // Just doing it because already done in the previous code.
public class BengaliLanguageUtils extends LanguageUtils { public class BengaliLanguageUtils extends LanguageUtils {
// Composite Letters. // Composite Letters.
private final static Hashtable<String, String> composites = new Hashtable<String, String>() { private final static HashMap<String, String> composites = new HashMap<String, String>() {
{ {
put("ক্ষ", "kkh"); put("ক্ষ", "kkh");
put("ঞ্চ", "NC"); put("ঞ্চ", "NC");
@ -24,7 +24,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
} }
}; };
// Vowels Only // Vowels Only
private final static Hashtable<String, String> vowelsAndHasants = new Hashtable<String, String>() { private final static HashMap<String, String> vowelsAndHasants = new HashMap<String, String>() {
{ {
put("", "aa"); put("", "aa");
put("", "a"); put("", "a");
@ -55,7 +55,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
}; };
// Single Character Letters. // Single Character Letters.
private final static Hashtable<String, String> letters = new Hashtable<String, String>() { private final static HashMap<String, String> letters = new HashMap<String, String>() {
{ {
put("", "aa"); put("", "aa");
put("", "a"); put("", "a");
@ -133,20 +133,21 @@ public class BengaliLanguageUtils extends LanguageUtils {
}; };
// The regex to extract Bengali characters in nested groups. // The regex to extract Bengali characters in nested groups.
private final static String pattern = "(র্){0,1}(([অ-হড়-য়])(্([অ-মশ-হড়-য়]))*)((){0,1}(্([য-ল]))){0,1}([া-ৌ]){0,1}|[্ঁঃংৎ০-৯।]| "; private final static String pattern = "(র্){0,1}(([অ-হড়-য়])(্([অ-মশ-হড়-য়]))*)((){0,1}(্([য-ল]))){0,1}([া-ৌ]){0,1}|([্ঁঃংৎ০-৯।])| ";
private final static Pattern bengaliRegex = Pattern.compile(pattern);
private static String getVal(String key){ private static String getVal(String key) {
if (key != null) { if (key != null) {
boolean hasKey = composites.containsKey(key); boolean hasKey = composites.containsKey(key);
if (hasKey) { if (hasKey) {
return composites.get(key); return composites.get(key);
} }
hasKey = letters.containsKey(key); hasKey = letters.containsKey(key);
if (hasKey) { if (hasKey) {
return letters.get(key); return letters.get(key);
} }
} }
return null; return null;
} }
public static String transliterate(String txt) { public static String transliterate(String txt) {
@ -154,8 +155,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
return txt; return txt;
} }
Pattern r = Pattern.compile(pattern); Matcher m = bengaliRegex.matcher(txt);
Matcher m = r.matcher(txt);
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while (m.find()) { while (m.find()) {
String appendableString = ""; String appendableString = "";
@ -166,32 +166,32 @@ public class BengaliLanguageUtils extends LanguageUtils {
// This is a filter-down approach. First considering larger groups, // This is a filter-down approach. First considering larger groups,
// If found any match breaks their. Else go to the next step. // If found any match breaks their. Else go to the next step.
// Helpful to solve some corner-cases. // Helpful to solve some corner-cases.
String mainPart = getVal(m.group(2)); String mainPart = getVal(m.group(2));
if (mainPart != null) { if (mainPart != null) {
appendableString = appendableString + mainPart; appendableString = appendableString + mainPart;
} else { } else {
String firstPart = getVal(m.group(3)); String firstPart = getVal(m.group(3));
if (firstPart != null){ if (firstPart != null) {
appendableString = appendableString + firstPart; appendableString = appendableString + firstPart;
} }
int g = 4; int g = 4;
while (g < 6){ while (g < 6) {
String part = getVal(m.group(g)); String part = getVal(m.group(g));
if (part != null){ if (part != null) {
appendableString = appendableString + part; appendableString = appendableString + part;
break; break;
} }
g = g + 1; g = g + 1;
} }
} }
int g = 6; int g = 6;
while (g < 10) { while (g < 10) {
String key = getVal(m.group(g)); String key = getVal(m.group(g));
if (key != null){ if (key != null) {
appendableString = appendableString + key; appendableString = appendableString + key;
break; break;
} }
g = g + 1; g = g + 1;
} }
String kaar = m.group(10); String kaar = m.group(10);
if (kaar != null) { if (kaar != null) {
@ -204,6 +204,13 @@ public class BengaliLanguageUtils extends LanguageUtils {
// TODO: Have to add it dynamically using Bengali grammer rules. // TODO: Have to add it dynamically using Bengali grammer rules.
appendableString = appendableString + "a"; appendableString = appendableString + "a";
} }
String singleton = m.group(11);
if (singleton != null) {
boolean hasKeyS = letters.containsKey(singleton);
if (hasKeyS) {
appendableString = appendableString + letters.get(singleton);
}
}
String others = m.group(0); String others = m.group(0);
if (others != null) { if (others != null) {

View File

@ -17,7 +17,7 @@ import static org.junit.Assert.assertTrue;
public class LanguageUtilsTest extends TestBase { public class LanguageUtilsTest extends TestBase {
@Test @Test
public void testStringTransliterateCyrillic() throws Exception { public void testStringTransliterateCyrillic() throws Exception {
//input with cyrillic and diacritic letters // input with cyrillic and diacritic letters
String input = "Прõсто текčт"; String input = "Прõсто текčт";
String output = LanguageUtils.transliterate(input); String output = LanguageUtils.transliterate(input);
String result = "Prosto tekct"; String result = "Prosto tekct";
@ -59,11 +59,15 @@ public class LanguageUtilsTest extends TestBase {
public void testStringTransliterateBengali() throws Exception { public void testStringTransliterateBengali() throws Exception {
//input with cyrillic and diacritic letters //input with cyrillic and diacritic letters
String input = "অনিরুদ্ধ"; String[] inputs = {"অনিরুদ্ধ", "বিজ্ঞানযাত্রা চলছে চলবে।", "আমি সব দেখেশুনে ক্ষেপে গিয়ে করি বাঙলায় চিৎকার!"};
String output = LanguageUtils.transliterate(input); String[] outputs = {"aniruddha", "biggaanaJaatraa chalachhe chalabe.", "aami saba dekheshune kkhepe giye kari baangalaaya chitkaara!"};
String result = "aniruddha";
assertEquals("Transliteration failed", result, output); String result;
for (int i = 0; i < inputs.length; i++) {
result = LanguageUtils.transliterate(inputs[i])
assertEquals("Transliteration failed", outputs[i], result);
}
} }
@Test @Test
@ -82,7 +86,8 @@ public class LanguageUtilsTest extends TestBase {
@Test @Test
public void testTransliterateOption() throws Exception { public void testTransliterateOption() throws Exception {
setDefaultTransliteration(); setDefaultTransliteration();
assertFalse("Transliteration option fail! Expected 'Off' by default, but result is 'On'", LanguageUtils.transliterate()); assertFalse("Transliteration option fail! Expected 'Off' by default, but result is 'On'",
LanguageUtils.transliterate());
enableTransliteration(true); enableTransliteration(true);
assertTrue("Transliteration option fail! Expected 'On', but result is 'Off'", LanguageUtils.transliterate()); assertTrue("Transliteration option fail! Expected 'On', but result is 'Off'", LanguageUtils.transliterate());