Skip to content

Commit bc62d3a

Browse files
committed
enabled filtering in Bopomofo mode
1 parent 2bb5b63 commit bc62d3a

File tree

4 files changed

+137
-10
lines changed

4 files changed

+137
-10
lines changed

app/languages/definitions/ChineseBopomofo.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ layout:
1515
- [ㄚ, ㄛ, ㄜ, ㄝ] # 7
1616
- [ㄞ, ㄟ, ㄠ, ㄡ] # 8
1717
- [ㄢ, ㄣ, ㄤ, ㄥ, ㄦ] # 9
18+
filterBySounds: yes
1819
sounds:
1920
- [I,0] #
20-
- [Yi,0] # ㄧ, probably unnecessary
21-
- [Y,0] # ㄧ, probably unnecessary
2221
- [U,0] #
23-
- [Wu,0] # ㄨ, probably unnecessary
24-
- [W,0] # ㄨ, probably unnecessary
2522
- [Yu,0] #
26-
- [Uu,0] # ㄩ, actually "ü", probably unnecessary
2723
- [B,1] #
2824
- [P,1] #
2925
- [M,1] #

app/languages/definitions/ChineseBopomofoLarge.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,11 @@ layout:
1515
- [ㄚ, ㄛ, ㄜ, ㄝ] # 7
1616
- [ㄞ, ㄟ, ㄠ, ㄡ] # 8
1717
- [ㄢ, ㄣ, ㄤ, ㄥ, ㄦ] # 9
18+
filterBySounds: yes
1819
sounds:
1920
- [I,0] #
20-
- [Yi,0] # ㄧ, probably unnecessary
21-
- [Y,0] # ㄧ, probably unnecessary
2221
- [U,0] #
23-
- [Wu,0] # ㄨ, probably unnecessary
24-
- [W,0] # ㄨ, probably unnecessary
2522
- [Yu,0] #
26-
- [Uu,0] # ㄩ, actually "ü", probably unnecessary
2723
- [B,1] #
2824
- [P,1] #
2925
- [M,1] #

app/src/main/java/io/github/sspanak/tt9/ime/modes/ModeBopomofo.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package io.github.sspanak.tt9.ime.modes;
22

3+
import androidx.annotation.NonNull;
34
import androidx.annotation.Nullable;
45

6+
import java.util.ArrayList;
7+
58
import io.github.sspanak.tt9.hacks.InputType;
69
import io.github.sspanak.tt9.ime.helpers.TextField;
10+
import io.github.sspanak.tt9.ime.modes.helpers.BopomofoConverter;
711
import io.github.sspanak.tt9.ime.modes.helpers.Sequences;
812
import io.github.sspanak.tt9.languages.EmojiLanguage;
913
import io.github.sspanak.tt9.languages.Language;
@@ -13,9 +17,11 @@
1317
import io.github.sspanak.tt9.util.chars.Characters;
1418

