2017-11-22 18:57:05 +01:00
|
|
|
|
package nodomain.freeyourgadget.gadgetbridge.util;
|
|
|
|
|
|
2018-08-24 10:42:27 +02:00
|
|
|
|
import java.util.Hashtable;
|
|
|
|
|
import java.util.regex.*;
|
2017-11-22 18:57:05 +01:00
|
|
|
|
|
2018-08-24 16:57:38 +02:00
|
|
|
|
// What's the reason to extending LanguageUtils?
|
|
|
|
|
// Just doing it because already done in the previous code.
|
2017-11-22 18:57:05 +01:00
|
|
|
|
public class BengaliLanguageUtils extends LanguageUtils {
|
2018-08-24 16:57:38 +02:00
|
|
|
|
// Composite Letters.
|
2018-08-24 10:42:27 +02:00
|
|
|
|
private final static Hashtable<String, String> composites = new Hashtable<String, String>() {
|
2017-11-22 18:57:05 +01:00
|
|
|
|
{
|
2018-08-24 10:42:27 +02:00
|
|
|
|
put("ক্ষ", "kkh");
|
|
|
|
|
put("ঞ্চ", "NC");
|
|
|
|
|
put("ঞ্ছ", "NCh");
|
|
|
|
|
put("ঞ্জ", "Ng");
|
|
|
|
|
put("জ্ঞ", "gg");
|
|
|
|
|
put("ঞ্ঝ", "Ngh");
|
|
|
|
|
put("্র", "r");
|
|
|
|
|
put("্ল", "l");
|
|
|
|
|
put("ষ্ম", "SSh");
|
|
|
|
|
put("র্", "r");
|
|
|
|
|
put("্য", "y");
|
|
|
|
|
put("্ব", "w");
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
2018-08-24 18:33:25 +02:00
|
|
|
|
// Vowels Only
|
|
|
|
|
private final static Hashtable<String, String> vowelsAndHasants = new Hashtable<String, String>() {
|
|
|
|
|
{
|
|
|
|
|
put("আ", "aa");
|
|
|
|
|
put("অ", "a");
|
|
|
|
|
put("ই", "i");
|
|
|
|
|
put("ঈ", "ii");
|
|
|
|
|
put("উ", "u");
|
|
|
|
|
put("ঊ", "uu");
|
|
|
|
|
put("ঋ", "ri");
|
|
|
|
|
put("এ", "e");
|
|
|
|
|
put("ঐ", "oi");
|
|
|
|
|
put("ও", "o");
|
|
|
|
|
put("ঔ", "ou");
|
|
|
|
|
put("া", "aa");
|
|
|
|
|
put("ি", "i");
|
|
|
|
|
put("ী", "ii");
|
|
|
|
|
put("ু", "u");
|
|
|
|
|
put("ূ", "uu");
|
|
|
|
|
put("ৃ", "r");
|
|
|
|
|
put("ে", "e");
|
|
|
|
|
put("ো", "o");
|
|
|
|
|
put("ৈ", "oi");
|
|
|
|
|
put("ৗ", "ou");
|
|
|
|
|
put("ৌ", "ou");
|
|
|
|
|
put("ং", "ng");
|
|
|
|
|
put("ঃ", "h");
|
|
|
|
|
put("।", ".");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-24 16:57:38 +02:00
|
|
|
|
// Single Character Letters.
|
2018-08-24 10:42:27 +02:00
|
|
|
|
private final static Hashtable<String, String> letters = new Hashtable<String, String>() {
|
2017-11-22 18:57:05 +01:00
|
|
|
|
{
|
2018-08-24 10:42:27 +02:00
|
|
|
|
put("আ", "aa");
|
|
|
|
|
put("অ", "a");
|
|
|
|
|
put("ই", "i");
|
|
|
|
|
put("ঈ", "ii");
|
|
|
|
|
put("উ", "u");
|
|
|
|
|
put("ঊ", "uu");
|
|
|
|
|
put("ঋ", "ri");
|
|
|
|
|
put("এ", "e");
|
|
|
|
|
put("ঐ", "oi");
|
|
|
|
|
put("ও", "o");
|
|
|
|
|
put("ঔ", "ou");
|
|
|
|
|
put("ক", "k");
|
|
|
|
|
put("খ", "kh");
|
|
|
|
|
put("গ", "g");
|
|
|
|
|
put("ঘ", "gh");
|
|
|
|
|
put("ঙ", "ng");
|
|
|
|
|
put("চ", "ch");
|
|
|
|
|
put("ছ", "chh");
|
|
|
|
|
put("জ", "j");
|
|
|
|
|
put("ঝ", "jh");
|
|
|
|
|
put("ঞ", "Ng");
|
|
|
|
|
put("ট", "T");
|
|
|
|
|
put("ঠ", "Th");
|
|
|
|
|
put("ড", "D");
|
|
|
|
|
put("ঢ", "Dh");
|
|
|
|
|
put("ণ", "N");
|
|
|
|
|
put("ত", "t");
|
|
|
|
|
put("থ", "th");
|
|
|
|
|
put("দ", "d");
|
|
|
|
|
put("ধ", "dh");
|
|
|
|
|
put("ন", "n");
|
|
|
|
|
put("প", "p");
|
|
|
|
|
put("ফ", "ph");
|
|
|
|
|
put("ব", "b");
|
|
|
|
|
put("ভ", "bh");
|
|
|
|
|
put("ম", "m");
|
|
|
|
|
put("য", "J");
|
|
|
|
|
put("র", "r");
|
|
|
|
|
put("ল", "l");
|
|
|
|
|
put("শ", "sh");
|
|
|
|
|
put("ষ", "Sh");
|
|
|
|
|
put("স", "s");
|
|
|
|
|
put("হ", "h");
|
|
|
|
|
put("ড়", "rh");
|
|
|
|
|
put("ঢ়", "rH");
|
|
|
|
|
put("য়", "y");
|
|
|
|
|
put("ৎ", "t");
|
|
|
|
|
put("০", "0");
|
|
|
|
|
put("১", "1");
|
|
|
|
|
put("২", "2");
|
|
|
|
|
put("৩", "3");
|
|
|
|
|
put("৪", "4");
|
|
|
|
|
put("৫", "5");
|
|
|
|
|
put("৬", "6");
|
|
|
|
|
put("৭", "7");
|
|
|
|
|
put("৮", "8");
|
|
|
|
|
put("৯", "9");
|
|
|
|
|
put("া", "aa");
|
|
|
|
|
put("ি", "i");
|
|
|
|
|
put("ী", "ii");
|
|
|
|
|
put("ু", "u");
|
|
|
|
|
put("ূ", "uu");
|
|
|
|
|
put("ৃ", "r");
|
|
|
|
|
put("ে", "e");
|
|
|
|
|
put("ো", "o");
|
|
|
|
|
put("ৈ", "oi");
|
|
|
|
|
put("ৗ", "ou");
|
|
|
|
|
put("ৌ", "ou");
|
|
|
|
|
put("ং", "ng");
|
|
|
|
|
put("ঃ", "h");
|
|
|
|
|
put("ঁ", "nN");
|
|
|
|
|
put("।", ".");
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-24 16:57:38 +02:00
|
|
|
|
// The regex to extract Bengali characters in nested groups.
|
2018-08-24 18:33:25 +02:00
|
|
|
|
private final static String pattern = "(র্){0,1}(([অ-হড়-য়])(্([অ-মশ-হড়-য়]))*)((){0,1}(্([য-ল]))){0,1}([া-ৌ]){0,1}|[্ঁঃংৎ০-৯।]| ";
|
2017-11-22 18:57:05 +01:00
|
|
|
|
|
2018-08-24 18:33:25 +02:00
|
|
|
|
private static String getVal(String key){
|
|
|
|
|
if (key != null) {
|
|
|
|
|
boolean hasKey = composites.containsKey(key);
|
|
|
|
|
if (hasKey) {
|
|
|
|
|
return composites.get(key);
|
|
|
|
|
}
|
|
|
|
|
hasKey = letters.containsKey(key);
|
|
|
|
|
if (hasKey) {
|
|
|
|
|
return letters.get(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-22 18:57:05 +01:00
|
|
|
|
public static String transliterate(String txt) {
|
|
|
|
|
if (txt.isEmpty()) {
|
|
|
|
|
return txt;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-24 10:42:27 +02:00
|
|
|
|
Pattern r = Pattern.compile(pattern);
|
|
|
|
|
Matcher m = r.matcher(txt);
|
|
|
|
|
StringBuffer sb = new StringBuffer();
|
|
|
|
|
while (m.find()) {
|
|
|
|
|
String appendableString = "";
|
|
|
|
|
String reff = m.group(1);
|
|
|
|
|
if (reff != null) {
|
|
|
|
|
appendableString = appendableString + "rr";
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
2018-08-24 16:57:38 +02:00
|
|
|
|
// This is a filter-down approach. First considering larger groups,
|
|
|
|
|
// If found any match breaks their. Else go to the next step.
|
|
|
|
|
// Helpful to solve some corner-cases.
|
2018-08-24 18:33:25 +02:00
|
|
|
|
String mainPart = getVal(m.group(2));
|
|
|
|
|
if (mainPart != null) {
|
|
|
|
|
appendableString = appendableString + mainPart;
|
|
|
|
|
} else {
|
|
|
|
|
String firstPart = getVal(m.group(3));
|
|
|
|
|
if (firstPart != null){
|
|
|
|
|
appendableString = appendableString + firstPart;
|
|
|
|
|
}
|
|
|
|
|
int g = 4;
|
|
|
|
|
while (g < 6){
|
|
|
|
|
String part = getVal(m.group(g));
|
|
|
|
|
if (part != null){
|
|
|
|
|
appendableString = appendableString + part;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
g = g + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int g = 6;
|
|
|
|
|
while (g < 10) {
|
|
|
|
|
String key = getVal(m.group(g));
|
|
|
|
|
if (key != null){
|
|
|
|
|
appendableString = appendableString + key;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
g = g + 1;
|
2018-08-24 10:42:27 +02:00
|
|
|
|
}
|
2018-08-24 18:33:25 +02:00
|
|
|
|
String kaar = m.group(10);
|
2018-08-24 10:42:27 +02:00
|
|
|
|
if (kaar != null) {
|
|
|
|
|
boolean hasKey = letters.containsKey(kaar);
|
|
|
|
|
if (hasKey) {
|
|
|
|
|
appendableString = appendableString + letters.get(kaar);
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
2018-08-24 18:33:25 +02:00
|
|
|
|
} else if (appendableString.length() > 0 && !vowelsAndHasants.containsKey(m.group(0))) {
|
2018-08-24 16:57:38 +02:00
|
|
|
|
// Adding 'a' like ITRANS if no vowel is present.
|
|
|
|
|
// TODO: Have to add it dynamically using Bengali grammer rules.
|
2018-08-24 10:42:27 +02:00
|
|
|
|
appendableString = appendableString + "a";
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
2018-08-24 10:42:27 +02:00
|
|
|
|
String others = m.group(0);
|
|
|
|
|
if (others != null) {
|
2017-11-22 18:57:05 +01:00
|
|
|
|
|
2018-08-24 10:42:27 +02:00
|
|
|
|
if (appendableString.length() <= 0) {
|
|
|
|
|
appendableString = appendableString + others;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
m.appendReplacement(sb, appendableString);
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
2018-08-24 10:42:27 +02:00
|
|
|
|
m.appendTail(sb);
|
|
|
|
|
return sb.toString();
|
2017-11-22 18:57:05 +01:00
|
|
|
|
}
|
2018-08-24 10:42:27 +02:00
|
|
|
|
}
|