feat(StringMatcher): add Chinese PinYin support option

- Add ChinesePinYinSupport property to MatchOption class
- Implement Chinese PinYin support in StringMatcher class
- Implement Chinese PinYin support in FuzzyStringMatcher class
- Use the new option to conditionally enable Chinese PinYin matching

Signed-off-by: 舰队的偶像-岛风酱! <frg2089@outlook.com>
This commit is contained in:
舰队的偶像-岛风酱!
2025-10-23 17:36:59 +08:00
parent cd188aa281
commit e24a89d322
4 changed files with 23 additions and 5 deletions

View File

@@ -7,4 +7,9 @@ namespace Common.Search.FuzzSearch;
public class MatchOption
{
public bool IgnoreCase { get; set; } = true;
/// <summary>
/// Gets or sets a value indicating whether to support Chinese PinYin
/// </summary>
public bool ChinesePinYinSupport { get; set; }
}

View File

@@ -43,8 +43,7 @@ public class StringMatcher
List<string> queryList = [query];
List<string> stringToCompareList = [stringToCompare];
// TODO: Add switch for Chinese.
if (true)
if (opt.ChinesePinYinSupport)
{
// Remove IME composition split characters.
var input = query.Replace("'", string.Empty);

View File

@@ -44,7 +44,17 @@ public partial class SettingsModel : ObservableObject
public bool AllowExternalReload { get; set; }
public bool EnableChinesePinYinSupport { get; set; }
public bool EnableChinesePinYinSupport
{
get => field;
set
{
if (SetProperty(ref field, value))
{
FuzzyStringMatcher.ChinesePinYinSupport = value;
}
}
}
public Dictionary<string, ProviderSettings> ProviderSettings { get; set; } = [];

View File

@@ -11,6 +11,11 @@ public static class FuzzyStringMatcher
{
private const int NOMATCH = 0;
/// <summary>
/// Gets or sets a value indicating whether to support Chinese PinYin
/// </summary>
public static bool ChinesePinYinSupport { get; set; }
public static int ScoreFuzzy(string needle, string haystack, bool allowNonContiguousMatches = true)
{
var (s, _) = ScoreFuzzyWithPositions(needle, haystack, allowNonContiguousMatches);
@@ -25,8 +30,7 @@ public static class FuzzyStringMatcher
List<string> needles = [needle];
List<string> haystacks = [haystack];
// TODO: Add switch for Chinese.
if (true)
if (ChinesePinYinSupport)
{
// Remove IME composition split characters.
var input = needle.Replace("'", string.Empty);