diff --git a/Wox.Infrastructure/Alphabet.cs b/Wox.Infrastructure/Alphabet.cs index 6c0dc1e6d9..896cda979f 100644 --- a/Wox.Infrastructure/Alphabet.cs +++ b/Wox.Infrastructure/Alphabet.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using hyjiacan.util.p4n; @@ -8,11 +9,12 @@ namespace Wox.Infrastructure { public static class Alphabet { - public static HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat(); + private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat(); + private static readonly ConcurrentDictionary PinyinCache = new ConcurrentDictionary(); static Alphabet() { - format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); + Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); } /// @@ -36,27 +38,42 @@ namespace Wox.Infrastructure /// e.g. 音乐 will return yinyue and yinle /// should be word or sentence, instead of single character. e.g. 微软 /// - public static IEnumerable PinyinComination(string characters) + public static string[][] PinyinComination(string characters) { - var allPinyins = new List(); - foreach (var c in characters) + if (!string.IsNullOrEmpty(characters)) { - var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, format); - if (pinyins != null) + if (!PinyinCache.ContainsKey(characters)) { - var r = pinyins.Distinct().ToArray(); - allPinyins.Add(r); + + var allPinyins = new List(); + 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 { - var r = new[] { c.ToString() }; - allPinyins.Add(r); + return PinyinCache[characters]; } } - - var comination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')); - - return comination; + else + { + return new string[][] { }; + } } public static string Acronym(string[] pinyin) diff --git a/Wox.Infrastructure/StringMatcher.cs b/Wox.Infrastructure/StringMatcher.cs index 78ddd9d4ec..9c793cb622 100644 --- a/Wox.Infrastructure/StringMatcher.cs +++ b/Wox.Infrastructure/StringMatcher.cs @@ -27,7 +27,6 @@ namespace Wox.Infrastructure 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);