From fcecc0ed92d7bc68618b55b9e044a03f29ac82cc Mon Sep 17 00:00:00 2001 From: M66B Date: Tue, 5 Jan 2021 12:55:12 +0100 Subject: [PATCH] Refactoring --- .../eu/faircode/email/MessageClassifier.java | 33 ++++++++++++------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/MessageClassifier.java b/app/src/main/java/eu/faircode/email/MessageClassifier.java index a59ea254f3..482840f2b9 100644 --- a/app/src/main/java/eu/faircode/email/MessageClassifier.java +++ b/app/src/main/java/eu/faircode/email/MessageClassifier.java @@ -190,18 +190,23 @@ public class MessageClassifier { // Filter classes of common occurring words List applyClasses = new ArrayList<>(classFrequency.keySet()); - for (String class1 : classFrequency.keySet()) + for (String class1 : classFrequency.keySet()) { + Integer messages1 = classMessages.get(account).get(class1); + if (messages1 == null || messages1 == 0) + continue; + double percentage1 = (double) classFrequency.get(class1) / messages1; + if (percentage1 == 0) + continue; + for (String class2 : classFrequency.keySet()) if (!class1.equals(class2)) { - int messages1 = classMessages.get(account).get(class1); - int messages2 = classMessages.get(account).get(class2); - int frequency1 = classFrequency.get(class1); - int frequency2 = classFrequency.get(class2); - if (messages1 == 0 || messages2 == 0 || frequency1 == 0 || frequency2 == 0) + Integer messages2 = classMessages.get(account).get(class2); + if (messages2 == null || messages2 == 0) + continue; + double percentage2 = (double) classFrequency.get(class2) / messages2; + if (percentage2 == 0) continue; - double percentage1 = (double) frequency1 / messages1; - double percentage2 = (double) frequency2 / messages2; double factor = percentage1 / percentage2; if (factor > 1) factor = 1 / factor; @@ -211,6 +216,7 @@ public class MessageClassifier { break; } } + } for (String clazz : applyClasses) { int frequency = classFrequency.get(clazz); @@ -246,10 +252,13 @@ public class MessageClassifier { if (!added) return null; + if (maxMatchedWords == 0) + return null; + List chances = new ArrayList<>(); for (String clazz : classStats.keySet()) { - int messages = classMessages.get(account).get(clazz); - if (messages == 0 || maxMatchedWords == 0) + Integer messages = classMessages.get(account).get(clazz); + if (messages == null || messages == 0) continue; Stat stat = classStats.get(clazz); @@ -264,7 +273,9 @@ public class MessageClassifier { if (BuildConfig.DEBUG) Log.i("Classifier words=" + TextUtils.join(", ", words)); - if (chances.size() <= 1 || maxMatchedWords < MIN_MATCHED_WORDS) + if (maxMatchedWords < MIN_MATCHED_WORDS) + return null; + if (chances.size() <= 1) return null; Collections.sort(chances, new Comparator() {