2019-12-10 03:57:59 +08:00
|
|
|
using System;
|
2019-12-03 22:02:59 +08:00
|
|
|
using System.Collections.Generic;
|
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;
|
2019-12-03 22:02:59 +08:00
|
|
|
using static Wox.Infrastructure.StringMatcher;
|
2016-04-23 06:29:38 +08:00
|
|
|
|
2019-12-30 07:13:33 +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-12-30 07:13:33 +08:00
|
|
|
public static int UserSettingSearchPrecision { get; set; }
|
|
|
|
|
2019-12-10 03:57:59 +08:00
|
|
|
public static bool ShouldUsePinyin { get; set; }
|
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")]
|
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-12-30 07:13:33 +08:00
|
|
|
|
2019-10-17 18:37:09 +08:00
|
|
|
query = query.Trim();
|
2019-09-29 12:24:38 +08:00
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
var fullStringToCompareWithoutCase = opt.IgnoreCase ? stringToCompare.ToLower() : stringToCompare;
|
|
|
|
|
|
|
|
var queryWithoutCase = opt.IgnoreCase ? query.ToLower() : query;
|
2020-01-02 05:02:23 +08:00
|
|
|
|
|
|
|
var separatedqueryStrings = queryWithoutCase.Split(' ');
|
|
|
|
int currentSeparatedQueryStringIndex = 0;
|
|
|
|
var currentSeparatedQueryString = separatedqueryStrings[currentSeparatedQueryStringIndex];
|
2019-09-29 12:24:38 +08:00
|
|
|
|
2020-01-02 05:02:23 +08:00
|
|
|
var queryIndex = 0;
|
2019-09-29 12:24:38 +08:00
|
|
|
var firstMatchIndex = -1;
|
2019-12-30 07:13:33 +08:00
|
|
|
var firstMatchIndexInWord = -1;
|
2019-09-29 12:24:38 +08:00
|
|
|
var lastMatchIndex = 0;
|
2019-12-30 07:13:33 +08:00
|
|
|
bool allMatched = false;
|
|
|
|
bool isFullWordMatched = false;
|
|
|
|
bool allWordsFullyMatched = true;
|
2019-12-03 22:02:59 +08:00
|
|
|
|
|
|
|
var indexList = new List<int>();
|
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
for (var index = 0; index < fullStringToCompareWithoutCase.Length; index++)
|
2019-09-29 12:24:38 +08:00
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
var ch = stringToCompare[index];
|
2020-01-02 05:02:23 +08:00
|
|
|
if (fullStringToCompareWithoutCase[index] == currentSeparatedQueryString[queryIndex])
|
2019-09-29 12:24:38 +08:00
|
|
|
{
|
|
|
|
if (firstMatchIndex < 0)
|
2019-12-30 07:13:33 +08:00
|
|
|
{ // first matched char will become the start of the compared string
|
|
|
|
firstMatchIndex = index;
|
|
|
|
}
|
|
|
|
|
2020-01-02 05:02:23 +08:00
|
|
|
if (queryIndex == 0)
|
2019-12-30 07:13:33 +08:00
|
|
|
{ // first letter of current word
|
|
|
|
isFullWordMatched = true;
|
|
|
|
firstMatchIndexInWord = index;
|
|
|
|
}
|
|
|
|
else if (!isFullWordMatched)
|
|
|
|
{ // we want to verify that there is not a better match if this is not a full word
|
|
|
|
// in order to do so we need to verify all previous chars are part of the pattern
|
2020-01-02 05:02:23 +08:00
|
|
|
int startIndexToVerify = index - queryIndex;
|
2019-12-30 07:13:33 +08:00
|
|
|
bool allMatch = true;
|
2020-01-02 05:02:23 +08:00
|
|
|
for (int indexToCheck = 0; indexToCheck < queryIndex; indexToCheck++)
|
2019-12-30 07:13:33 +08:00
|
|
|
{
|
|
|
|
if (fullStringToCompareWithoutCase[startIndexToVerify + indexToCheck] !=
|
2020-01-02 05:02:23 +08:00
|
|
|
currentSeparatedQueryString[indexToCheck])
|
2019-12-30 07:13:33 +08:00
|
|
|
{
|
|
|
|
allMatch = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allMatch)
|
|
|
|
{ // update to this as a full word
|
|
|
|
isFullWordMatched = true;
|
2020-01-02 05:02:23 +08:00
|
|
|
if (currentSeparatedQueryStringIndex == 0)
|
2019-12-30 07:13:33 +08:00
|
|
|
{ // first word so we need to update start index
|
|
|
|
firstMatchIndex = startIndexToVerify;
|
|
|
|
}
|
|
|
|
|
|
|
|
indexList.RemoveAll(x => x >= firstMatchIndexInWord);
|
2020-01-02 05:02:23 +08:00
|
|
|
for (int indexToCheck = 0; indexToCheck < queryIndex; indexToCheck++)
|
2019-12-30 07:13:33 +08:00
|
|
|
{ // update the index list
|
|
|
|
indexList.Add(startIndexToVerify + indexToCheck);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastMatchIndex = index + 1;
|
|
|
|
indexList.Add(index);
|
|
|
|
|
2020-01-02 05:02:23 +08:00
|
|
|
queryIndex++;
|
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
// increase the pattern matched index and check if everything was matched
|
2020-01-02 05:02:23 +08:00
|
|
|
if (queryIndex == currentSeparatedQueryString.Length)
|
2019-12-30 07:13:33 +08:00
|
|
|
{
|
2020-01-02 05:02:23 +08:00
|
|
|
currentSeparatedQueryStringIndex++;
|
|
|
|
|
|
|
|
if (currentSeparatedQueryStringIndex >= separatedqueryStrings.Length)
|
2019-12-30 07:13:33 +08:00
|
|
|
{ // moved over all the words
|
|
|
|
allMatched = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise move to the next word
|
2020-01-02 05:02:23 +08:00
|
|
|
currentSeparatedQueryString = separatedqueryStrings[currentSeparatedQueryStringIndex];
|
|
|
|
queryIndex = 0;
|
2019-12-30 07:13:33 +08:00
|
|
|
if (!isFullWordMatched)
|
|
|
|
{ // if any of the words was not fully matched all are not fully matched
|
|
|
|
allWordsFullyMatched = false;
|
|
|
|
}
|
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
isFullWordMatched = false;
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
|
|
|
|
// return rendered string if we have a match for every char or all substring without whitespaces matched
|
|
|
|
if (allMatched)
|
2019-09-29 12:24:38 +08:00
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
// check if all query string was contained in string to compare
|
|
|
|
bool containedFully = lastMatchIndex - firstMatchIndex == queryWithoutCase.Length;
|
|
|
|
var score = CalculateSearchScore(query, stringToCompare, firstMatchIndex, lastMatchIndex - firstMatchIndex, containedFully, allWordsFullyMatched);
|
2019-12-03 22:02:59 +08:00
|
|
|
var pinyinScore = ScoreForPinyin(stringToCompare, query);
|
|
|
|
|
|
|
|
var result = new MatchResult
|
2019-09-29 12:24:38 +08:00
|
|
|
{
|
|
|
|
Success = true,
|
2019-12-03 22:02:59 +08:00
|
|
|
MatchData = indexList,
|
2019-12-10 04:06:31 +08:00
|
|
|
RawScore = Math.Max(score, pinyinScore)
|
2019-09-29 12:24:38 +08:00
|
|
|
};
|
2019-12-03 22:02:59 +08:00
|
|
|
|
2019-12-12 02:10:11 +08:00
|
|
|
return result;
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new MatchResult { Success = false };
|
|
|
|
}
|
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
private static int CalculateSearchScore(string query, string stringToCompare, int firstIndex, int matchLen,
|
|
|
|
bool isFullyContained, bool allWordsFullyMatched)
|
2019-09-29 12:24:38 +08:00
|
|
|
{
|
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
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
if (isFullyContained)
|
|
|
|
{
|
|
|
|
score += 20; // honestly I'm not sure what would be a good number here or should it factor the size of the pattern
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allWordsFullyMatched)
|
|
|
|
{
|
|
|
|
score += 20;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2019-12-10 03:57:59 +08:00
|
|
|
if (!ShouldUsePinyin)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
|
|
|
{
|
|
|
|
if (Alphabet.ContainsChinese(source))
|
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
var combination = Alphabet.PinyinComination(source);
|
2019-09-29 12:24:38 +08:00
|
|
|
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-12-30 07:13:33 +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-12-30 07:13:33 +08:00
|
|
|
}
|
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
|
|
|
|
2019-12-10 04:06:31 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The final score of the match result with all search precision filters applied.
|
|
|
|
/// </summary>
|
|
|
|
public int Score { get; private set; }
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The raw calculated search score without any search precision filtering applied.
|
|
|
|
/// </summary>
|
|
|
|
private int _rawScore;
|
2019-12-30 07:13:33 +08:00
|
|
|
|
2019-12-10 04:06:31 +08:00
|
|
|
public int RawScore
|
2019-12-03 21:58:52 +08:00
|
|
|
{
|
2019-12-10 04:06:31 +08:00
|
|
|
get { return _rawScore; }
|
2019-12-03 21:58:52 +08:00
|
|
|
set
|
|
|
|
{
|
2019-12-10 04:06:31 +08:00
|
|
|
_rawScore = value;
|
|
|
|
Score = ApplySearchPrecisionFilter(_rawScore);
|
2019-12-03 21:58:52 +08:00
|
|
|
}
|
|
|
|
}
|
2019-12-03 22:02:59 +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)
|
|
|
|
{
|
2019-12-30 07:13:33 +08:00
|
|
|
return score >= UserSettingSearchPrecision;
|
2019-12-03 21:58:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private int ApplySearchPrecisionFilter(int score)
|
|
|
|
{
|
|
|
|
return IsSearchPrecisionScoreMet(score) ? score : 0;
|
|
|
|
}
|
2019-09-29 12:24:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public class MatchOption
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// prefix of match char, use for hightlight
|
|
|
|
/// </summary>
|
2019-12-30 07:13:33 +08:00
|
|
|
[Obsolete("this is never used")]
|
|
|
|
public string Prefix { get; set; } = "";
|
|
|
|
|
2019-09-29 12:24:38 +08:00
|
|
|
/// <summary>
|
|
|
|
/// suffix of match char, use for hightlight
|
|
|
|
/// </summary>
|
2019-12-30 07:13:33 +08:00
|
|
|
[Obsolete("this is never used")]
|
|
|
|
public string Suffix { get; set; } = "";
|
2019-09-29 12:24:38 +08:00
|
|
|
|
2019-12-30 07:13:33 +08:00
|
|
|
public bool IgnoreCase { get; set; } = true;
|
2015-02-07 16:53:33 +08:00
|
|
|
}
|
2019-12-30 07:13:33 +08:00
|
|
|
}
|