Fix furigana with katakana in headword
CHANGELOG.md
| 14 | 14 | This fixes the issues where the furigana was not fully visible if it took | |
| 15 | 15 | too much space, as well as the view disappearing completely when the text was | |
| 16 | 16 | too large. | |
| 17 | + | * Fixed furigana with katakana in headword. It used to make the furigana matching | |
| 18 | + | fail completely. | |
| 17 | 19 | ||
| 18 | 20 | ### Features | |
| 19 | 21 |
app/src/main/java/eu/lepiller/nani/ResultPagerAdapter.java
| 18 | 18 | import java.util.List; | |
| 19 | 19 | import java.util.Map; | |
| 20 | 20 | ||
| 21 | - | import eu.lepiller.views.RubyTextView; | |
| 21 | + | import me.weilunli.views.RubyTextView; | |
| 22 | 22 | import eu.lepiller.nani.result.KanjiResult; | |
| 23 | 23 | import eu.lepiller.nani.result.Result; | |
| 24 | 24 |
app/src/main/java/eu/lepiller/nani/result/Result.java
| 10 | 10 | import java.util.regex.Pattern; | |
| 11 | 11 | ||
| 12 | 12 | import static java.lang.Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS; | |
| 13 | + | import static java.lang.Character.UnicodeBlock.KATAKANA; | |
| 13 | 14 | ||
| 14 | 15 | public class Result { | |
| 15 | 16 | public static class Source { | |
… | |||
| 139 | 140 | ||
| 140 | 141 | // split the text into kanji / not kanji portions | |
| 141 | 142 | ArrayList<String> portions = new ArrayList<>(); | |
| 143 | + | ArrayList<String> portionsMatcher = new ArrayList<>(); | |
| 142 | 144 | ||
| 143 | 145 | StringBuilder current = new StringBuilder(); | |
| 146 | + | StringBuilder currentMatcher = new StringBuilder(); | |
| 144 | 147 | Character.UnicodeBlock b = CJK_UNIFIED_IDEOGRAPHS; | |
| 145 | 148 | ||
| 149 | + | MojiConverter converter = new MojiConverter(); | |
| 146 | 150 | for(int i=0; i<txt.length(); i++) { | |
| 147 | 151 | Character.UnicodeBlock b2 = Character.UnicodeBlock.of(txt.charAt(i)); | |
| 148 | 152 | if(b == b2) { | |
| 153 | + | // if the headwork contains katakana, convert it to hiragana to match pronunciation | |
| 154 | + | // better. | |
| 149 | 155 | current.append(txt.charAt(i)); | |
| 156 | + | if(b2 == KATAKANA) { | |
| 157 | + | String s = new String(new char[]{txt.charAt(i)}); | |
| 158 | + | String hiragana = converter.convertRomajiToHiragana(converter.convertKanaToRomaji(s)); | |
| 159 | + | currentMatcher.append(hiragana.charAt(0)); | |
| 160 | + | } else { | |
| 161 | + | currentMatcher.append(txt.charAt(i)); | |
| 162 | + | } | |
| 150 | 163 | } else { | |
| 151 | 164 | String s = current.toString(); | |
| 152 | 165 | if(!s.isEmpty()) | |
| 153 | 166 | portions.add(s); | |
| 167 | + | s = currentMatcher.toString(); | |
| 168 | + | if(!s.isEmpty()) | |
| 169 | + | portionsMatcher.add(s); | |
| 154 | 170 | current = new StringBuilder(); | |
| 171 | + | currentMatcher = new StringBuilder(); | |
| 155 | 172 | current.append(txt.charAt(i)); | |
| 173 | + | if(b2 == KATAKANA) { | |
| 174 | + | String katakana = new String(new char[]{txt.charAt(i)}); | |
| 175 | + | String hiragana = converter.convertRomajiToHiragana(converter.convertKanaToRomaji(katakana)); | |
| 176 | + | currentMatcher.append(hiragana.charAt(0)); | |
| 177 | + | } else { | |
| 178 | + | currentMatcher.append(txt.charAt(i)); | |
| 179 | + | } | |
| 156 | 180 | } | |
| 157 | 181 | ||
| 158 | 182 | b = b2; | |
… | |||
| 160 | 184 | String str = current.toString(); | |
| 161 | 185 | if(!str.isEmpty()) | |
| 162 | 186 | portions.add(str); | |
| 187 | + | str = currentMatcher.toString(); | |
| 188 | + | if(!str.isEmpty()) { | |
| 189 | + | portionsMatcher.add(str); | |
| 190 | + | } | |
| 163 | 191 | ||
| 164 | 192 | // Create a regexp to match kanji places | |
| 165 | 193 | current = new StringBuilder(); | |
| 166 | 194 | current.append("^"); | |
| 167 | - | for(String s: portions) { | |
| 195 | + | for(String s: portionsMatcher) { | |
| 168 | 196 | if(Character.UnicodeBlock.of(s.charAt(0)) == CJK_UNIFIED_IDEOGRAPHS) { | |
| 169 | 197 | current.append("(.*)"); | |
| 170 | 198 | } else { | |
… | |||
| 179 | 207 | Matcher m = p.matcher(reading); | |
| 180 | 208 | ||
| 181 | 209 | if(!m.matches()) { | |
| 182 | - | Log.v("RESULT", "Finaly: " + txt); | |
| 210 | + | Log.v("RESULT", "Finally: " + txt); | |
| 183 | 211 | return txt; | |
| 184 | 212 | } | |
| 185 | 213 | ||