Order merges by score
app/src/main/java/eu/lepiller/nani/result/Result.java
133 | 133 | return null; | |
134 | 134 | } | |
135 | 135 | ||
136 | - | private static boolean isKanji(char c) { | |
136 | + | private static boolean isKanji(char c) { | |
137 | 137 | Character.UnicodeBlock b = Character.UnicodeBlock.of(c); | |
138 | 138 | return b == CJK_UNIFIED_IDEOGRAPHS || | |
139 | 139 | b == CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A || | |
… | |||
239 | 239 | return current.toString(); | |
240 | 240 | } | |
241 | 241 | ||
242 | + | private static void restrictResult(Result other) { | |
243 | + | for(Sense s: other.senses) { | |
244 | + | boolean hasReadingLimits = false; | |
245 | + | boolean hasKanjiLimits = false; | |
246 | + | for(String l: s.limits) { | |
247 | + | if(other.kanjis.contains(l)) | |
248 | + | hasKanjiLimits = true; | |
249 | + | for(Reading r: other.readings) { | |
250 | + | if (r.readings.contains(l)) { | |
251 | + | hasReadingLimits = true; | |
252 | + | break; | |
253 | + | } | |
254 | + | } | |
255 | + | } | |
256 | + | if(!hasKanjiLimits) { | |
257 | + | s.limits.addAll(other.kanjis); | |
258 | + | } | |
259 | + | if(!hasReadingLimits) { | |
260 | + | for(Reading r: other.readings) | |
261 | + | s.limits.addAll(r.readings); | |
262 | + | } | |
263 | + | } | |
264 | + | } | |
265 | + | ||
242 | 266 | public void merge(Result other) { | |
267 | + | restrictResult(other); | |
268 | + | restrictResult(this); | |
269 | + | // merge meanings and readings | |
270 | + | Log.d("MERGE", "score, other: " + other.score + ", this: " + this.score); | |
271 | + | if(other.score <= this.score) { | |
272 | + | this.readings.addAll(0, other.readings); | |
273 | + | this.senses.addAll(0, other.senses); | |
274 | + | } else { | |
275 | + | this.readings.addAll(other.readings); | |
276 | + | this.senses.addAll(other.senses); | |
277 | + | } | |
278 | + | ||
243 | 279 | // merge kanjis | |
244 | 280 | for(String ok: other.kanjis) { | |
245 | 281 | boolean here = false; | |
246 | 282 | for (String k: kanjis) | |
247 | 283 | if(k.compareTo(ok) == 0) | |
248 | 284 | here = true; | |
249 | - | if(!here) | |
250 | - | kanjis.add(ok); | |
285 | + | if(!here) { | |
286 | + | // Add it first if the other entry has higher score | |
287 | + | if(other.score < this.score) | |
288 | + | kanjis.add(0, ok); | |
289 | + | else | |
290 | + | kanjis.add(ok); | |
291 | + | } | |
251 | 292 | } | |
252 | - | ||
253 | - | // merge readings | |
254 | - | readings.addAll(other.readings); | |
255 | - | ||
256 | - | // merge senses | |
257 | - | senses.addAll(other.senses); | |
258 | 293 | } | |
259 | 294 | } |