mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
Make FuzzMatcher class obsolete and keep backwards compatibility
This commit is contained in:
@@ -1,10 +1,8 @@
|
|||||||
using System.Text;
|
using System;
|
||||||
|
|
||||||
namespace Wox.Infrastructure
|
namespace Wox.Infrastructure
|
||||||
{
|
{
|
||||||
/// <summary>
|
[Obsolete("This class is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")]
|
||||||
/// refer to https://github.com/mattyork/fuzzy
|
|
||||||
/// </summary>
|
|
||||||
public class FuzzyMatcher
|
public class FuzzyMatcher
|
||||||
{
|
{
|
||||||
private string query;
|
private string query;
|
||||||
@@ -28,99 +26,7 @@ namespace Wox.Infrastructure
|
|||||||
|
|
||||||
public MatchResult Evaluate(string str)
|
public MatchResult Evaluate(string str)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(str) || string.IsNullOrEmpty(query)) return new MatchResult { Success = false };
|
return StringMatcher.FuzzySearch(query, str, opt);
|
||||||
|
|
||||||
var len = str.Length;
|
|
||||||
var compareString = opt.IgnoreCase ? str.ToLower() : str;
|
|
||||||
var pattern = opt.IgnoreCase ? query.ToLower() : query;
|
|
||||||
|
|
||||||
var sb = new StringBuilder(str.Length + (query.Length * (opt.Prefix.Length + opt.Suffix.Length)));
|
|
||||||
var patternIdx = 0;
|
|
||||||
var firstMatchIndex = -1;
|
|
||||||
var lastMatchIndex = 0;
|
|
||||||
char ch;
|
|
||||||
for (var idx = 0; idx < len; idx++)
|
|
||||||
{
|
|
||||||
ch = str[idx];
|
|
||||||
if (compareString[idx] == pattern[patternIdx])
|
|
||||||
{
|
|
||||||
if (firstMatchIndex < 0)
|
|
||||||
firstMatchIndex = idx;
|
|
||||||
lastMatchIndex = idx + 1;
|
|
||||||
|
|
||||||
sb.Append(opt.Prefix + ch + opt.Suffix);
|
|
||||||
patternIdx += 1;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
sb.Append(ch);
|
|
||||||
}
|
|
||||||
|
|
||||||
// match success, append remain char
|
|
||||||
if (patternIdx == pattern.Length && (idx + 1) != compareString.Length)
|
|
||||||
{
|
|
||||||
sb.Append(str.Substring(idx + 1));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// return rendered string if we have a match for every char
|
|
||||||
if (patternIdx == pattern.Length)
|
|
||||||
{
|
|
||||||
return new MatchResult
|
|
||||||
{
|
|
||||||
Success = true,
|
|
||||||
Value = sb.ToString(),
|
|
||||||
Score = CalScore(str, firstMatchIndex, lastMatchIndex - firstMatchIndex)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return new MatchResult { Success = false };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private int CalScore(string str, int firstIndex, int matchLen)
|
|
||||||
{
|
|
||||||
//a match found near the beginning of a string is scored more than a match found near the end
|
|
||||||
//a match is scored more if the characters in the patterns are closer to each other, while the score is lower if they are more spread out
|
|
||||||
var score = 100 * (query.Length + 1) / ((1 + firstIndex) + (matchLen + 1));
|
|
||||||
//a match with less characters assigning more weights
|
|
||||||
if (str.Length - query.Length < 5)
|
|
||||||
score = score + 20;
|
|
||||||
else if (str.Length - query.Length < 10)
|
|
||||||
score = score + 10;
|
|
||||||
|
|
||||||
return score;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MatchResult
|
|
||||||
{
|
|
||||||
public bool Success { get; set; }
|
|
||||||
public int Score { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// hightlight string
|
|
||||||
/// </summary>
|
|
||||||
public string Value { get; set; }
|
|
||||||
}
|
|
||||||
|
|
||||||
public class MatchOption
|
|
||||||
{
|
|
||||||
public MatchOption()
|
|
||||||
{
|
|
||||||
Prefix = "";
|
|
||||||
Suffix = "";
|
|
||||||
IgnoreCase = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// prefix of match char, use for hightlight
|
|
||||||
/// </summary>
|
|
||||||
public string Prefix { get; set; }
|
|
||||||
/// <summary>
|
|
||||||
/// suffix of match char, use for hightlight
|
|
||||||
/// </summary>
|
|
||||||
public string Suffix { get; set; }
|
|
||||||
|
|
||||||
public bool IgnoreCase { get; set; }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user