mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
Add cache for pinyin query #858
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Concurrent;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using hyjiacan.util.p4n;
|
using hyjiacan.util.p4n;
|
||||||
@@ -8,11 +9,12 @@ namespace Wox.Infrastructure
|
|||||||
{
|
{
|
||||||
public static class Alphabet
|
public static class Alphabet
|
||||||
{
|
{
|
||||||
public static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
|
private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
|
||||||
|
private static readonly ConcurrentDictionary<string, string[][]> PinyinCache = new ConcurrentDictionary<string, string[][]>();
|
||||||
|
|
||||||
static Alphabet()
|
static Alphabet()
|
||||||
{
|
{
|
||||||
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -36,27 +38,42 @@ namespace Wox.Infrastructure
|
|||||||
/// e.g. 音乐 will return yinyue and yinle
|
/// e.g. 音乐 will return yinyue and yinle
|
||||||
/// <param name="characters"> should be word or sentence, instead of single character. e.g. 微软 </param>
|
/// <param name="characters"> should be word or sentence, instead of single character. e.g. 微软 </param>
|
||||||
/// </summmary>
|
/// </summmary>
|
||||||
public static IEnumerable<string[]> PinyinComination(string characters)
|
public static string[][] PinyinComination(string characters)
|
||||||
{
|
{
|
||||||
var allPinyins = new List<string[]>();
|
if (!string.IsNullOrEmpty(characters))
|
||||||
foreach (var c in characters)
|
|
||||||
{
|
{
|
||||||
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, format);
|
if (!PinyinCache.ContainsKey(characters))
|
||||||
if (pinyins != null)
|
|
||||||
{
|
{
|
||||||
var r = pinyins.Distinct().ToArray();
|
|
||||||
allPinyins.Add(r);
|
var allPinyins = new List<string[]>();
|
||||||
|
foreach (var c in characters)
|
||||||
|
{
|
||||||
|
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
|
||||||
|
if (pinyins != null)
|
||||||
|
{
|
||||||
|
var r = pinyins.Distinct().ToArray();
|
||||||
|
allPinyins.Add(r);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var r = new[] { c.ToString() };
|
||||||
|
allPinyins.Add(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
|
||||||
|
PinyinCache[characters] = combination;
|
||||||
|
return combination;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var r = new[] { c.ToString() };
|
return PinyinCache[characters];
|
||||||
allPinyins.Add(r);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else
|
||||||
var comination = allPinyins.Aggregate(Combination).Select(c => c.Split(';'));
|
{
|
||||||
|
return new string[][] { };
|
||||||
return comination;
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string Acronym(string[] pinyin)
|
public static string Acronym(string[] pinyin)
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ namespace Wox.Infrastructure
|
|||||||
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
|
||||||
{
|
{
|
||||||
FuzzyMatcher matcher = FuzzyMatcher.Create(target);
|
FuzzyMatcher matcher = FuzzyMatcher.Create(target);
|
||||||
//todo happlebao currently generate pinyin on every query, should be generate on startup/index
|
|
||||||
if (Alphabet.ContainsChinese(source))
|
if (Alphabet.ContainsChinese(source))
|
||||||
{
|
{
|
||||||
var combination = Alphabet.PinyinComination(source);
|
var combination = Alphabet.PinyinComination(source);
|
||||||
|
|||||||
Reference in New Issue
Block a user