Skip to content

[Core] Testing FuzzySharp (levenshtein) for more robust fuzzy matching #616

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

Draft
wants to merge 4 commits into
base: dev
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Flow.Launcher.Plugin/SharedModels/MatchResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public MatchResult(bool success, SearchPrecisionScore searchPrecision, List<int>
/// <summary>
/// The final score of the match result with search precision filters applied.
/// </summary>
public int Score { get; private set; }
public int Score { get; set; }

/// <summary>
/// The raw calculated search score without any search precision filtering applied.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FuzzySharp" Version="2.0.2" />
<PackageReference Include="ini-parser" Version="2.5.2" />
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.0" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
Expand Down
44 changes: 43 additions & 1 deletion Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,26 @@ private static MatchResult Match(string query, IReadOnlyCollection<string> candi
if (candidates.Count == 0)
return null;

var match = candidates.Select(candidate => Main.Context.API.FuzzySearch(query, candidate))
var match = candidates.Select(candidate =>
{
var matchResult = Main.Context.API.FuzzySearch(query, candidate);
matchResult.Score = FuzzySearch(query, candidate);
return matchResult;
})
.MaxBy(match => match.Score);

return match?.IsSearchPrecisionScoreMet() ?? false ? match : null;
}

private static int FuzzySearch(string query, string stringToCompare)
{
var newScore = FuzzySharp.Fuzz.PartialTokenSetRatio(query.ToLower(), stringToCompare.ToLower());
if (stringToCompare.Length < query.Length)
newScore = 0;

return newScore;
}

public Result Result(string query, IPublicAPI api)
{
string title;
Expand All @@ -112,13 +126,16 @@ public Result Result(string query, IPublicAPI api)
{
title = resultName;
matchResult = Main.Context.API.FuzzySearch(query, resultName);
matchResult.Score = FuzzySearch(query, resultName);
}
else
{
// Search in both
title = $"{resultName}: {Description}";
var nameMatch = Main.Context.API.FuzzySearch(query, resultName);
nameMatch.Score = FuzzySearch(query, resultName);
var descriptionMatch = Main.Context.API.FuzzySearch(query, Description);
descriptionMatch.Score = FuzzySearch(query, Description);
if (descriptionMatch.Score > nameMatch.Score)
{
for (int i = 0; i < descriptionMatch.MatchData.Count; i++)
Expand All @@ -134,6 +151,31 @@ public Result Result(string query, IPublicAPI api)
}
}

// if (!matchResult.IsSearchPrecisionScoreMet())
// {
// if (ExecutableName != null) // only lnk program will need this one
// matchResult = StringMatcher.FuzzySearch(query, ExecutableName);
//
// if (!matchResult.IsSearchPrecisionScoreMet())
// return null;
//
// matchResult.MatchData = new List<int>();
// }

/*var newScore = FuzzySharp.Fuzz.PartialTokenSetRatio(query.ToLower(), Name.ToLower());
if (Name.Length < query.Length)
newScore = 0;

var oldScore = matchResult.RawScore;
matchResult.RawScore = Math.Max(newScore, oldScore);
matchResult.RawScore *= 10; // Bypass the SearchPrecisionScore and related tests
matchResult.MatchData = new List<int>();

var result = new Result
{
Title = title + $"<{matchResult.Score}> <{newScore}> <{oldScore}>",
SubTitle = LnkResolvedPath ?? FullPath,*/

List<string> candidates = new List<string>();

if (!matchResult.IsSearchPrecisionScoreMet())
Expand Down
Loading