|
1 | 1 | package org.schabi.newpipe.settings.preferencesearch; |
2 | 2 |
|
3 | 3 | import android.text.TextUtils; |
| 4 | +import android.util.Pair; |
4 | 5 |
|
5 | 6 | import org.apache.commons.text.similarity.FuzzyScore; |
6 | 7 |
|
7 | 8 | import java.util.Comparator; |
8 | 9 | import java.util.Locale; |
9 | 10 | import java.util.Map; |
10 | 11 | import java.util.function.Function; |
| 12 | +import java.util.stream.Collectors; |
11 | 13 | import java.util.stream.Stream; |
12 | 14 |
|
13 | 15 | public class PreferenceFuzzySearchFunction |
@@ -72,39 +74,22 @@ static class FuzzySearchSpecificDTO { |
72 | 74 | ); |
73 | 75 |
|
74 | 76 | private final PreferenceSearchItem item; |
75 | | - private final float score; |
| 77 | + private final double score; |
76 | 78 |
|
77 | | - FuzzySearchSpecificDTO( |
78 | | - final PreferenceSearchItem item, |
79 | | - final String keyword) { |
| 79 | + FuzzySearchSpecificDTO(final PreferenceSearchItem item, final String keyword) { |
80 | 80 | this.item = item; |
81 | | - |
82 | | - float attributeScoreSum = 0; |
83 | | - int countOfAttributesWithScore = 0; |
84 | | - for (final Map.Entry<Function<PreferenceSearchItem, String>, Float> we |
85 | | - : WEIGHT_MAP.entrySet()) { |
86 | | - final String valueToProcess = we.getKey().apply(item); |
87 | | - if (valueToProcess.isEmpty()) { |
88 | | - continue; |
89 | | - } |
90 | | - |
91 | | - attributeScoreSum += |
92 | | - FUZZY_SCORE.fuzzyScore(valueToProcess, keyword) * we.getValue(); |
93 | | - countOfAttributesWithScore++; |
94 | | - } |
95 | | - |
96 | | - if (countOfAttributesWithScore != 0) { |
97 | | - this.score = attributeScoreSum / countOfAttributesWithScore; |
98 | | - } else { |
99 | | - this.score = 0; |
100 | | - } |
| 81 | + this.score = WEIGHT_MAP.entrySet().stream() |
| 82 | + .map(entry -> new Pair<>(entry.getKey().apply(item), entry.getValue())) |
| 83 | + .filter(pair -> !pair.first.isEmpty()) |
| 84 | + .collect(Collectors.averagingDouble(pair -> |
| 85 | + FUZZY_SCORE.fuzzyScore(pair.first, keyword) * pair.second)); |
101 | 86 | } |
102 | 87 |
|
103 | 88 | public PreferenceSearchItem getItem() { |
104 | 89 | return item; |
105 | 90 | } |
106 | 91 |
|
107 | | - public float getScore() { |
| 92 | + public double getScore() { |
108 | 93 | return score; |
109 | 94 | } |
110 | 95 | } |
|
0 commit comments