Merge results
CHANGELOG.md
| 14 | 14 | icon in the notification, which was not supported. | |
| 15 | 15 | * Fix default radical button size which was too small. | |
| 16 | 16 | ||
| 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 | + | ||
| 17 | 23 | Changes In 0.2.2.1 | |
| 18 | 24 | ------------------ | |
| 19 | 25 |
app/src/main/java/eu/lepiller/nani/dictionary/DictionaryFactory.java
| 14 | 14 | import java.net.URL; | |
| 15 | 15 | import java.util.ArrayList; | |
| 16 | 16 | import java.util.HashMap; | |
| 17 | + | import java.util.HashSet; | |
| 17 | 18 | import java.util.List; | |
| 18 | 19 | import java.util.Locale; | |
| 19 | 20 | import java.util.Map; | |
… | |||
| 183 | 184 | throw new NoDictionaryException(); | |
| 184 | 185 | ||
| 185 | 186 | int available = 0; | |
| 186 | - | ArrayList<Result> results = new ArrayList<>(); | |
| 187 | + | HashMap<String, Result> results = new HashMap<>(); | |
| 188 | + | ||
| 187 | 189 | for(Dictionary d: dictionaries) { | |
| 188 | 190 | if (d instanceof JMDict && d.isDownloaded()) { | |
| 189 | 191 | available++; | |
| 190 | 192 | 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 | + | } | |
| 193 | 205 | } | |
| 194 | 206 | } | |
| 195 | 207 | ||
| 196 | 208 | if(available == 0) { | |
| 197 | 209 | throw new NoDictionaryException(); | |
| 198 | 210 | } | |
| 199 | - | return results; | |
| 211 | + | ||
| 212 | + | return new ArrayList<>(results.values()); | |
| 200 | 213 | } | |
| 201 | 214 | ||
| 202 | 215 | public static Dictionary get(int position) { | |
app/src/main/java/eu/lepiller/nani/result/Result.java
| 167 | 167 | Log.d("RESULT", "Finaly: " + current.toString()); | |
| 168 | 168 | return current.toString(); | |
| 169 | 169 | } | |
| 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 | + | } | |
| 170 | 188 | } |