2019-11-28 19:44:01 +08:00
|
|
|
using System;
|
2016-04-24 07:37:25 +08:00
|
|
|
using System.Linq;
|
2019-09-29 12:24:38 +08:00
|
|
|
using System.Text;
|
2016-04-24 07:37:25 +08:00
|
|
|
using Wox.Infrastructure.Logger;
|
2019-09-29 12:24:38 +08:00
|
|
|
using Wox.Infrastructure.UserSettings;
|
2016-04-23 06:29:38 +08:00
|
|
|
|
|
|
|
namespace Wox.Infrastructure
|
2015-02-07 16:53:33 +08:00
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
public static class StringMatcher
|
2015-02-07 16:53:33 +08:00
|
|
|
{
|
2019-10-20 20:45:06 +08:00
|
|
|
public static MatchOption DefaultMatchOption = new MatchOption();
|
2019-10-17 18:37:09 +08:00
|
|
|
|
2019-09-29 12:24:38 +08:00
|
|
|
public static string UserSettingSearchPrecision { get; set; }
|
|
|
|
|
|
|
|
[Obsolete("This method is obsolete and should not be used. Please use the static function StringMatcher.FuzzySearch")]
|
2016-04-24 07:37:25 +08:00
|
|
|
public static int Score(string source, string target)
|
2015-02-07 16:53:33 +08:00
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
|
|
|
{
|
2019-10-20 20:45:06 +08:00
|
|
|
return FuzzySearch(target, source, DefaultMatchOption).Score;
|
2016-04-24 07:37:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2015-02-07 16:53:33 +08:00
|
|
|
|
2019-09-29 12:24:38 +08:00
|
|
|
[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)
|
|
|
|
{
|
2019-10-20 20:45:06 +08:00
|
|
|
return FuzzySearch(target, source, DefaultMatchOption).Score > 0;
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
|
2019-09-29 13:03:30 +08:00
|
|
|
public static MatchResult FuzzySearch(string query, string stringToCompare)
|
2019-09-29 12:24:38 +08:00
|
|
|
{
|
2019-10-20 20:45:06 +08:00
|
|
|
return FuzzySearch(query, stringToCompare, DefaultMatchOption);
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// refer to https://github.com/mattyork/fuzzy
|
|
|
|
/// </summary>
|
|
|
|
public static MatchResult FuzzySearch(string query, string stringToCompare, MatchOption opt)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrEmpty(stringToCompare) || string.IsNullOrEmpty(query)) return new MatchResult { Success = false };
|
|
|
|
|
2019-10-17 18:37:09 +08:00
|
|
|
query = query.Trim();
|
2019-09-29 12:24:38 +08:00
|
|
|
|
|
|
|
var len = stringToCompare.Length;
|
|
|
|
var compareString = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare;
|
|
|
|
var pattern = opt.IgnoreCase ? query.ToLower() : query;
|
|
|
|
|
|
|
|
var sb = new StringBuilder(stringToCompare.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 = stringToCompare[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(stringToCompare.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(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return new MatchResult { Success = false };
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int CalScore(string query, string stringToCompare, int firstIndex, int matchLen)
|
|
|
|
{
|
2019-11-28 19:44:01 +08:00
|
|
|
// 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
|
2019-09-29 12:24:38 +08:00
|
|
|
var score = 100 * (query.Length + 1) / ((1 + firstIndex) + (matchLen + 1));
|
2019-11-28 19:44:01 +08:00
|
|
|
|
|
|
|
// A match with less characters assigning more weights
|
2019-09-29 12:24:38 +08:00
|
|
|
if (stringToCompare.Length - query.Length < 5)
|
2019-11-28 19:44:01 +08:00
|
|
|
{
|
2019-10-17 18:37:09 +08:00
|
|
|
score += 20;
|
2019-11-28 19:44:01 +08:00
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
else if (stringToCompare.Length - query.Length < 10)
|
2019-11-28 19:44:01 +08:00
|
|
|
{
|
2019-10-17 18:37:09 +08:00
|
|
|
score += 10;
|
2019-11-28 19:44:01 +08:00
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
|
2019-09-29 13:03:30 +08:00
|
|
|
public enum SearchPrecisionScore
|
|
|
|
{
|
|
|
|
Regular = 50,
|
|
|
|
Low = 20,
|
|
|
|
None = 0
|
|
|
|
}
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
public static int ScoreForPinyin(string source, string target)
|
|
|
|
{
|
|
|
|
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
|
|
|
{
|
|
|
|
if (Alphabet.ContainsChinese(source))
|
|
|
|
{
|
2019-09-29 12:24:38 +08:00
|
|
|
var combination = Alphabet.PinyinComination(source);
|
|
|
|
var pinyinScore = combination
|
2019-10-17 18:37:09 +08:00
|
|
|
.Select(pinyin => FuzzySearch(target, string.Join("", pinyin)).Score)
|
2016-04-24 07:37:25 +08:00
|
|
|
.Max();
|
2019-09-29 12:24:38 +08:00
|
|
|
var acronymScore = combination.Select(Alphabet.Acronym)
|
2019-10-17 18:37:09 +08:00
|
|
|
.Select(pinyin => FuzzySearch(target, pinyin).Score)
|
2016-04-24 07:37:25 +08:00
|
|
|
.Max();
|
|
|
|
var score = Math.Max(pinyinScore, acronymScore);
|
|
|
|
return score;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
}
|
2015-02-07 16:53:33 +08:00
|
|
|
|
2019-09-29 12:24:38 +08:00
|
|
|
public class MatchResult
|
|
|
|
{
|
|
|
|
public bool Success { get; set; }
|
2019-12-03 21:58:52 +08:00
|
|
|
|
|
|
|
private int _score;
|
|
|
|
public int Score
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _score;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
_score = ApplySearchPrecisionFilter(value);
|
|
|
|
}
|
|
|
|
}
|
2019-10-17 18:37:09 +08:00
|
|
|
|
2019-09-29 12:24:38 +08:00
|
|
|
/// <summary>
|
2019-12-03 21:58:52 +08:00
|
|
|
/// Matched data to highlight.
|
2019-09-29 12:24:38 +08:00
|
|
|
/// </summary>
|
2019-12-03 21:58:52 +08:00
|
|
|
public List<int> MatchData { get; set; }
|
|
|
|
|
|
|
|
public bool IsSearchPrecisionScoreMet()
|
|
|
|
{
|
|
|
|
return IsSearchPrecisionScoreMet(Score);
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsSearchPrecisionScoreMet(int score)
|
|
|
|
{
|
|
|
|
var precisionScore = (SearchPrecisionScore)Enum.Parse(
|
|
|
|
typeof(SearchPrecisionScore),
|
|
|
|
UserSettingSearchPrecision ?? SearchPrecisionScore.Regular.ToString());
|
|
|
|
return score >= (int)precisionScore;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int ApplySearchPrecisionFilter(int score)
|
|
|
|
{
|
|
|
|
return IsSearchPrecisionScoreMet(score) ? score : 0;
|
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class MatchOption
|
|
|
|
{
|
|
|
|
public MatchOption()
|
2015-02-07 16:53:33 +08:00
|
|
|
{
|
2019-09-29 12:24:38 +08:00
|
|
|
Prefix = "";
|
|
|
|
Suffix = "";
|
|
|
|
IgnoreCase = true;
|
2015-02-07 16:53:33 +08:00
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
|
|
|
|
/// <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; }
|
2015-02-07 16:53:33 +08:00
|
|
|
}
|
|
|
|
}
|