added empty match option

This commit is contained in:
Amir Tepper
2019-10-17 13:37:09 +03:00
parent 31916384c1
commit 0e241a0fc5
4 changed files with 21 additions and 18 deletions

View File

@@ -8,9 +8,9 @@ namespace Wox.Plugin.BrowserBookmark.Commands
{
internal static bool MatchProgram(Bookmark bookmark, string queryString)
{
if (StringMatcher.FuzzySearch(queryString, bookmark.Name, new MatchOption()).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName, new MatchOption()).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.Url, new MatchOption()).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.Name).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.Url).IsSearchPrecisionScoreMet()) return true;
return false;
}

View File

@@ -16,7 +16,7 @@ namespace Wox.Infrastructure
public static FuzzyMatcher Create(string query)
{
return new FuzzyMatcher(query, new MatchOption());
return new FuzzyMatcher(query, StringMatcher.EmptyMatchOption);
}
public static FuzzyMatcher Create(string query, MatchOption opt)

View File

@@ -8,6 +8,8 @@ namespace Wox.Infrastructure
{
public static class StringMatcher
{
public static MatchOption EmptyMatchOption = new MatchOption();
public static string UserSettingSearchPrecision { get; set; }
[Obsolete("This method is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")]
@@ -15,7 +17,7 @@ namespace Wox.Infrastructure
{
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
{
return FuzzySearch(target, source, new MatchOption()).Score;
return FuzzySearch(target, source, EmptyMatchOption).Score;
}
else
{
@@ -26,12 +28,12 @@ namespace Wox.Infrastructure
[Obsolete("This method is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")]
public static bool IsMatch(string source, string target)
{
return FuzzySearch(target, source, new MatchOption()).Score > 0;
return FuzzySearch(target, source, EmptyMatchOption).Score > 0;
}
public static MatchResult FuzzySearch(string query, string stringToCompare)
{
return FuzzySearch(query, stringToCompare, new MatchOption());
return FuzzySearch(query, stringToCompare, EmptyMatchOption);
}
/// <summary>
@@ -41,7 +43,7 @@ namespace Wox.Infrastructure
{
if (string.IsNullOrEmpty(stringToCompare) || string.IsNullOrEmpty(query)) return new MatchResult { Success = false };
query.Trim();
query = query.Trim();
var len = stringToCompare.Length;
var compareString = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare;
@@ -98,9 +100,9 @@ namespace Wox.Infrastructure
var score = 100 * (query.Length + 1) / ((1 + firstIndex) + (matchLen + 1));
//a match with less characters assigning more weights
if (stringToCompare.Length - query.Length < 5)
score = score + 20;
score += 20;
else if (stringToCompare.Length - query.Length < 10)
score = score + 10;
score += 10;
return score;
}
@@ -139,10 +141,10 @@ namespace Wox.Infrastructure
{
var combination = Alphabet.PinyinComination(source);
var pinyinScore = combination
.Select(pinyin => FuzzySearch(target, string.Join("", pinyin), new MatchOption()).Score)
.Select(pinyin => FuzzySearch(target, string.Join("", pinyin)).Score)
.Max();
var acronymScore = combination.Select(Alphabet.Acronym)
.Select(pinyin => FuzzySearch(target, pinyin, new MatchOption()).Score)
.Select(pinyin => FuzzySearch(target, pinyin).Score)
.Max();
var score = Math.Max(pinyinScore, acronymScore);
return score;
@@ -163,8 +165,9 @@ namespace Wox.Infrastructure
{
public bool Success { get; set; }
public int Score { get; set; }
/// <summary>
/// hightlight string
/// highlight string
/// </summary>
public string Value { get; set; }
}

View File

@@ -55,7 +55,7 @@ namespace Wox.Test
results.Add(new Result
{
Title = str,
Score = StringMatcher.FuzzySearch("inst", str, new MatchOption()).Score
Score = StringMatcher.FuzzySearch("inst", str).Score
});
}
@@ -72,7 +72,7 @@ namespace Wox.Test
{
var compareString = "Can have rum only in my glass";
var scoreResult = StringMatcher.FuzzySearch(searchString, compareString, new MatchOption()).Score;
var scoreResult = StringMatcher.FuzzySearch(searchString, compareString).Score;
Assert.True(scoreResult == 0);
}
@@ -92,7 +92,7 @@ namespace Wox.Test
results.Add(new Result
{
Title = str,
Score = StringMatcher.FuzzySearch(searchTerm, str, new MatchOption()).Score
Score = StringMatcher.FuzzySearch(searchTerm, str).Score
});
}
@@ -135,7 +135,7 @@ namespace Wox.Test
results.Add(new Result
{
Title = str,
Score = StringMatcher.FuzzySearch(searchTerm, str, new MatchOption()).Score
Score = StringMatcher.FuzzySearch(searchTerm, str).Score
});
}
@@ -173,7 +173,7 @@ namespace Wox.Test
{
var expectedPrecisionString = (StringMatcher.SearchPrecisionScore)expectedPrecisionScore;
StringMatcher.UserSettingSearchPrecision = expectedPrecisionString.ToString();
var matchResult = StringMatcher.FuzzySearch(queryString, compareString, new MatchOption());
var matchResult = StringMatcher.FuzzySearch(queryString, compareString);
Debug.WriteLine("");
Debug.WriteLine("###############################################");