Skip to content

Commit

Permalink
Merge pull request microsoft#77 from theClueless/pinyinInfraSettings
Browse files Browse the repository at this point in the history
Pinyin infra settings
  • Loading branch information
jjw24 authored Nov 17, 2019
2 parents 85ad6b1 + 9d98d26 commit 9c69f22
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
33 changes: 30 additions & 3 deletions Wox.Infrastructure/Alphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using hyjiacan.util.p4n.format;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
using Wox.Infrastructure.UserSettings;

namespace Wox.Infrastructure
{
Expand All @@ -14,9 +15,11 @@ public static class Alphabet
private static readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
private static ConcurrentDictionary<string, string[][]> PinyinCache;
private static BinaryStorage<ConcurrentDictionary<string, string[][]>> _pinyinStorage;
private static Settings _settings;

public static void Initialize()
public static void Initialize(Settings settings)
{
_settings = settings;
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

Stopwatch.Normal("|Wox.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
Expand All @@ -34,12 +37,20 @@ public static void Save()
_pinyinStorage.Save(PinyinCache);
}

private static string[] EmptyStringArray = new string[0];
private static string[][] Empty2DStringArray = new string[0][];

/// <summary>
/// replace chinese character with pinyin, non chinese character won't be modified
/// <param name="word"> should be word or sentence, instead of single character. e.g. 微软 </param>
/// </summary>
public static string[] Pinyin(string word)
{
if (!_settings.ShouldUsePinyin)
{
return EmptyStringArray;
}

var pinyin = word.Select(c =>
{
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c);
Expand All @@ -57,7 +68,7 @@ public static string[] Pinyin(string word)
/// </summmary>
public static string[][] PinyinComination(string characters)
{
if (!string.IsNullOrEmpty(characters))
if (_settings.ShouldUsePinyin && !string.IsNullOrEmpty(characters))
{
if (!PinyinCache.ContainsKey(characters))
{
Expand Down Expand Up @@ -89,7 +100,7 @@ public static string[][] PinyinComination(string characters)
}
else
{
return new string[][] { };
return Empty2DStringArray;
}
}

Expand All @@ -101,13 +112,29 @@ public static string Acronym(string[] pinyin)

public static bool ContainsChinese(string word)
{
if (!_settings.ShouldUsePinyin)
{
return false;
}

if (word.Length > 40)
{
Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {word}");
return false;
}

var chinese = word.Select(PinyinHelper.toHanyuPinyinStringArray)
.Any(p => p != null);
return chinese;
}

private static string[] Combination(string[] array1, string[] array2)
{
if (!_settings.ShouldUsePinyin)
{
return EmptyStringArray;
}

var combination = (
from a1 in array1
from a2 in array2
Expand Down
6 changes: 0 additions & 6 deletions Wox.Infrastructure/StringMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,6 @@ public static int ScoreForPinyin(string source, string target)
{
if (!string.IsNullOrEmpty(source) && !string.IsNullOrEmpty(target))
{
if(source.Length > 40)
{
Log.Debug($"|Wox.Infrastructure.StringMatcher.ScoreForPinyin|skip too long string: {source}");
return 0;
}

if (Alphabet.ContainsChinese(source))
{
var combination = Alphabet.PinyinComination(source);
Expand Down
5 changes: 5 additions & 0 deletions Wox.Infrastructure/UserSettings/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class Settings : BaseModel
public string ResultFontWeight { get; set; }
public string ResultFontStretch { get; set; }

/// <summary>
/// when false Alphabet static service will always return empty results
/// </summary>
public bool ShouldUsePinyin { get; set; } = true;

private string _querySearchPrecision { get; set; } = StringMatcher.SearchPrecisionScore.Regular.ToString();
public string QuerySearchPrecision
{
Expand Down
3 changes: 2 additions & 1 deletion Wox/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ private void OnStartup(object sender, StartupEventArgs e)
RegisterDispatcherUnhandledException();

ImageLoader.Initialize();
Alphabet.Initialize();

_settingsVM = new SettingWindowViewModel();
_settings = _settingsVM.Settings;

Alphabet.Initialize(_settings);

StringMatcher.UserSettingSearchPrecision = _settings.QuerySearchPrecision;

PluginManager.LoadPlugins(_settings.PluginSettings);
Expand Down
1 change: 1 addition & 0 deletions Wox/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<system:String x:Key="hideOnStartup">Hide Wox on startup</system:String>
<system:String x:Key="hideNotifyIcon">Hide tray icon</system:String>
<system:String x:Key="querySearchPrecision">Query Search Precision</system:String>
<system:String x:Key="ShouldUsePinyin">Should Use Pinyin</system:String>

<!--Setting Plugin-->
<system:String x:Key="plugin">Plugin</system:String>
Expand Down
3 changes: 3 additions & 0 deletions Wox/SettingWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
Checked="OnAutoStartupChecked" Unchecked="OnAutoStartupUncheck">
<TextBlock Text="{DynamicResource autoUpdates}" />
</CheckBox>
<CheckBox Margin="10" IsChecked="{Binding Settings.ShouldUsePinyin}">
<TextBlock Text="{DynamicResource ShouldUsePinyin}" />
</CheckBox>
<StackPanel Margin="10" Orientation="Horizontal">
<TextBlock Text="{DynamicResource querySearchPrecision}" />
<ComboBox Margin="10 0 0 0" Width="120"
Expand Down

0 comments on commit 9c69f22

Please sign in to comment.