@@ -27,7 +27,7 @@ public PinyinAlphabet()
2727 {
2828 switch ( e . PropertyName )
2929 {
30- case nameof ( Settings . ShouldUsePinyin ) :
30+ case nameof ( Settings . ShouldUsePinyin ) :
3131 if ( _settings . ShouldUsePinyin )
3232 {
3333 Reload ( ) ;
@@ -52,7 +52,7 @@ public void Reload()
5252
5353 private void CreateDoublePinyinTableFromStream ( Stream jsonStream )
5454 {
55- var table = JsonSerializer . Deserialize < Dictionary < string , Dictionary < string , string > > > ( jsonStream ) ??
55+ var table = JsonSerializer . Deserialize < Dictionary < string , Dictionary < string , string > > > ( jsonStream ) ??
5656 throw new InvalidOperationException ( "Failed to deserialize double pinyin table: result is null" ) ;
5757
5858 var schemaKey = _settings . DoublePinyinSchema . ToString ( ) ;
@@ -128,12 +128,12 @@ public bool ShouldTranslate(string stringToTranslate)
128128 if ( IsChineseCharacter ( content [ i ] ) )
129129 {
130130 var translated = _settings . UseDoublePinyin ? ToDoublePinyin ( resultList [ i ] ) : resultList [ i ] ;
131-
132- if ( i > 0 )
131+
132+ if ( i > 0 && content [ i - 1 ] != ' ' )
133133 {
134134 resultBuilder . Append ( ' ' ) ;
135135 }
136-
136+
137137 map . AddNewIndex ( resultBuilder . Length , translated . Length ) ;
138138 resultBuilder . Append ( translated ) ;
139139 previousIsChinese = true ;
@@ -144,19 +144,22 @@ public bool ShouldTranslate(string stringToTranslate)
144144 if ( previousIsChinese )
145145 {
146146 previousIsChinese = false ;
147- resultBuilder . Append ( ' ' ) ;
147+ if ( content [ i ] != ' ' )
148+ {
149+ resultBuilder . Append ( ' ' ) ;
150+ }
148151 }
149-
150- map . AddNewIndex ( resultBuilder . Length , resultList [ i ] . Length ) ;
151- resultBuilder . Append ( resultList [ i ] ) ;
152+
153+ map . AddNewIndex ( resultBuilder . Length , 1 ) ;
154+ resultBuilder . Append ( content [ i ] ) ;
152155 }
153156 }
154157
155158 map . EndConstruct ( ) ;
156159
157160 var translation = resultBuilder . ToString ( ) ;
158161 var result = ( translation , map ) ;
159-
162+
160163 return _pinyinCache [ content ] = result ;
161164 }
162165
@@ -185,8 +188,8 @@ private static bool IsChineseCharacter(char c)
185188
186189 private string ToDoublePinyin ( string fullPinyin )
187190 {
188- return currentDoublePinyinTable . TryGetValue ( fullPinyin , out var doublePinyinValue )
189- ? doublePinyinValue
191+ return currentDoublePinyinTable . TryGetValue ( fullPinyin , out var doublePinyinValue )
192+ ? doublePinyinValue
190193 : fullPinyin ;
191194 }
192195 }
0 commit comments