66using hyjiacan . util . p4n . format ;
77using Wox . Infrastructure . Logger ;
88using Wox . Infrastructure . Storage ;
9+ using Wox . Infrastructure . UserSettings ;
910
1011namespace Wox . Infrastructure
1112{
@@ -14,9 +15,11 @@ public static class Alphabet
1415 private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat ( ) ;
1516 private static ConcurrentDictionary < string , string [ ] [ ] > PinyinCache ;
1617 private static BinaryStorage < ConcurrentDictionary < string , string [ ] [ ] > > _pinyinStorage ;
18+ private static Settings _settings ;
1719
18- public static void Initialize ( )
20+ public static void Initialize ( Settings settings )
1921 {
22+ _settings = settings ;
2023 Format . setToneType ( HanyuPinyinToneType . WITHOUT_TONE ) ;
2124
2225 Stopwatch . Normal ( "|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache" , ( ) =>
@@ -34,12 +37,20 @@ public static void Save()
3437 _pinyinStorage . Save ( PinyinCache ) ;
3538 }
3639
40+ private static string [ ] EmptyStringArray = new string [ 0 ] ;
41+ private static string [ ] [ ] Empty2DStringArray = new string [ 0 ] [ ] ;
42+
3743 /// <summary>
3844 /// replace chinese character with pinyin, non chinese character won't be modified
3945 /// <param name="word"> should be word or sentence, instead of single character. e.g. 微软 </param>
4046 /// </summary>
4147 public static string [ ] Pinyin ( string word )
4248 {
49+ if ( ! _settings . ShouldUsePinyin )
50+ {
51+ return EmptyStringArray ;
52+ }
53+
4354 var pinyin = word . Select ( c =>
4455 {
4556 var pinyins = PinyinHelper . toHanyuPinyinStringArray ( c ) ;
@@ -57,7 +68,7 @@ public static string[] Pinyin(string word)
5768 /// </summmary>
5869 public static string [ ] [ ] PinyinComination ( string characters )
5970 {
60- if ( ! string . IsNullOrEmpty ( characters ) )
71+ if ( _settings . ShouldUsePinyin && ! string . IsNullOrEmpty ( characters ) )
6172 {
6273 if ( ! PinyinCache . ContainsKey ( characters ) )
6374 {
@@ -89,7 +100,7 @@ public static string[][] PinyinComination(string characters)
89100 }
90101 else
91102 {
92- return new string [ ] [ ] { } ;
103+ return Empty2DStringArray ;
93104 }
94105 }
95106
@@ -101,13 +112,29 @@ public static string Acronym(string[] pinyin)
101112
102113 public static bool ContainsChinese ( string word )
103114 {
115+ if ( ! _settings . ShouldUsePinyin )
116+ {
117+ return false ;
118+ }
119+
120+ if ( word . Length > 40 )
121+ {
122+ Log . Debug ( $ "|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: { word } ") ;
123+ return false ;
124+ }
125+
104126 var chinese = word . Select ( PinyinHelper . toHanyuPinyinStringArray )
105127 . Any ( p => p != null ) ;
106128 return chinese ;
107129 }
108130
109131 private static string [ ] Combination ( string [ ] array1 , string [ ] array2 )
110132 {
133+ if ( ! _settings . ShouldUsePinyin )
134+ {
135+ return EmptyStringArray ;
136+ }
137+
111138 var combination = (
112139 from a1 in array1
113140 from a2 in array2
0 commit comments