Merge results

Julien LepillerFri Jun 05 21:17:27+0200 2020

3ddf7bc

Merge results

CHANGELOG.md

1414
  icon in the notification, which was not supported.
1515
* Fix default radical button size which was too small.
1616
17+
### Features
18+
19+
* When two identical results come from different dictionaries, they are now
20+
  merged (eg. if you have two dictionaries for two different languages, you
21+
  will see the meanings for both languages in the same entry).
22+
1723
Changes In 0.2.2.1
1824
------------------
1925

app/src/main/java/eu/lepiller/nani/dictionary/DictionaryFactory.java

1414
import java.net.URL;
1515
import java.util.ArrayList;
1616
import java.util.HashMap;
17+
import java.util.HashSet;
1718
import java.util.List;
1819
import java.util.Locale;
1920
import java.util.Map;

183184
            throw new NoDictionaryException();
184185
185186
        int available = 0;
186-
        ArrayList<Result> results = new ArrayList<>();
187+
        HashMap<String, Result> results = new HashMap<>();
188+
187189
        for(Dictionary d: dictionaries) {
188190
            if (d instanceof JMDict && d.isDownloaded()) {
189191
                available++;
190192
                ArrayList<Result> dr = ((JMDict) d).search(text);
191-
                if(dr != null)
192-
                    results.addAll(dr);
193+
                if(dr != null) {
194+
                    for(Result r: dr) {
195+
                        String k = r.getKanji();
196+
                        if(results.containsKey(k)) {
197+
                            Result res = results.get(k);
198+
                            if(res != null)
199+
                                res.merge(r);
200+
                        } else {
201+
                            results.put(k, r);
202+
                        }
203+
                    }
204+
                }
193205
            }
194206
        }
195207
196208
        if(available == 0) {
197209
            throw new NoDictionaryException();
198210
        }
199-
        return results;
211+
212+
        return new ArrayList<>(results.values());
200213
    }
201214
202215
    public static Dictionary get(int position) {

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

167167
        Log.d("RESULT", "Finaly: " + current.toString());
168168
        return current.toString();
169169
    }
170+
171+
    public void merge(Result other) {
172+
        // merge kanjis
173+
        for(String ok: other.kanjis) {
174+
            boolean here = false;
175+
            for (String k: kanjis)
176+
                if(k.compareTo(ok) == 0)
177+
                    here = true;
178+
            if(!here)
179+
                kanjis.add(ok);
180+
        }
181+
182+
        // merge readings
183+
        readings.addAll(other.readings);
184+
185+
        // merge senses
186+
        senses.addAll(other.senses);
187+
    }
170188
}