2016-04-23 06:29:38 +08:00
|
|
|
|
using System;
|
2016-04-24 07:37:25 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Wox.Infrastructure;
|
|
|
|
|
using Wox.Infrastructure.Logger;
|
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
|
|
|
|
{
|
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))
|
|
|
|
|
{
|
|
|
|
|
FuzzyMatcher matcher = FuzzyMatcher.Create(target);
|
|
|
|
|
var score = matcher.Evaluate(source).Score;
|
|
|
|
|
return score;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-02-07 16:53:33 +08:00
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
|
public static int ScoreForPinyin(string source, string target)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
|
|
|
|
{
|
2018-12-19 11:41:41 +08:00
|
|
|
|
if(source.Length > 40)
|
|
|
|
|
{
|
|
|
|
|
Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {source}");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
|
if (Alphabet.ContainsChinese(source))
|
|
|
|
|
{
|
2018-12-19 11:41:41 +08:00
|
|
|
|
FuzzyMatcher matcher = FuzzyMatcher.Create(target);
|
2016-04-24 07:37:25 +08:00
|
|
|
|
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;
|
|
|
|
|
}
|
2015-02-07 16:53:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-04-24 07:37:25 +08:00
|
|
|
|
public static bool IsMatch(string source, string target)
|
2015-02-07 16:53:33 +08:00
|
|
|
|
{
|
2016-04-24 07:37:25 +08:00
|
|
|
|
return Score(source, target) > 0;
|
2015-02-07 16:53:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|