1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-14 05:59:26 +01:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/BengaliLanguageUtils.java

321 lines
11 KiB
Java
Raw Normal View History

2021-01-10 23:37:09 +01:00
/* Copyright (C) 2017-2021 Aniruddha Adhikary, Carsten Pfeiffer, Daniele
2019-02-13 20:43:30 +01:00
Gobbetti, Utsob Roy
This file is part of Gadgetbridge.
Gadgetbridge is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Gadgetbridge is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
2017-11-22 18:57:05 +01:00
package nodomain.freeyourgadget.gadgetbridge.util;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 {
// Composite Letters.
private final static HashMap<String, String> composites = new HashMap<String, String>() {
2017-11-22 18:57:05 +01: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
}
};
// Vowels Only
private final static HashMap<String, String> vowels = new HashMap<String, String>() {
{
put("", "aa");
put("", "a");
put("", "i");
put("", "ii");
put("", "u");
put("", "uu");
put("", "ri");
put("", "e");
put("", "oi");
put("", "o");
put("", "ou");
}
};
// Vowels and Hasants
private final static HashMap<String, String> vowelsAndHasants = new HashMap<String, String>() {
2017-11-22 18:57:05 +01:00
{
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("", ".");
2017-11-22 18:57:05 +01:00
}
};
2018-08-24 16:57:38 +02:00
// Single Character Letters.
private final static HashMap<String, String> letters = new HashMap<String, String>() {
2017-11-22 18:57:05 +01: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.
private final static String pattern = "(র্){0,1}(([অ-হড়-য়])(্([অ-মশ-হড়-য়]))*)((){0,1}(্([য-ল]))){0,1}([া-ৌ]){0,1}|([্ঁঃংৎ০-৯।])|(\\s)";
private final static Pattern bengaliRegex = Pattern.compile(pattern);
2017-11-22 18:57:05 +01:00
private static String getVal(String key) {
if (key != null) {
2018-09-01 04:59:57 +02:00
String comp = composites.get(key);
if (comp != null) {
return comp;
}
2018-09-01 04:59:57 +02:00
String sl = letters.get(key);
if (sl != null) {
return letters.get(key);
}
2017-11-22 18:57:05 +01:00
}
return null;
2017-11-22 18:57:05 +01:00
}
public static String transliterate(String txt) {
if (txt.isEmpty()) {
return txt;
}
Matcher m = bengaliRegex.matcher(txt);
StringBuffer sb = new StringBuffer();
String lastChar = "";
boolean lastHadComposition = false;
boolean lastHadKaar = false;
boolean nextNeedsO = false;
int lastHadO = 0;
while (m.find()) {
boolean thisNeedsO = false;
boolean changePronounciation = false;
boolean thisHadKaar = false;
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.
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;
}
2017-11-22 18:57:05 +01:00
}
if (m.group(2) != null && m.group(2).equals("ক্ষ")) {
changePronounciation = true;
thisNeedsO = true;
}
int g = 6;
while (g < 10) {
String key = getVal(m.group(g));
if (key != null) {
appendableString = appendableString + key;
break;
}
g = g + 1;
2017-11-22 18:57:05 +01:00
}
String phala = m.group(8);
if (phala != null && phala.equals("্য")) {
changePronounciation = true;
thisNeedsO = true;
}
String jukto = m.group(4);
if (jukto != null) {
thisNeedsO = true;
}
String kaar = m.group(10);
if (kaar != null) {
2018-09-01 04:59:57 +02:00
String kaarStr = letters.get(kaar);
if (kaarStr != null) {
appendableString = appendableString + kaarStr;
2018-11-01 23:16:14 +01:00
if (kaarStr.equals("i") || kaarStr.equals("ii") || kaarStr.equals("u") || kaarStr.equals("uu")) {
changePronounciation = true;
}
}
2017-11-22 18:57:05 +01:00
}
String singleton = m.group(11);
if (singleton != null) {
2018-09-01 04:59:57 +02:00
String singleStr = letters.get(singleton);
if (singleStr != null) {
appendableString = appendableString + singleStr;
2017-11-22 18:57:05 +01:00
}
}
if (changePronounciation && lastChar.equals("a")) {
sb.setCharAt(sb.length() - 1, 'o');
}
String others = m.group(0);
if (others != null) {
2017-11-22 18:57:05 +01:00
if (appendableString.length() <= 0) {
appendableString = appendableString + others;
}
}
String whitespace = m.group(12);
if (nextNeedsO && kaar == null && whitespace == null && !vowels.containsKey(m.group(0))) {
appendableString = appendableString + "o";
lastHadO++;
thisNeedsO = false;
}
if (((kaar != null && lastHadO > 1) || whitespace != null) && !lastHadKaar && sb.length() > 0
&& sb.charAt(sb.length() - 1) == 'o' && !lastHadComposition) {
sb.deleteCharAt(sb.length() - 1);
lastHadO = 0;
}
nextNeedsO = false;
if (thisNeedsO && kaar == null && whitespace == null && !vowels.containsKey(m.group(0))) {
appendableString = appendableString + "o";
lastHadO++;
}
if (appendableString.length() > 0 && !vowelsAndHasants.containsKey(m.group(0)) && kaar == null) {
nextNeedsO = true;
}
if (reff != null || m.group(4) != null || m.group(6) != null) {
lastHadComposition = true;
} else {
lastHadComposition = false;
}
if (kaar != null) {
lastHadKaar = true;
} else {
lastHadKaar = false;
}
m.appendReplacement(sb, appendableString);
lastChar = appendableString;
}
if (!lastHadKaar && sb.length() > 0 && sb.charAt(sb.length() - 1) == 'o' && !lastHadComposition) {
sb.deleteCharAt(sb.length() - 1);
2017-11-22 18:57:05 +01:00
}
m.appendTail(sb);
return sb.toString();
2017-11-22 18:57:05 +01:00
}
}