Skip to content

Commit 9c69f22

Browse files
authored
Merge pull request #77 from theClueless/pinyinInfraSettings
Pinyin infra settings
2 parents 85ad6b1 + 9d98d26 commit 9c69f22

File tree

6 files changed

+41
-10
lines changed

6 files changed

+41
-10
lines changed

Wox.Infrastructure/Alphabet.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using hyjiacan.util.p4n.format;
77
using Wox.Infrastructure.Logger;
88
using Wox.Infrastructure.Storage;
9+
using Wox.Infrastructure.UserSettings;
910

1011
namespace 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

Wox.Infrastructure/StringMatcher.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,6 @@ public static int ScoreForPinyin(string source, string target)
131131
{
132132
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
133133
{
134-
if(source.Length > 40)
135-
{
136-
Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {source}");
137-
return 0;
138-
}
139-
140134
if (Alphabet.ContainsChinese(source))
141135
{
142136
var combination = Alphabet.PinyinComination(source);

Wox.Infrastructure/UserSettings/Settings.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class Settings : BaseModel
2121
public string ResultFontWeight { get; set; }
2222
public string ResultFontStretch { get; set; }
2323

24+
/// <summary>
25+
/// when false Alphabet static service will always return empty results
26+
/// </summary>
27+
public bool ShouldUsePinyin { get; set; } = true;
28+
2429
private string _querySearchPrecision { get; set; } = StringMatcher.SearchPrecisionScore.Regular.ToString();
2530
public string QuerySearchPrecision
2631
{

Wox/App.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ private void OnStartup(object sender, StartupEventArgs e)
4949
RegisterDispatcherUnhandledException();
5050

5151
ImageLoader.Initialize();
52-
Alphabet.Initialize();
5352

5453
_settingsVM = new SettingWindowViewModel();
5554
_settings = _settingsVM.Settings;
5655

56+
Alphabet.Initialize(_settings);
57+
5758
StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;
5859

5960
PluginManager.LoadPlugins(_settings.PluginSettings);

Wox/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<system:String x:Key="hideOnStartup">Hide Wox on startup</system:String>
3535
<system:String x:Key="hideNotifyIcon">Hide tray icon</system:String>
3636
<system:String x:Key="querySearchPrecision">Query Search Precision</system:String>
37+
<system:String x:Key="ShouldUsePinyin">Should Use Pinyin</system:String>
3738

3839
<!--Setting Plugin-->
3940
<system:String x:Key="plugin">Plugin</system:String>

Wox/SettingWindow.xaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@
5555
Checked="OnAutoStartupChecked" Unchecked="OnAutoStartupUncheck">
5656
<TextBlock Text="{DynamicResource autoUpdates}" />
5757
</CheckBox>
58+
<CheckBox Margin="10" IsChecked="{Binding Settings.ShouldUsePinyin}">
59+
<TextBlock Text="{DynamicResource ShouldUsePinyin}" />
60+
</CheckBox>
5861
<StackPanel Margin="10" Orientation="Horizontal">
5962
<TextBlock Text="{DynamicResource querySearchPrecision}" />
6063
<ComboBox Margin="10 0 0 0" Width="120"

0 commit comments

Comments
 (0)