3
3
using System . Collections . Generic ;
4
4
using System . Linq ;
5
5
using System . Text ;
6
- using hyjiacan . util . p4n ;
7
- using hyjiacan . util . p4n . format ;
8
6
using JetBrains . Annotations ;
9
7
using Flow . Launcher . Infrastructure . Logger ;
10
8
using Flow . Launcher . Infrastructure . Storage ;
11
9
using Flow . Launcher . Infrastructure . UserSettings ;
10
+ using ToolGood . Words . Pinyin ;
11
+ using System . Threading . Tasks ;
12
12
13
13
namespace Flow . Launcher . Infrastructure
14
14
{
@@ -19,160 +19,44 @@ public interface IAlphabet
19
19
20
20
public class Alphabet : IAlphabet
21
21
{
22
- private readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat ( ) ;
23
- private ConcurrentDictionary < string , string [ ] [ ] > PinyinCache ;
24
- private BinaryStorage < Dictionary < string , string [ ] [ ] > > _pinyinStorage ;
22
+ private ConcurrentDictionary < string , string > _pinyinCache ;
25
23
private Settings _settings ;
26
-
24
+
27
25
public void Initialize ( [ NotNull ] Settings settings )
28
26
{
29
27
_settings = settings ?? throw new ArgumentNullException ( nameof ( settings ) ) ;
30
- InitializePinyinHelpers ( ) ;
31
- }
32
-
33
- private void InitializePinyinHelpers ( )
34
- {
35
- Format . setToneType ( HanyuPinyinToneType . WITHOUT_TONE ) ;
36
-
37
- Stopwatch . Normal ( "|Flow Launcher.Infrastructure.Alphabet.Initialize|Preload pinyin cache" , ( ) =>
38
- {
39
- _pinyinStorage = new BinaryStorage < Dictionary < string , string [ ] [ ] > > ( "Pinyin" ) ;
40
-
41
- lock ( _pinyinStorage )
42
- {
43
- var loaded = _pinyinStorage . TryLoad ( new Dictionary < string , string [ ] [ ] > ( ) ) ;
44
-
45
- PinyinCache = new ConcurrentDictionary < string , string [ ] [ ] > ( loaded ) ;
46
- }
47
-
48
- // force pinyin library static constructor initialize
49
- PinyinHelper . toHanyuPinyinStringArray ( 'T' , Format ) ;
50
- } ) ;
51
- Log . Info ( $ "|Flow Launcher.Infrastructure.Alphabet.Initialize|Number of preload pinyin combination<{ PinyinCache . Count } >") ;
52
- }
53
-
54
- public string Translate ( string str )
55
- {
56
- return ConvertChineseCharactersToPinyin ( str ) ;
57
- }
58
-
59
- public string ConvertChineseCharactersToPinyin ( string source )
60
- {
61
- if ( ! _settings . ShouldUsePinyin )
62
- return source ;
63
-
64
- if ( string . IsNullOrEmpty ( source ) )
65
- return source ;
66
-
67
- if ( ! ContainsChinese ( source ) )
68
- return source ;
69
-
70
- var combination = PinyinCombination ( source ) ;
71
-
72
- var pinyinArray = combination . Select ( x => string . Join ( "" , x ) ) ;
73
- var acronymArray = combination . Select ( Acronym ) . Distinct ( ) ;
74
-
75
- var joinedSingleStringCombination = new StringBuilder ( ) ;
76
- var all = acronymArray . Concat ( pinyinArray ) ;
77
- all . ToList ( ) . ForEach ( x => joinedSingleStringCombination . Append ( x ) ) ;
78
-
79
- return joinedSingleStringCombination . ToString ( ) ;
80
- }
81
-
82
- public void Save ( )
83
- {
84
- if ( ! _settings . ShouldUsePinyin )
85
- {
86
- return ;
87
- }
88
-
89
- lock ( _pinyinStorage )
90
- {
91
- _pinyinStorage . Save ( PinyinCache . ToDictionary ( i => i . Key , i => i . Value ) ) ;
92
- }
93
28
}
94
29
95
- private static string [ ] EmptyStringArray = new string [ 0 ] ;
96
- private static string [ ] [ ] Empty2DStringArray = new string [ 0 ] [ ] ;
97
30
98
- /// <summmary>
99
- /// replace chinese character with pinyin, non chinese character won't be modified
100
- /// Because we don't have words dictionary, so we can only return all possiblie pinyin combination
101
- /// e.g. 音乐 will return yinyue and yinle
102
- /// <param name="characters"> should be word or sentence, instead of single character. e.g. 微软 </param>
103
- /// </summmary>
104
- public string [ ] [ ] PinyinCombination ( string characters )
31
+ public string Translate ( string content )
105
32
{
106
- if ( ! _settings . ShouldUsePinyin || string . IsNullOrEmpty ( characters ) )
107
- {
108
- return Empty2DStringArray ;
109
- }
110
-
111
- if ( ! PinyinCache . ContainsKey ( characters ) )
33
+ if ( _settings . ShouldUsePinyin )
112
34
{
113
- var allPinyins = new List < string [ ] > ( ) ;
114
- foreach ( var c in characters )
35
+ string result = _pinyinCache . GetValueOrDefault ( content ) ;
36
+ if ( result == null )
115
37
{
116
- var pinyins = PinyinHelper . toHanyuPinyinStringArray ( c , Format ) ;
117
- if ( pinyins != null )
38
+ if ( WordsHelper . HasChinese ( content ) )
118
39
{
119
- var r = pinyins . Distinct ( ) . ToArray ( ) ;
120
- allPinyins . Add ( r ) ;
40
+ result = WordsHelper . GetPinyin ( content , ";" ) ;
41
+ result = GetFirstPinyinChar ( result ) + result . Replace ( ";" , "" ) ;
42
+ _pinyinCache [ content ] = result ;
121
43
}
122
44
else
123
45
{
124
- var r = new [ ] { c . ToString ( ) } ;
125
- allPinyins . Add ( r ) ;
46
+ result = content ;
126
47
}
127
48
}
128
-
129
- var combination = allPinyins . Aggregate ( Combination ) . Select ( c => c . Split ( ';' ) ) . ToArray ( ) ;
130
- PinyinCache [ characters ] = combination ;
131
- return combination ;
49
+ return result ;
132
50
}
133
51
else
134
52
{
135
- return PinyinCache [ characters ] ;
53
+ return content ;
136
54
}
137
55
}
138
56
139
- public string Acronym ( string [ ] pinyin )
140
- {
141
- var acronym = string . Join ( "" , pinyin . Select ( p => p [ 0 ] ) ) ;
142
- return acronym ;
143
- }
144
-
145
- public bool ContainsChinese ( string word )
57
+ private string GetFirstPinyinChar ( string content )
146
58
{
147
- if ( ! _settings . ShouldUsePinyin )
148
- {
149
- return false ;
150
- }
151
-
152
- if ( word . Length > 40 )
153
- {
154
- //Skip strings that are too long string for Pinyin conversion.
155
- return false ;
156
- }
157
-
158
- var chinese = word . Select ( PinyinHelper . toHanyuPinyinStringArray )
159
- . Any ( p => p != null ) ;
160
- return chinese ;
161
- }
162
-
163
- private string [ ] Combination ( string [ ] array1 , string [ ] array2 )
164
- {
165
- if ( ! _settings . ShouldUsePinyin )
166
- {
167
- return EmptyStringArray ;
168
- }
169
-
170
- var combination = (
171
- from a1 in array1
172
- from a2 in array2
173
- select $ "{ a1 } ;{ a2 } "
174
- ) . ToArray ( ) ;
175
- return combination ;
59
+ return new string ( content . Split ( ";" ) . Select ( c => c . First ( ) ) . ToArray ( ) ) ;
176
60
}
177
61
}
178
- }
62
+ }
0 commit comments