Order merges by score

Julien LepillerSat Jul 02 19:19:22+0200 2022

fcb8d49

Order merges by score

app/src/main/java/eu/lepiller/nani/result/Result.java

133133
        return null;
134134
    }
135135
136-
   private static boolean isKanji(char c) {
136+
    private static boolean isKanji(char c) {
137137
       Character.UnicodeBlock b = Character.UnicodeBlock.of(c);
138138
       return b == CJK_UNIFIED_IDEOGRAPHS ||
139139
              b == CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A ||

239239
        return current.toString();
240240
    }
241241
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+
242266
    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+
243279
        // merge kanjis
244280
        for(String ok: other.kanjis) {
245281
            boolean here = false;
246282
            for (String k: kanjis)
247283
                if(k.compareTo(ok) == 0)
248284
                    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+
            }
251292
        }
252-
253-
        // merge readings
254-
        readings.addAll(other.readings);
255-
256-
        // merge senses
257-
        senses.addAll(other.senses);
258293
    }
259294
}