mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-08 20:27:36 +02:00
Refactoring pinyin
1. use custom patched pinyin library Pinyin4Net 2. fix memory leak on startup: 360mb -> 160mb when using vs15 debugger
This commit is contained in:
@@ -1,31 +1,58 @@
|
||||
using System;
|
||||
using JetBrains.Annotations;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.Logger;
|
||||
|
||||
namespace Wox.Infrastructure
|
||||
{
|
||||
public class StringMatcher
|
||||
public static class StringMatcher
|
||||
{
|
||||
/// <summary>
|
||||
/// Check if a candidate is match with the source
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <param name="candidate"></param>
|
||||
/// <returns>Match score</returns>
|
||||
public static int Match(string source, string candidate)
|
||||
public static int Score(string source, string target)
|
||||
{
|
||||
if (string.IsNullOrEmpty(source) || string.IsNullOrEmpty(candidate)) return 0;
|
||||
|
||||
FuzzyMatcher matcher = FuzzyMatcher.Create(candidate);
|
||||
int score = matcher.Evaluate(source).Score;
|
||||
if (score > 0) return score;
|
||||
|
||||
score = matcher.Evaluate(source.Unidecode()).Score;
|
||||
return score;
|
||||
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
||||
{
|
||||
FuzzyMatcher matcher = FuzzyMatcher.Create(target);
|
||||
var score = matcher.Evaluate(source).Score;
|
||||
return score;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsMatch(string source, string candidate)
|
||||
public static int ScoreForPinyin(string source, string target)
|
||||
{
|
||||
return Match(source, candidate) > 0;
|
||||
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
||||
{
|
||||
FuzzyMatcher matcher = FuzzyMatcher.Create(target);
|
||||
//todo happlebao currently generate pinyin on every query, should be generate on startup/index
|
||||
if (Alphabet.ContainsChinese(source))
|
||||
{
|
||||
var combination = Alphabet.PinyinComination(source);
|
||||
var pinyinScore = combination.Select(pinyin => matcher.Evaluate(string.Join("", pinyin)).Score)
|
||||
.Max();
|
||||
var acronymScore = combination.Select(Alphabet.Acronym)
|
||||
.Select(pinyin => matcher.Evaluate(pinyin).Score)
|
||||
.Max();
|
||||
var score = Math.Max(pinyinScore, acronymScore);
|
||||
return score;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool IsMatch(string source, string target)
|
||||
{
|
||||
return Score(source, target) > 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user