mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Only pass needed setting to Alphabet
As Alphabet class is static, its methods could be used without ever calling Alphabet.initialize(_settings) beforehand which would end in an exception. Therefor only _shouldUsePinyin settings needed will be transferred with a given default value.
This commit is contained in:
parent
05f66f9bea
commit
3d55ad783e
2 changed files with 44 additions and 34 deletions
|
|
@ -15,17 +15,26 @@ namespace Wox.Infrastructure
|
||||||
private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
|
private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
|
||||||
private static ConcurrentDictionary<string, string[][]> PinyinCache;
|
private static ConcurrentDictionary<string, string[][]> PinyinCache;
|
||||||
private static BinaryStorage<ConcurrentDictionary<string, string[][]>> _pinyinStorage;
|
private static BinaryStorage<ConcurrentDictionary<string, string[][]>> _pinyinStorage;
|
||||||
private static Settings _settings;
|
private static bool _shouldUsePinyin = true;
|
||||||
|
|
||||||
public static void Initialize(Settings settings)
|
public static void Initialize(bool shouldUsePinyin = true)
|
||||||
|
{
|
||||||
|
_shouldUsePinyin = shouldUsePinyin;
|
||||||
|
if (_shouldUsePinyin)
|
||||||
|
{
|
||||||
|
InitializePinyinHelpers();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void InitializePinyinHelpers()
|
||||||
{
|
{
|
||||||
_settings = settings;
|
|
||||||
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
|
||||||
|
|
||||||
Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
|
Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
|
||||||
{
|
{
|
||||||
_pinyinStorage = new BinaryStorage<ConcurrentDictionary<string, string[][]>>("Pinyin");
|
_pinyinStorage = new BinaryStorage<ConcurrentDictionary<string, string[][]>>("Pinyin");
|
||||||
PinyinCache = _pinyinStorage.TryLoad(new ConcurrentDictionary<string, string[][]>());
|
PinyinCache = _pinyinStorage.TryLoad(new ConcurrentDictionary<string, string[][]>());
|
||||||
|
|
||||||
// force pinyin library static constructor initialize
|
// force pinyin library static constructor initialize
|
||||||
PinyinHelper.toHanyuPinyinStringArray('T', Format);
|
PinyinHelper.toHanyuPinyinStringArray('T', Format);
|
||||||
});
|
});
|
||||||
|
|
@ -34,6 +43,10 @@ namespace Wox.Infrastructure
|
||||||
|
|
||||||
public static void Save()
|
public static void Save()
|
||||||
{
|
{
|
||||||
|
if (!_shouldUsePinyin)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
_pinyinStorage.Save(PinyinCache);
|
_pinyinStorage.Save(PinyinCache);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -46,7 +59,7 @@ namespace Wox.Infrastructure
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public static string[] Pinyin(string word)
|
public static string[] Pinyin(string word)
|
||||||
{
|
{
|
||||||
if (!_settings.ShouldUsePinyin)
|
if (!_shouldUsePinyin)
|
||||||
{
|
{
|
||||||
return EmptyStringArray;
|
return EmptyStringArray;
|
||||||
}
|
}
|
||||||
|
|
@ -68,39 +81,36 @@ namespace Wox.Infrastructure
|
||||||
/// </summmary>
|
/// </summmary>
|
||||||
public static string[][] PinyinComination(string characters)
|
public static string[][] PinyinComination(string characters)
|
||||||
{
|
{
|
||||||
if (_settings.ShouldUsePinyin && !string.IsNullOrEmpty(characters))
|
if (!_shouldUsePinyin || string.IsNullOrEmpty(characters))
|
||||||
{
|
{
|
||||||
if (!PinyinCache.ContainsKey(characters))
|
return Empty2DStringArray;
|
||||||
{
|
}
|
||||||
|
|
||||||
var allPinyins = new List<string[]>();
|
if (!PinyinCache.ContainsKey(characters))
|
||||||
foreach (var c in characters)
|
{
|
||||||
|
var allPinyins = new List<string[]>();
|
||||||
|
foreach (var c in characters)
|
||||||
|
{
|
||||||
|
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
|
||||||
|
if (pinyins != null)
|
||||||
{
|
{
|
||||||
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
|
var r = pinyins.Distinct().ToArray();
|
||||||
if (pinyins != null)
|
allPinyins.Add(r);
|
||||||
{
|
|
||||||
var r = pinyins.Distinct().ToArray();
|
|
||||||
allPinyins.Add(r);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
var r = new[] { c.ToString() };
|
|
||||||
allPinyins.Add(r);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
var r = new[] { c.ToString() };
|
||||||
|
allPinyins.Add(r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
|
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
|
||||||
PinyinCache[characters] = combination;
|
PinyinCache[characters] = combination;
|
||||||
return combination;
|
return combination;
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return PinyinCache[characters];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return Empty2DStringArray;
|
return PinyinCache[characters];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -112,7 +122,7 @@ namespace Wox.Infrastructure
|
||||||
|
|
||||||
public static bool ContainsChinese(string word)
|
public static bool ContainsChinese(string word)
|
||||||
{
|
{
|
||||||
if (!_settings.ShouldUsePinyin)
|
if (!_shouldUsePinyin)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -130,7 +140,7 @@ namespace Wox.Infrastructure
|
||||||
|
|
||||||
private static string[] Combination(string[] array1, string[] array2)
|
private static string[] Combination(string[] array1, string[] array2)
|
||||||
{
|
{
|
||||||
if (!_settings.ShouldUsePinyin)
|
if (!_shouldUsePinyin)
|
||||||
{
|
{
|
||||||
return EmptyStringArray;
|
return EmptyStringArray;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using System.Timers;
|
using System.Timers;
|
||||||
|
|
@ -53,7 +53,7 @@ namespace Wox
|
||||||
_settingsVM = new SettingWindowViewModel();
|
_settingsVM = new SettingWindowViewModel();
|
||||||
_settings = _settingsVM.Settings;
|
_settings = _settingsVM.Settings;
|
||||||
|
|
||||||
Alphabet.Initialize(_settings);
|
Alphabet.Initialize(_settings.ShouldUsePinyin);
|
||||||
|
|
||||||
StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue