From b09dc9372bc299db0301b592cee4e79e055c5f2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 15 Oct 2020 21:05:44 +0800 Subject: [PATCH 01/11] change reference --- .../Flow.Launcher.Infrastructure.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj b/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj index 410d11536de..d834bd2d36e 100644 --- a/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj +++ b/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj @@ -53,10 +53,10 @@ - + From ce613ba446db08cb76498eff44a3be6db8eeef7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 15 Oct 2020 21:06:01 +0800 Subject: [PATCH 02/11] Seems that we don't need storage cache --- Flow.Launcher/PublicAPIInstance.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 23f5d85b704..8520f7ba050 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -76,7 +76,6 @@ public void SaveAppAllSettings() _settingsVM.Save(); PluginManager.Save(); ImageLoader.Save(); - _alphabet.Save(); } public void ReloadAllPluginData() From eefe210ecaffde42edf30e78b20e735f90420402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 15 Oct 2020 21:06:23 +0800 Subject: [PATCH 03/11] remove unnecessery query translate --- Flow.Launcher.Infrastructure/StringMatcher.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/StringMatcher.cs b/Flow.Launcher.Infrastructure/StringMatcher.cs index 2a4270fb4b2..d390e48a2f1 100644 --- a/Flow.Launcher.Infrastructure/StringMatcher.cs +++ b/Flow.Launcher.Infrastructure/StringMatcher.cs @@ -50,7 +50,6 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption if (_alphabet != null) { - query = _alphabet.Translate(query); stringToCompare = _alphabet.Translate(stringToCompare); } From 0094c8cc6b9ca72956569025752b32c42afb08b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Thu, 15 Oct 2020 21:06:57 +0800 Subject: [PATCH 04/11] Change Pinyin Libiary (reference from Wox) --- Flow.Launcher.Infrastructure/Alphabet.cs | 152 +++-------------------- 1 file changed, 18 insertions(+), 134 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Alphabet.cs b/Flow.Launcher.Infrastructure/Alphabet.cs index 7e24a820673..11fd6873b76 100644 --- a/Flow.Launcher.Infrastructure/Alphabet.cs +++ b/Flow.Launcher.Infrastructure/Alphabet.cs @@ -3,12 +3,12 @@ using System.Collections.Generic; using System.Linq; using System.Text; -using hyjiacan.util.p4n; -using hyjiacan.util.p4n.format; using JetBrains.Annotations; using Flow.Launcher.Infrastructure.Logger; using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Infrastructure.UserSettings; +using ToolGood.Words.Pinyin; +using System.Threading.Tasks; namespace Flow.Launcher.Infrastructure { @@ -19,160 +19,44 @@ public interface IAlphabet public class Alphabet : IAlphabet { - private readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat(); - private ConcurrentDictionary PinyinCache; - private BinaryStorage> _pinyinStorage; + private ConcurrentDictionary _pinyinCache; private Settings _settings; - + public void Initialize([NotNull] Settings settings) { _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - InitializePinyinHelpers(); - } - - private void InitializePinyinHelpers() - { - Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE); - - Stopwatch.Normal("|Flow Launcher.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () => - { - _pinyinStorage = new BinaryStorage>("Pinyin"); - - lock(_pinyinStorage) - { - var loaded = _pinyinStorage.TryLoad(new Dictionary()); - - PinyinCache = new ConcurrentDictionary(loaded); - } - - // force pinyin library static constructor initialize - PinyinHelper.toHanyuPinyinStringArray('T', Format); - }); - Log.Info($"|Flow Launcher.Infrastructure.Alphabet.Initialize|Number of preload pinyin combination<{PinyinCache.Count}>"); - } - - public string Translate(string str) - { - return ConvertChineseCharactersToPinyin(str); - } - - public string ConvertChineseCharactersToPinyin(string source) - { - if (!_settings.ShouldUsePinyin) - return source; - - if (string.IsNullOrEmpty(source)) - return source; - - if (!ContainsChinese(source)) - return source; - - var combination = PinyinCombination(source); - - var pinyinArray=combination.Select(x => string.Join("", x)); - var acronymArray = combination.Select(Acronym).Distinct(); - - var joinedSingleStringCombination = new StringBuilder(); - var all = acronymArray.Concat(pinyinArray); - all.ToList().ForEach(x => joinedSingleStringCombination.Append(x)); - - return joinedSingleStringCombination.ToString(); - } - - public void Save() - { - if (!_settings.ShouldUsePinyin) - { - return; - } - - lock(_pinyinStorage) - { - _pinyinStorage.Save(PinyinCache.ToDictionary(i => i.Key, i => i.Value)); - } } - private static string[] EmptyStringArray = new string[0]; - private static string[][] Empty2DStringArray = new string[0][]; - /// - /// replace chinese character with pinyin, non chinese character won't be modified - /// Because we don't have words dictionary, so we can only return all possiblie pinyin combination - /// e.g. 音乐 will return yinyue and yinle - /// should be word or sentence, instead of single character. e.g. 微软 - /// - public string[][] PinyinCombination(string characters) + public string Translate(string content) { - if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters)) - { - return Empty2DStringArray; - } - - if (!PinyinCache.ContainsKey(characters)) + if (_settings.ShouldUsePinyin) { - var allPinyins = new List(); - foreach (var c in characters) + string result = _pinyinCache.GetValueOrDefault(content); + if (result == null) { - var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format); - if (pinyins != null) + if (WordsHelper.HasChinese(content)) { - var r = pinyins.Distinct().ToArray(); - allPinyins.Add(r); + result = WordsHelper.GetPinyin(content,";"); + result = GetFirstPinyinChar(result) + result.Replace(";",""); + _pinyinCache[content] = result; } else { - var r = new[] { c.ToString() }; - allPinyins.Add(r); + result = content; } } - - var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray(); - PinyinCache[characters] = combination; - return combination; + return result; } else { - return PinyinCache[characters]; + return content; } } - public string Acronym(string[] pinyin) - { - var acronym = string.Join("", pinyin.Select(p => p[0])); - return acronym; - } - - public bool ContainsChinese(string word) + private string GetFirstPinyinChar(string content) { - if (!_settings.ShouldUsePinyin) - { - return false; - } - - if (word.Length > 40) - { - //Skip strings that are too long string for Pinyin conversion. - return false; - } - - var chinese = word.Select(PinyinHelper.toHanyuPinyinStringArray) - .Any(p => p != null); - return chinese; - } - - private string[] Combination(string[] array1, string[] array2) - { - if (!_settings.ShouldUsePinyin) - { - return EmptyStringArray; - } - - var combination = ( - from a1 in array1 - from a2 in array2 - select $"{a1};{a2}" - ).ToArray(); - return combination; + return new string(content.Split(";").Select(c => c.First()).ToArray()); } } -} +} \ No newline at end of file From daac3a3b8e11a272aad9926114b705ed6197ae0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sat, 17 Oct 2020 14:08:35 +0800 Subject: [PATCH 05/11] Revert "remove unnecessery query translate" This reverts commit eefe210ecaffde42edf30e78b20e735f90420402. --- Flow.Launcher.Infrastructure/StringMatcher.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Flow.Launcher.Infrastructure/StringMatcher.cs b/Flow.Launcher.Infrastructure/StringMatcher.cs index d390e48a2f1..2a4270fb4b2 100644 --- a/Flow.Launcher.Infrastructure/StringMatcher.cs +++ b/Flow.Launcher.Infrastructure/StringMatcher.cs @@ -50,6 +50,7 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption if (_alphabet != null) { + query = _alphabet.Translate(query); stringToCompare = _alphabet.Translate(stringToCompare); } From 75c815e30600adea20b8cc079aa3b3cea9440bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sat, 17 Oct 2020 14:17:53 +0800 Subject: [PATCH 06/11] change new string to string.concat() --- Flow.Launcher.Infrastructure/Alphabet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Alphabet.cs b/Flow.Launcher.Infrastructure/Alphabet.cs index 11fd6873b76..eda9caaaebd 100644 --- a/Flow.Launcher.Infrastructure/Alphabet.cs +++ b/Flow.Launcher.Infrastructure/Alphabet.cs @@ -56,7 +56,7 @@ public string Translate(string content) private string GetFirstPinyinChar(string content) { - return new string(content.Split(";").Select(c => c.First()).ToArray()); + return string.Concat(content.Split(';').Select(x => x.First())); } } } \ No newline at end of file From 94256268a340c6ec3809fdba60dccf44d03c1825 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 18 Oct 2020 09:59:58 +0800 Subject: [PATCH 07/11] Fix uninitialized issue --- Flow.Launcher.Infrastructure/Alphabet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/Alphabet.cs b/Flow.Launcher.Infrastructure/Alphabet.cs index eda9caaaebd..9f5477008ad 100644 --- a/Flow.Launcher.Infrastructure/Alphabet.cs +++ b/Flow.Launcher.Infrastructure/Alphabet.cs @@ -19,7 +19,7 @@ public interface IAlphabet public class Alphabet : IAlphabet { - private ConcurrentDictionary _pinyinCache; + private ConcurrentDictionary _pinyinCache = new ConcurrentDictionary(); private Settings _settings; public void Initialize([NotNull] Settings settings) From 2f6080478572983116925da4e51c03e893600b8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 18 Oct 2020 11:35:20 +0800 Subject: [PATCH 08/11] change query translation to Program.Main instead of using Alphabet.Tranlate to avoid cache. --- Flow.Launcher.Infrastructure/StringMatcher.cs | 1 - Plugins/Flow.Launcher.Plugin.Program/Main.cs | 7 +++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher.Infrastructure/StringMatcher.cs b/Flow.Launcher.Infrastructure/StringMatcher.cs index 2a4270fb4b2..d390e48a2f1 100644 --- a/Flow.Launcher.Infrastructure/StringMatcher.cs +++ b/Flow.Launcher.Infrastructure/StringMatcher.cs @@ -50,7 +50,6 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption if (_alphabet != null) { - query = _alphabet.Translate(query); stringToCompare = _alphabet.Translate(stringToCompare); } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index 9f3160746b8..36ff8bbe57e 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -8,6 +8,7 @@ using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Plugin.Program.Programs; using Flow.Launcher.Plugin.Program.Views; +using ToolGood.Words.Pinyin; using Stopwatch = Flow.Launcher.Infrastructure.Stopwatch; namespace Flow.Launcher.Plugin.Program @@ -77,13 +78,15 @@ public List Query(Query query) uwps = _uwps; } + var searchText = WordsHelper.HasChinese(query.Search) ? WordsHelper.GetPinyin(query.Search) : query.Search; + var results1 = win32.AsParallel() .Where(p => p.Enabled) - .Select(p => p.Result(query.Search, _context.API)); + .Select(p => p.Result(searchText, _context.API)); var results2 = uwps.AsParallel() .Where(p => p.Enabled) - .Select(p => p.Result(query.Search, _context.API)); + .Select(p => p.Result(searchText, _context.API)); var result = results1.Concat(results2).Where(r => r != null && r.Score > 0).ToList(); return result; From a0924ee73f0a75916e21980fdcdc4e1abd0201f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 18 Oct 2020 11:45:17 +0800 Subject: [PATCH 09/11] Use Generic to merge query from win32 and uwp --- Plugins/Flow.Launcher.Plugin.Program/Main.cs | 16 +++++++--------- .../Programs/IProgram.cs | 4 +++- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/Plugins/Flow.Launcher.Plugin.Program/Main.cs b/Plugins/Flow.Launcher.Plugin.Program/Main.cs index 36ff8bbe57e..9b779ec0c87 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Main.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Main.cs @@ -80,15 +80,13 @@ public List Query(Query query) var searchText = WordsHelper.HasChinese(query.Search) ? WordsHelper.GetPinyin(query.Search) : query.Search; - var results1 = win32.AsParallel() - .Where(p => p.Enabled) - .Select(p => p.Result(searchText, _context.API)); - - var results2 = uwps.AsParallel() - .Where(p => p.Enabled) - .Select(p => p.Result(searchText, _context.API)); - - var result = results1.Concat(results2).Where(r => r != null && r.Score > 0).ToList(); + var result = win32 + .Cast() + .Concat(uwps) + .AsParallel() + .Where(p => p.Enabled) + .Select(p => p.Result(searchText, _context.API)) + .Where(r=>r?.Score > 0).ToList(); return result; } diff --git a/Plugins/Flow.Launcher.Plugin.Program/Programs/IProgram.cs b/Plugins/Flow.Launcher.Plugin.Program/Programs/IProgram.cs index b42acfbce5b..e71d0884a09 100644 --- a/Plugins/Flow.Launcher.Plugin.Program/Programs/IProgram.cs +++ b/Plugins/Flow.Launcher.Plugin.Program/Programs/IProgram.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace Flow.Launcher.Plugin.Program.Programs { @@ -9,5 +10,6 @@ public interface IProgram string UniqueIdentifier { get; set; } string Name { get; } string Location { get; } + bool Enabled { get; } } } From 417a418078ebc8ada37e64d30b0a95b56c513795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 18 Oct 2020 11:46:56 +0800 Subject: [PATCH 10/11] using ContainKey instead of getvalueofdefault --- Flow.Launcher.Infrastructure/Alphabet.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Alphabet.cs b/Flow.Launcher.Infrastructure/Alphabet.cs index 9f5477008ad..28d7f2f8942 100644 --- a/Flow.Launcher.Infrastructure/Alphabet.cs +++ b/Flow.Launcher.Infrastructure/Alphabet.cs @@ -32,21 +32,20 @@ public string Translate(string content) { if (_settings.ShouldUsePinyin) { - string result = _pinyinCache.GetValueOrDefault(content); - if (result == null) + if (_pinyinCache.ContainsKey(content)) { if (WordsHelper.HasChinese(content)) { - result = WordsHelper.GetPinyin(content,";"); + var result = WordsHelper.GetPinyin(content,";"); result = GetFirstPinyinChar(result) + result.Replace(";",""); _pinyinCache[content] = result; + return result; } else { - result = content; + return content; } } - return result; } else { From e8eb80f900ba4dfd600693d7c4b5c79ca5f9eb4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Sun, 18 Oct 2020 11:48:17 +0800 Subject: [PATCH 11/11] fixup the reduced code due to testing --- Flow.Launcher.Infrastructure/Alphabet.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Infrastructure/Alphabet.cs b/Flow.Launcher.Infrastructure/Alphabet.cs index 28d7f2f8942..792b06bc234 100644 --- a/Flow.Launcher.Infrastructure/Alphabet.cs +++ b/Flow.Launcher.Infrastructure/Alphabet.cs @@ -36,8 +36,8 @@ public string Translate(string content) { if (WordsHelper.HasChinese(content)) { - var result = WordsHelper.GetPinyin(content,";"); - result = GetFirstPinyinChar(result) + result.Replace(";",""); + var result = WordsHelper.GetPinyin(content, ";"); + result = GetFirstPinyinChar(result) + result.Replace(";", ""); _pinyinCache[content] = result; return result; } @@ -46,6 +46,8 @@ public string Translate(string content) return content; } } + else + return _pinyinCache[content]; } else {