Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor fuzzy #2359

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/src/main/java/fr/neamar/kiss/CustomIconDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@
import fr.neamar.kiss.icons.SystemIconPack;
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.utils.DrawableUtils;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyFactory;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.UserHandle;
import fr.neamar.kiss.utils.Utilities;

Expand Down Expand Up @@ -308,13 +309,13 @@ private void refreshList() {
IconsHandler iconsHandler = KissApplication.getApplication(getActivity()).getIconsHandler();
IconPackXML iconPack = iconsHandler.getCustomIconPack();
if (iconPack != null) {
Collection<IconPackXML.DrawableInfo> drawables = ((IconPackXML) iconPack).getDrawableList();
Collection<IconPackXML.DrawableInfo> drawables = iconPack.getDrawableList();
if (drawables != null) {
StringNormalizer.Result normalized = StringNormalizer.normalizeWithResult(mSearch.getText(), true);
FuzzyScore fuzzyScore = new FuzzyScore(normalized.codePoints);
FuzzyScore fuzzyScore = FuzzyFactory.createFuzzyScore(getActivity(), normalized.codePoints);
for (IconPackXML.DrawableInfo info : drawables) {
if (fuzzyScore.match(info.getDrawableName()).match)
mIconData.add(new IconData((IconPackXML) iconPack, info));
mIconData.add(new IconData(iconPack, info));
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/fr/neamar/kiss/adapter/RecordAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
import fr.neamar.kiss.result.ShortcutsResult;
import fr.neamar.kiss.searcher.QueryInterface;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyFactory;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;

public class RecordAdapter extends BaseAdapter implements SectionIndexer {
private final QueryInterface parent;
Expand Down Expand Up @@ -128,12 +129,12 @@ public void removeResult(Context context, Result<?> result) {
parent.temporarilyDisableTranscriptMode();
}

public void updateResults(List<Result<?>> results, boolean isRefresh, String query) {
public void updateResults(@NonNull Context context, List<Result<?>> results, boolean isRefresh, String query) {
this.results.clear();
this.results.addAll(results);
StringNormalizer.Result queryNormalized = StringNormalizer.normalizeWithResult(query, false);

fuzzyScore = new FuzzyScore(queryNormalized.codePoints, true);
fuzzyScore = FuzzyFactory.createFuzzyScore(context, queryNormalized.codePoints, true);
notifyDataSetChanged();

if (isRefresh) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.pojo.AppPojo;
import fr.neamar.kiss.searcher.Searcher;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyFactory;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.UserHandle;
import fr.neamar.kiss.utils.fuzzy.MatchInfo;

public class AppProvider extends Provider<AppPojo> {

Expand Down Expand Up @@ -127,7 +129,7 @@ public void requestResults(String query, Searcher searcher) {
return;
}

FuzzyScore fuzzyScore = new FuzzyScore(queryNormalized.codePoints);
FuzzyScore fuzzyScore = FuzzyFactory.createFuzzyScore(this, queryNormalized.codePoints);

for (AppPojo pojo : getPojos()) {
// exclude apps from results
Expand All @@ -139,7 +141,7 @@ public void requestResults(String query, Searcher searcher) {
continue;
}

FuzzyScore.MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
boolean match = pojo.updateMatchingRelevance(matchInfo, false);

// check relevance for tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;

Expand All @@ -17,8 +16,10 @@
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.pojo.ContactsPojo;
import fr.neamar.kiss.searcher.Searcher;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyFactory;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.Permission;
import fr.neamar.kiss.utils.fuzzy.MatchInfo;

public class ContactsProvider extends Provider<ContactsPojo> {
private final static String TAG = ContactsProvider.class.getSimpleName();
Expand Down Expand Up @@ -88,10 +89,10 @@ public void requestResults(String query, Searcher searcher) {
return;
}

FuzzyScore fuzzyScore = new FuzzyScore(queryNormalized.codePoints);
FuzzyScore fuzzyScore = FuzzyFactory.createFuzzyScore(this, queryNormalized.codePoints);

for (ContactsPojo pojo : getPojos()) {
FuzzyScore.MatchInfo matchInfo;
MatchInfo matchInfo;
boolean match = false;

if (pojo.normalizedName != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.pojo.ShortcutPojo;
import fr.neamar.kiss.searcher.Searcher;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyFactory;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.ShortcutUtil;
import fr.neamar.kiss.utils.fuzzy.MatchInfo;

public class ShortcutsProvider extends Provider<ShortcutPojo> {
private static boolean notifiedKissNotDefaultLauncher = false;
Expand Down Expand Up @@ -87,15 +89,15 @@ public void requestResults(String query, Searcher searcher) {
return;
}

FuzzyScore fuzzyScore = new FuzzyScore(queryNormalized.codePoints);
FuzzyScore fuzzyScore = FuzzyFactory.createFuzzyScore(this, queryNormalized.codePoints);

for (ShortcutPojo pojo : getPojos()) {
// exclude favorites from results
if (excludedFavoriteIds.contains(pojo.getFavoriteId())) {
continue;
}

FuzzyScore.MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
boolean match = pojo.updateMatchingRelevance(matchInfo, false);

// check relevance for tags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import androidx.annotation.DrawableRes;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -18,13 +19,15 @@
import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.pojo.SettingPojo;
import fr.neamar.kiss.searcher.Searcher;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyFactory;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.MatchInfo;

public class SettingsProvider extends SimpleProvider<SettingPojo> {
private final static String SCHEME = "setting://";
private final String settingName;
private final List<SettingPojo> pojos;
private final SharedPreferences prefs;
private final WeakReference<Context> contextReference;

public SettingsProvider(Context context) {
pojos = new ArrayList<>();
Expand Down Expand Up @@ -61,8 +64,7 @@ public SettingsProvider(Context context) {

settingName = context.getString(R.string.settings_prefix).toLowerCase(Locale.ROOT);

this.prefs = PreferenceManager.getDefaultSharedPreferences(context);

this.contextReference = new WeakReference<>(context);
}

private void assignName(SettingPojo pojo, String name) {
Expand All @@ -88,20 +90,25 @@ private SettingPojo createPojo(String name, String settingName, @DrawableRes int

@Override
public void requestResults(String query, Searcher searcher) {
Context context = contextReference.get();
if (context == null) {
return;
}

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (!prefs.getBoolean("enable-settings", true)) {
return;
}

StringNormalizer.Result queryNormalized = StringNormalizer.normalizeWithResult(query, false);

if (queryNormalized.codePoints.length == 0) {
return;
}

FuzzyScore fuzzyScore = new FuzzyScore(queryNormalized.codePoints);
FuzzyScore fuzzyScore = FuzzyFactory.createFuzzyScore(context, queryNormalized.codePoints);

for (SettingPojo pojo : pojos) {
FuzzyScore.MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
MatchInfo matchInfo = fuzzyScore.match(pojo.normalizedName.codePoints);
boolean match = pojo.updateMatchingRelevance(matchInfo, false);

if (!match) {
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/fr/neamar/kiss/pojo/Pojo.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fr.neamar.kiss.pojo;

import fr.neamar.kiss.normalizer.StringNormalizer;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.MatchInfo;

public abstract class Pojo {
public static final String DEFAULT_ID = "(none)";
Expand Down Expand Up @@ -77,10 +77,10 @@ public String getFavoriteId() {
* Updates relevance of this pojo with score of given {@code matchInfo} if there is a match.
*
* @param matchInfo used for update
* @param matched flag to indicate if there was already a match before. If {@code matched} is false relevance is always set to {@link fr.neamar.kiss.utils.FuzzyScore.MatchInfo#score}, else it will be only set if {@link fr.neamar.kiss.utils.FuzzyScore.MatchInfo#score} is also higher than current value of relevance.
* @return true, if {@link fr.neamar.kiss.utils.FuzzyScore.MatchInfo#match} is true and relevance was updated, else value of {@code matched} is returned as is
* @param matched flag to indicate if there was already a match before. If {@code matched} is false relevance is always set to {@link MatchInfo#score}, else it will be only set if {@link MatchInfo#score} is also higher than current value of relevance.
* @return true, if {@link MatchInfo#match} is true and relevance was updated, else value of {@code matched} is returned as is
*/
public boolean updateMatchingRelevance(FuzzyScore.MatchInfo matchInfo, boolean matched) {
public boolean updateMatchingRelevance(MatchInfo matchInfo, boolean matched) {
if (matchInfo.match && (!matched || matchInfo.score > relevance)) {
this.relevance = matchInfo.score;
return true;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/result/AppResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
import fr.neamar.kiss.ui.GoogleCalendarIcon;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.DrawableUtils;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.PackageManagerUtils;
import fr.neamar.kiss.utils.SpaceTokenizer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import fr.neamar.kiss.ui.ImprovedQuickContactBadge;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.ui.ShapedContactBadge;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.MimeTypeUtils;
import fr.neamar.kiss.utils.PackageManagerUtils;
import fr.neamar.kiss.utils.UserHandle;
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/result/PhoneResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import fr.neamar.kiss.adapter.RecordAdapter;
import fr.neamar.kiss.pojo.PhonePojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;

public class PhoneResult extends CallResult<PhonePojo> {

Expand Down
5 changes: 3 additions & 2 deletions app/src/main/java/fr/neamar/kiss/result/Result.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@
import fr.neamar.kiss.pojo.TagDummyPojo;
import fr.neamar.kiss.searcher.QueryInterface;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.MatchInfo;

public abstract class Result<T extends Pojo> {

Expand Down Expand Up @@ -135,7 +136,7 @@ void displayHighlighted(String text, List<Pair<Integer, Integer>> positions, Tex

boolean displayHighlighted(StringNormalizer.Result normalized, String text, FuzzyScore fuzzyScore,
TextView view, Context context) {
FuzzyScore.MatchInfo matchInfo = fuzzyScore.match(normalized.codePoints);
MatchInfo matchInfo = fuzzyScore.match(normalized.codePoints);

if (!matchInfo.match) {
view.setText(text);
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/result/SearchResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import fr.neamar.kiss.pojo.SearchPojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.ClipboardUtils;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.PackageManagerUtils;
import fr.neamar.kiss.utils.UserHandle;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import fr.neamar.kiss.R;
import fr.neamar.kiss.pojo.SettingPojo;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;

public class SettingsResult extends Result<SettingPojo> {
private static final String TAG = SettingsResult.class.getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import fr.neamar.kiss.pojo.ShortcutPojo;
import fr.neamar.kiss.ui.ListPopup;
import fr.neamar.kiss.utils.DrawableUtils;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.PackageManagerUtils;
import fr.neamar.kiss.utils.ShortcutUtil;
import fr.neamar.kiss.utils.SpaceTokenizer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import fr.neamar.kiss.UIColors;
import fr.neamar.kiss.pojo.TagDummyPojo;
import fr.neamar.kiss.utils.DrawableUtils;
import fr.neamar.kiss.utils.FuzzyScore;
import fr.neamar.kiss.utils.fuzzy.FuzzyScore;
import fr.neamar.kiss.utils.Utilities;

public class TagDummyResult extends Result<TagDummyPojo> {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/fr/neamar/kiss/searcher/Searcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected void onPostExecute(Void param) {

activity.beforeListChange();

activity.adapter.updateResults(results, isRefresh, query);
activity.adapter.updateResults(activity, results, isRefresh, query);

activity.afterListChange();
}
Expand Down
24 changes: 24 additions & 0 deletions app/src/main/java/fr/neamar/kiss/utils/fuzzy/FuzzyFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.neamar.kiss.utils.fuzzy;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import androidx.annotation.NonNull;

public class FuzzyFactory {

public static FuzzyScore createFuzzyScore(@NonNull Context context, int[] pattern) {
return createFuzzyScore(context, pattern, false);
}

public static FuzzyScore createFuzzyScore(@NonNull Context context, int[] pattern, boolean detailedMatchIndices) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
if (prefs.getBoolean("use-fuzzy-score-v2", false)) {
return new FuzzyScoreV2(pattern, detailedMatchIndices);
} else {
return new FuzzyScoreV1(pattern, detailedMatchIndices);
}
}

}
24 changes: 24 additions & 0 deletions app/src/main/java/fr/neamar/kiss/utils/fuzzy/FuzzyScore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package fr.neamar.kiss.utils.fuzzy;

public interface FuzzyScore {

FuzzyScore setFullWordBonus(int full_word_bonus);

FuzzyScore setAdjacencyBonus(int adjacency_bonus);

FuzzyScore setSeparatorBonus(int separator_bonus);

FuzzyScore setCamelBonus(int camel_bonus);

FuzzyScore setLeadingLetterPenalty(int leading_letter_penalty);

FuzzyScore setMaxLeadingLetterPenalty(int max_leading_letter_penalty);

FuzzyScore setUnmatchedLetterPenalty(int unmatched_letter_penalty);

FuzzyScore setFirstLetterBonus(int first_letter_bonus);

MatchInfo match(CharSequence text);

MatchInfo match(int[] text);
}
Loading