Search for kanji in result if not in search string
app/src/main/java/eu/lepiller/nani/MainActivity.java
28 | 28 | import com.moji4j.MojiDetector; | |
29 | 29 | ||
30 | 30 | import java.util.ArrayList; | |
31 | + | import java.util.Collections; | |
31 | 32 | import java.util.List; | |
32 | 33 | ||
33 | 34 | import eu.lepiller.nani.dictionary.DictionaryException; | |
… | |||
205 | 206 | ArrayList<String> tried = new ArrayList<>(); | |
206 | 207 | tried.add(text); | |
207 | 208 | ||
208 | - | List<KanjiResult> kanjiResults = new ArrayList<>(); | |
209 | - | for(String c: text.split("")) { | |
210 | - | if(Character.UnicodeBlock.of(c.codePointAt(0)) == CJK_UNIFIED_IDEOGRAPHS) { | |
211 | - | KanjiResult r; | |
212 | - | try { | |
213 | - | r = DictionaryFactory.searchKanji(c); | |
214 | - | } catch(DictionaryException e) { | |
215 | - | return new SearchResult(e); | |
216 | - | } | |
217 | - | Log.d(TAG, "kanji " + c + ", result: " + r); | |
218 | - | if(r != null) | |
219 | - | kanjiResults.add(r); | |
220 | - | } | |
221 | - | } | |
222 | - | ||
223 | 209 | ArrayList<Result> searchResult; | |
224 | 210 | try { | |
225 | 211 | searchResult = DictionaryFactory.search(text); | |
… | |||
240 | 226 | if(searchResult.size() == 0) { | |
241 | 227 | return new SearchResult(new NoResultDictionaryException(tried)); | |
242 | 228 | } | |
229 | + | } | |
230 | + | ||
243 | 231 | ||
244 | - | return new SearchResult(searchResult, kanjiResults, text, true); | |
245 | - | } else { | |
246 | - | return new SearchResult(searchResult, kanjiResults, text, false); | |
232 | + | ||
233 | + | List<String> kanji = new ArrayList<>(); | |
234 | + | for(String c: text.split("")) { | |
235 | + | if(Character.UnicodeBlock.of(c.codePointAt(0)) == CJK_UNIFIED_IDEOGRAPHS) { | |
236 | + | kanji.add(c); | |
237 | + | } | |
238 | + | } | |
239 | + | if(kanji.size() == 0) { | |
240 | + | int count = 0; | |
241 | + | for(Result r: searchResult) { | |
242 | + | for(String alt: r.getAlternatives()) { | |
243 | + | for(String c: alt.split("")) { | |
244 | + | if(count>5) | |
245 | + | break; | |
246 | + | if(c.length() > 0 && Character.UnicodeBlock.of(c.codePointAt(0)) == CJK_UNIFIED_IDEOGRAPHS | |
247 | + | && !kanji.contains(c)) { | |
248 | + | count++; | |
249 | + | kanji.add(c); | |
250 | + | } | |
251 | + | } | |
252 | + | } | |
253 | + | } | |
247 | 254 | } | |
255 | + | ||
256 | + | List<KanjiResult> kanjiResults = new ArrayList<>(); | |
257 | + | for(String c: kanji) { | |
258 | + | KanjiResult r; | |
259 | + | try { | |
260 | + | r = DictionaryFactory.searchKanji(c); | |
261 | + | } catch(DictionaryException e) { | |
262 | + | return new SearchResult(e); | |
263 | + | } | |
264 | + | Log.d(TAG, "kanji " + c + ", result: " + r); | |
265 | + | if(r != null) | |
266 | + | kanjiResults.add(r); | |
267 | + | } | |
268 | + | ||
269 | + | return new SearchResult(searchResult, kanjiResults, text, true); | |
248 | 270 | } | |
249 | 271 | ||
250 | 272 | @Override |