1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 12:56:49 +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;
import java.util.Hashtable;
import java.util.HashMap;
import java.util.regex.*;
// What's the reason to extending LanguageUtils?
// Just doing it because already done in the previous code.
public class BengaliLanguageUtils extends LanguageUtils {
// 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("ঞ্চ", "NC");
@ -24,7 +24,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
}
};
// 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("", "a");
@ -55,7 +55,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
};
// 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("", "a");
@ -133,9 +133,10 @@ public class BengaliLanguageUtils extends LanguageUtils {
};
// 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) {
boolean hasKey = composites.containsKey(key);
if (hasKey) {
@ -154,8 +155,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
return txt;
}
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(txt);
Matcher m = bengaliRegex.matcher(txt);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String appendableString = "";
@ -171,13 +171,13 @@ public class BengaliLanguageUtils extends LanguageUtils {
appendableString = appendableString + mainPart;
} else {
String firstPart = getVal(m.group(3));
if (firstPart != null){
if (firstPart != null) {
appendableString = appendableString + firstPart;
}
int g = 4;
while (g < 6){
while (g < 6) {
String part = getVal(m.group(g));
if (part != null){
if (part != null) {
appendableString = appendableString + part;
break;
}
@ -187,7 +187,7 @@ public class BengaliLanguageUtils extends LanguageUtils {
int g = 6;
while (g < 10) {
String key = getVal(m.group(g));
if (key != null){
if (key != null) {
appendableString = appendableString + key;
break;
}
@ -204,6 +204,13 @@ public class BengaliLanguageUtils extends LanguageUtils {
// TODO: Have to add it dynamically using Bengali grammer rules.
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);
if (others != null) {

View File

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