Skip to content

Commit 3454dc6

Browse files
bao-qiantaooceros
authored andcommitted
Change Pinyin Library
1 parent a1327c1 commit 3454dc6

File tree

1 file changed

+18
-134
lines changed

1 file changed

+18
-134
lines changed

Flow.Launcher.Infrastructure/Alphabet.cs

Lines changed: 18 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Text;
6-
using hyjiacan.util.p4n;
7-
using hyjiacan.util.p4n.format;
86
using JetBrains.Annotations;
97
using Flow.Launcher.Infrastructure.Logger;
108
using Flow.Launcher.Infrastructure.Storage;
119
using Flow.Launcher.Infrastructure.UserSettings;
10+
using ToolGood.Words.Pinyin;
11+
using System.Threading.Tasks;
1212

1313
namespace Flow.Launcher.Infrastructure
1414
{
@@ -19,160 +19,44 @@ public interface IAlphabet
1919

2020
public class Alphabet : IAlphabet
2121
{
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;
2523
private Settings _settings;
26-
24+
2725
public void Initialize([NotNull] Settings settings)
2826
{
2927
_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-
}
9328
}
9429

95-
private static string[] EmptyStringArray = new string[0];
96-
private static string[][] Empty2DStringArray = new string[0][];
9730

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)
10532
{
106-
if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters))
107-
{
108-
return Empty2DStringArray;
109-
}
110-
111-
if (!PinyinCache.ContainsKey(characters))
33+
if (_settings.ShouldUsePinyin)
11234
{
113-
var allPinyins = new List<string[]>();
114-
foreach (var c in characters)
35+
string result = _pinyinCache.GetValueOrDefault(content);
36+
if (result == null)
11537
{
116-
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
117-
if (pinyins != null)
38+
if (WordsHelper.HasChinese(content))
11839
{
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;
12143
}
12244
else
12345
{
124-
var r = new[] { c.ToString() };
125-
allPinyins.Add(r);
46+
result = content;
12647
}
12748
}
128-
129-
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
130-
PinyinCache[characters] = combination;
131-
return combination;
49+
return result;
13250
}
13351
else
13452
{
135-
return PinyinCache[characters];
53+
return content;
13654
}
13755
}
13856

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)
14658
{
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());
17660
}
17761
}
178-
}
62+
}

0 commit comments

Comments
 (0)