1519
public class ModeBopomofo extends ModePinyin {
20+
private final BopomofoConverter bpmfConvert;
1621

1722
protected ModeBopomofo(SettingsStore settings, Language lang, InputType inputType, TextField textField) {
1823
super(settings, lang, inputType, textField);
24+
bpmfConvert = new BopomofoConverter();
1925
seq = new Sequences("S1", "S0");
2026
}
2127

@@ -28,6 +34,20 @@ public boolean validateLanguage(@Nullable Language newLanguage) {
2834

2935
/* **************************** LOAD SUGGESTIONS *********************************/
3036

37+
@NonNull
38+
@Override
39+
public ArrayList<String> getSuggestions() {
40+
ArrayList<String> newSuggestions = new ArrayList<>();
41+
for (String latinSuggestion : suggestions) {
42+
// all transcriptions are stored in Latin in the database, so we need to convert them to
43+
// Bopomofo before presenting them to the user
44+
newSuggestions.add(bpmfConvert.toBopomofo(latinSuggestion));
45+
}
46+
47+
return newSuggestions;
48+
}
49+
50+
3151
/**
3252
* Not possible in Bopomofo mode, because 0-key is used for typing letters.
3353
*/
@@ -50,6 +70,13 @@ protected void setCustomSpecialCharacters() {
5070
);
5171
}
5272

73+
74+
@Override
75+
public boolean onReplaceSuggestion(@NonNull String bopomofoWord) {
76+
// convert the Bopomofo word to Latin to search for it in the database
77+
return super.onReplaceSuggestion(bpmfConvert.toLatin(bopomofoWord));
78+
}
79+
5380
/***************************** TYPING *********************************/
5481

5582
@Override
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package io.github.sspanak.tt9.ime.modes.helpers;
2+
3+
import java.util.HashMap;
4+
5+
public class BopomofoConverter {
6+
7+
private final HashMap<String, String> bopomofoToLatin = new HashMap<>();
8+
private final HashMap<String, String> latinToBopomofo = new HashMap<>();
9+
10+
public BopomofoConverter() {
11+
// because I was too lazy to parse ChineseBopomofo.yml...
12+
// the YAML is never going to change, so this is fine
13+
latinToBopomofo.put("I", "ㄧ");
14+
latinToBopomofo.put("U", "ㄨ");
15+
latinToBopomofo.put("Yu", "ㄩ");
16+
latinToBopomofo.put("B", "ㄅ");
17+
latinToBopomofo.put("P", "ㄆ");
18+
latinToBopomofo.put("M", "ㄇ");
19+
latinToBopomofo.put("F", "ㄈ");
20+
latinToBopomofo.put("D", "ㄉ");
21+
latinToBopomofo.put("T", "ㄊ");
22+
latinToBopomofo.put("N", "ㄋ");
23+
latinToBopomofo.put("L", "ㄌ");
24+
latinToBopomofo.put("G", "ㄍ");
25+
latinToBopomofo.put("K", "ㄎ");
26+
latinToBopomofo.put("H", "ㄏ");
27+
latinToBopomofo.put("J", "ㄐ");
28+
latinToBopomofo.put("Q", "ㄑ");
29+
latinToBopomofo.put("X", "ㄒ");
30+
latinToBopomofo.put("Zh", "ㄓ");
31+
latinToBopomofo.put("Ch", "ㄔ");
32+
latinToBopomofo.put("Sh", "ㄕ");
33+
latinToBopomofo.put("R", "ㄖ");
34+
latinToBopomofo.put("Z", "ㄗ");
35+
latinToBopomofo.put("C", "ㄘ");
36+
latinToBopomofo.put("S", "ㄙ");
37+
latinToBopomofo.put("A", "ㄚ");
38+
latinToBopomofo.put("O", "ㄛ");
39+
latinToBopomofo.put("E", "ㄜ");
40+
latinToBopomofo.put("Ie", "ㄝ");
41+
latinToBopomofo.put("Ai", "ㄞ");
42+
latinToBopomofo.put("Ei", "ㄟ");
43+
latinToBopomofo.put("Ao", "ㄠ");
44+
latinToBopomofo.put("Ou", "ㄡ");
45+
latinToBopomofo.put("An", "ㄢ");
46+
latinToBopomofo.put("En", "ㄣ");
47+
latinToBopomofo.put("Ang", "ㄤ");
48+
latinToBopomofo.put("Eng", "ㄥ");
49+
latinToBopomofo.put("Er", "ㄦ");
50+
51+
for (HashMap.Entry<String, String> entry : latinToBopomofo.entrySet()) {
52+
bopomofoToLatin.put(entry.getValue(), entry.getKey());
53+
}
54+
}
55+
56+
/**
57+
* Converts a Bopomofo string to its Latin transcription using the predefined mapping.
58+
* If a character is not found in the mapping, it is left unchanged.
59+
*/
60+
public String toLatin(String bopomofo) {
61+
StringBuilder result = new StringBuilder();
62+
for (char c : bopomofo.toCharArray()) {
63+
if (bopomofoToLatin.containsKey(String.valueOf(c))) {
64+
result.append(bopomofoToLatin.get(String.valueOf(c)));
65+
} else {
66+
result.append(c);
67+
}
68+
}
69+
return result.toString();
70+
}
71+
72+
/**
73+
* Converts a Latin string to its Bopomofo transcription using the predefined mapping.
74+
* If a character or sequence is not found in the mapping, it is left unchanged.
75+
*/
76+
public String toBopomofo(String latin) {
77+
StringBuilder result = new StringBuilder();
78+
int i = 0;
79+
while (i < latin.length()) {
80+
if (i + 3 <= latin.length()) {
81+
String three = latin.substring(i, i + 3);
82+
if (latinToBopomofo.containsKey(three)) {
83+
result.append(latinToBopomofo.get(three));
84+
i += 3;
85+
continue;
86+
}
87+
}
88+
89+
if (i + 2 <= latin.length()) {
90+
String two = latin.substring(i, i + 2);
91+
if (latinToBopomofo.containsKey(two)) {
92+
result.append(latinToBopomofo.get(two));
93+
i += 2;
94+
continue;
95+
}
96+
}
97+
98+
String one = latin.substring(i, i + 1);
99+
if (latinToBopomofo.containsKey(one)) {
100+
result.append(latinToBopomofo.get(one));
101+
} else {
102+
result.append(one);
103+
}
104+
i++;
105+
}
106+
return result.toString();
107+
}
108+
}

0 commit comments

Comments
 (0)