From 3abd05f6b3a687e73fc9e6ab0f09ec2af2f6c06a Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 18 Nov 2023 14:58:16 +0800 Subject: [PATCH 01/13] Implement double pinyin --- .../DoublePinAlphabet.cs | 193 ++++++++++++++++++ .../UserSettings/Settings.cs | 2 + 2 files changed, 195 insertions(+) create mode 100644 Flow.Launcher.Infrastructure/DoublePinAlphabet.cs diff --git a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs new file mode 100644 index 00000000000..607582097f9 --- /dev/null +++ b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs @@ -0,0 +1,193 @@ +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Text; +using Flow.Launcher.Infrastructure.UserSettings; +using ToolGood.Words.Pinyin; + +namespace Flow.Launcher.Infrastructure +{ + public class DoublePinAlphabet : IAlphabet + { + private ConcurrentDictionary _doublePinCache = + new ConcurrentDictionary(); + + private Settings _settings; + + public void Initialize([NotNull] Settings settings) + { + _settings = settings ?? throw new ArgumentNullException(nameof(settings)); + } + + public bool CanBeTranslated(string stringToTranslate) + { + return WordsHelper.HasChinese(stringToTranslate); + } + + public (string translation, TranslationMapping map) Translate(string content) + { + if (_settings.ShouldUseDoublePin) + { + if (!_doublePinCache.ContainsKey(content)) + { + return BuildCacheFromContent(content); + } + else + { + return _doublePinCache[content]; + } + } + return (content, null); + } + + private (string translation, TranslationMapping map) BuildCacheFromContent(string content) + { + if (WordsHelper.HasChinese(content)) + { + var resultList = WordsHelper.GetPinyinList(content).Select(ToDoublePin).ToArray(); + StringBuilder resultBuilder = new StringBuilder(); + TranslationMapping map = new TranslationMapping(); + + bool pre = false; + + for (int i = 0; i < resultList.Length; i++) + { + if (content[i] >= 0x3400 && content[i] <= 0x9FD5) + { + map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1); + resultBuilder.Append(' '); + resultBuilder.Append(resultList[i]); + pre = true; + } + else + { + if (pre) + { + pre = false; + resultBuilder.Append(' '); + } + + resultBuilder.Append(resultList[i]); + } + } + + map.endConstruct(); + + var key = resultBuilder.ToString(); + map.setKey(key); + + return _doublePinCache[content] = (key, map); + } + else + { + return (content, null); + } + } + + private static readonly ReadOnlyDictionary special = new(new Dictionary(){ + {"a", "aa"}, + {"ai", "ai"}, + {"an", "an"}, + {"ang", "ah"}, + {"ao", "ao"}, + {"e", "ee"}, + {"ei", "ei"}, + {"en", "en"}, + {"er", "er"}, + {"o", "oo"}, + {"ou", "ou"} + }); + + + private static readonly ReadOnlyDictionary first = new(new Dictionary(){ + {"ch", "i"}, + {"sh", "u"}, + {"zh", "v"} + }); + + + private static readonly ReadOnlyDictionary second = new(new Dictionary() + { + {"ua", "x"}, + {"ei", "w"}, + {"e", "e"}, + {"ou", "z"}, + {"iu", "q"}, + {"ve", "t"}, + {"ue", "t"}, + {"u", "u"}, + {"i", "i"}, + {"o", "o"}, + {"uo", "o"}, + {"ie", "p"}, + {"a", "a"}, + {"ong", "s"}, + {"iong", "s"}, + {"ai", "d"}, + {"ing", "k"}, + {"uai", "k"}, + {"ang", "h"}, + {"uan", "r"}, + {"an", "j"}, + {"en", "f"}, + {"ia", "x"}, + {"iang", "l"}, + {"uang", "l"}, + {"eng", "g"}, + {"in", "b"}, + {"ao", "c"}, + {"v", "v"}, + {"ui", "v"}, + {"un", "y"}, + {"iao", "n"}, + {"ian", "m"} + }); + + private static string ToDoublePin(string fullPinyin) + { + // Assuming s is valid + StringBuilder doublePin = new StringBuilder(); + + if (fullPinyin.Length <= 3 && (fullPinyin[0] == 'a' || fullPinyin[0] == 'e' || fullPinyin[0] == 'o')) + { + if (special.ContainsKey(fullPinyin)) + { + return special[fullPinyin]; + } + } + + // zh, ch, sh + if (fullPinyin.Length >= 2 && first.ContainsKey(fullPinyin[..2])) + { + doublePin.Append(first[fullPinyin[..2]]); + + if (second.TryGetValue(fullPinyin[2..], out string tmp)) + { + doublePin.Append(tmp); + } + else + { + doublePin.Append(fullPinyin[2..]); + } + } + else + { + doublePin.Append(fullPinyin[0]); + + if (second.TryGetValue(fullPinyin[1..], out string tmp)) + { + doublePin.Append(tmp); + } + else + { + doublePin.Append(fullPinyin[1..]); + } + } + + return doublePin.ToString(); + } + } +} diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 4588466652e..8d94cdba56a 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -185,6 +185,8 @@ public CustomBrowserViewModel CustomBrowser /// when false Alphabet static service will always return empty results /// public bool ShouldUsePinyin { get; set; } = false; + + public bool ShouldUseDoublePin { get; set; } = false; public bool AlwaysPreview { get; set; } = false; public bool AlwaysStartEn { get; set; } = false; From fb6635344b8e5ecae06aadd31a3b112471b48ae2 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 18 Nov 2023 14:58:23 +0800 Subject: [PATCH 02/13] Test double pinyin --- Flow.Launcher/App.xaml.cs | 2 +- Flow.Launcher/PublicAPIInstance.cs | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 765a1a5593a..d74ea62fb1b 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -30,7 +30,7 @@ public partial class App : IDisposable, ISingleInstanceApp private SettingWindowViewModel _settingsVM; private readonly Updater _updater = new Updater(Flow.Launcher.Properties.Settings.Default.GithubRepo); private readonly Portable _portable = new Portable(); - private readonly PinyinAlphabet _alphabet = new PinyinAlphabet(); + private readonly DoublePinAlphabet _alphabet = new DoublePinAlphabet(); private StringMatcher _stringMatcher; [STAThread] diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index b49bf39d3c5..952ce0edb9b 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -32,11 +32,11 @@ public class PublicAPIInstance : IPublicAPI { private readonly SettingWindowViewModel _settingsVM; private readonly MainViewModel _mainVM; - private readonly PinyinAlphabet _alphabet; + private readonly DoublePinAlphabet _alphabet; #region Constructor - public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, PinyinAlphabet alphabet) + public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, DoublePinAlphabet alphabet) { _settingsVM = settingsVM; _mainVM = mainVM; From f6ae71a99f838b2237c14c62c6daff5e97e6eec0 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 18 Nov 2023 20:24:13 +0800 Subject: [PATCH 03/13] Only convert to double pinyin when meeting Chinese --- Flow.Launcher.Infrastructure/DoublePinAlphabet.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs index 607582097f9..e09046410aa 100644 --- a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs @@ -47,7 +47,7 @@ public bool CanBeTranslated(string stringToTranslate) { if (WordsHelper.HasChinese(content)) { - var resultList = WordsHelper.GetPinyinList(content).Select(ToDoublePin).ToArray(); + var resultList = WordsHelper.GetPinyinList(content); StringBuilder resultBuilder = new StringBuilder(); TranslationMapping map = new TranslationMapping(); @@ -57,9 +57,10 @@ public bool CanBeTranslated(string stringToTranslate) { if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { - map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1); + string dp = ToDoublePin(resultList[i].ToLower()); + map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); resultBuilder.Append(' '); - resultBuilder.Append(resultList[i]); + resultBuilder.Append(dp); pre = true; } else From e5285b19921ae2cc49b6f55ebe5772a77f05cd54 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 19 Nov 2023 12:30:17 +0800 Subject: [PATCH 04/13] Temp: compatibility with full pinyin option --- Flow.Launcher.Infrastructure/DoublePinAlphabet.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs index e09046410aa..e6930ad931a 100644 --- a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs @@ -29,7 +29,7 @@ public bool CanBeTranslated(string stringToTranslate) public (string translation, TranslationMapping map) Translate(string content) { - if (_settings.ShouldUseDoublePin) + if (_settings.ShouldUsePinyin) { if (!_doublePinCache.ContainsKey(content)) { @@ -57,7 +57,7 @@ public bool CanBeTranslated(string stringToTranslate) { if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { - string dp = ToDoublePin(resultList[i].ToLower()); + string dp = _settings.ShouldUseDoublePin ? resultList[i] : ToDoublePin(resultList[i].ToLower()); map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); resultBuilder.Append(' '); resultBuilder.Append(dp); From 99ff3b2ec5c9a4d8b4f136735a866f52914c246e Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 20 Nov 2023 22:49:39 +0800 Subject: [PATCH 05/13] Fix wrong condition --- Flow.Launcher.Infrastructure/DoublePinAlphabet.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs index e6930ad931a..a1eb788d730 100644 --- a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs @@ -57,7 +57,7 @@ public bool CanBeTranslated(string stringToTranslate) { if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { - string dp = _settings.ShouldUseDoublePin ? resultList[i] : ToDoublePin(resultList[i].ToLower()); + string dp = _settings.ShouldUseDoublePin ? ToDoublePin(resultList[i].ToLower()) : resultList[i]; map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); resultBuilder.Append(' '); resultBuilder.Append(dp); From 46d49d8fdf373e020481b111cbd23bf8ee09538d Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sat, 25 May 2024 15:02:37 +0800 Subject: [PATCH 06/13] Only translate when string is double pinyin --- Flow.Launcher.Infrastructure/DoublePinAlphabet.cs | 4 ++-- Flow.Launcher.Infrastructure/PinyinAlphabet.cs | 4 ++-- Flow.Launcher.Infrastructure/StringMatcher.cs | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs index a1eb788d730..945f47a56b0 100644 --- a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs @@ -22,9 +22,9 @@ public void Initialize([NotNull] Settings settings) _settings = settings ?? throw new ArgumentNullException(nameof(settings)); } - public bool CanBeTranslated(string stringToTranslate) + public bool ShouldTranslate(string stringToTranslate) { - return WordsHelper.HasChinese(stringToTranslate); + return stringToTranslate.Length % 2 == 0 && !WordsHelper.HasChinese(stringToTranslate); } public (string translation, TranslationMapping map) Translate(string content) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 7d72359684d..961af1d322f 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -119,7 +119,7 @@ public interface IAlphabet /// /// String to translate. /// - public bool CanBeTranslated(string stringToTranslate); + public bool ShouldTranslate(string stringToTranslate); } public class PinyinAlphabet : IAlphabet @@ -134,7 +134,7 @@ public void Initialize([NotNull] Settings settings) _settings = settings ?? throw new ArgumentNullException(nameof(settings)); } - public bool CanBeTranslated(string stringToTranslate) + public bool ShouldTranslate(string stringToTranslate) { return WordsHelper.HasChinese(stringToTranslate); } diff --git a/Flow.Launcher.Infrastructure/StringMatcher.cs b/Flow.Launcher.Infrastructure/StringMatcher.cs index bd5dbdda9be..4929e4cd2cf 100644 --- a/Flow.Launcher.Infrastructure/StringMatcher.cs +++ b/Flow.Launcher.Infrastructure/StringMatcher.cs @@ -61,7 +61,7 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption query = query.Trim(); TranslationMapping translationMapping = null; - if (_alphabet is not null && !_alphabet.CanBeTranslated(query)) + if (_alphabet is not null && _alphabet.ShouldTranslate(query)) { // We assume that if a query can be translated (containing characters of a language, like Chinese) // it actually means user doesn't want it to be translated to English letters. From b1cb852673005e955b1d4bd0bb2ed692e44308c9 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:18:57 +0800 Subject: [PATCH 07/13] Extract classes --- Flow.Launcher.Infrastructure/IAlphabet.cs | 22 ++++ .../PinyinAlphabet.cs | 113 ------------------ .../TranslationMapping.cs | 99 +++++++++++++++ 3 files changed, 121 insertions(+), 113 deletions(-) create mode 100644 Flow.Launcher.Infrastructure/IAlphabet.cs create mode 100644 Flow.Launcher.Infrastructure/TranslationMapping.cs diff --git a/Flow.Launcher.Infrastructure/IAlphabet.cs b/Flow.Launcher.Infrastructure/IAlphabet.cs new file mode 100644 index 00000000000..e79ec0c6d6f --- /dev/null +++ b/Flow.Launcher.Infrastructure/IAlphabet.cs @@ -0,0 +1,22 @@ +namespace Flow.Launcher.Infrastructure +{ + /// + /// Translate a language to English letters using a given rule. + /// + public interface IAlphabet + { + /// + /// Translate a string to English letters, using a given rule. + /// + /// String to translate. + /// + public (string translation, TranslationMapping map) Translate(string stringToTranslate); + + /// + /// Determine if a string can be translated to English letter with this Alphabet. + /// + /// String to translate. + /// + public bool ShouldTranslate(string stringToTranslate); + } +} diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 961af1d322f..d98d823d7b9 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -9,119 +9,6 @@ namespace Flow.Launcher.Infrastructure { - public class TranslationMapping - { - private bool constructed; - - private List originalIndexs = new List(); - private List translatedIndexs = new List(); - private int translatedLength = 0; - - public string key { get; private set; } - - public void setKey(string key) - { - this.key = key; - } - - public void AddNewIndex(int originalIndex, int translatedIndex, int length) - { - if (constructed) - throw new InvalidOperationException("Mapping shouldn't be changed after constructed"); - - originalIndexs.Add(originalIndex); - translatedIndexs.Add(translatedIndex); - translatedIndexs.Add(translatedIndex + length); - translatedLength += length - 1; - } - - public int MapToOriginalIndex(int translatedIndex) - { - if (translatedIndex > translatedIndexs.Last()) - return translatedIndex - translatedLength - 1; - - int lowerBound = 0; - int upperBound = originalIndexs.Count - 1; - - int count = 0; - - // Corner case handle - if (translatedIndex < translatedIndexs[0]) - return translatedIndex; - if (translatedIndex > translatedIndexs.Last()) - { - int indexDef = 0; - for (int k = 0; k < originalIndexs.Count; k++) - { - indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2]; - } - - return translatedIndex - indexDef - 1; - } - - // Binary Search with Range - for (int i = originalIndexs.Count / 2;; count++) - { - if (translatedIndex < translatedIndexs[i * 2]) - { - // move to lower middle - upperBound = i; - i = (i + lowerBound) / 2; - } - else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1) - { - lowerBound = i; - // move to upper middle - // due to floor of integer division, move one up on corner case - i = (i + upperBound + 1) / 2; - } - else - return originalIndexs[i]; - - if (upperBound - lowerBound <= 1 && - translatedIndex > translatedIndexs[lowerBound * 2 + 1] && - translatedIndex < translatedIndexs[upperBound * 2]) - { - int indexDef = 0; - - for (int j = 0; j < upperBound; j++) - { - indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2]; - } - - return translatedIndex - indexDef - 1; - } - } - } - - public void endConstruct() - { - if (constructed) - throw new InvalidOperationException("Mapping has already been constructed"); - constructed = true; - } - } - - /// - /// Translate a language to English letters using a given rule. - /// - public interface IAlphabet - { - /// - /// Translate a string to English letters, using a given rule. - /// - /// String to translate. - /// - public (string translation, TranslationMapping map) Translate(string stringToTranslate); - - /// - /// Determine if a string can be translated to English letter with this Alphabet. - /// - /// String to translate. - /// - public bool ShouldTranslate(string stringToTranslate); - } - public class PinyinAlphabet : IAlphabet { private ConcurrentDictionary _pinyinCache = diff --git a/Flow.Launcher.Infrastructure/TranslationMapping.cs b/Flow.Launcher.Infrastructure/TranslationMapping.cs new file mode 100644 index 00000000000..f288c816a64 --- /dev/null +++ b/Flow.Launcher.Infrastructure/TranslationMapping.cs @@ -0,0 +1,99 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace Flow.Launcher.Infrastructure +{ + public class TranslationMapping + { + private bool constructed; + + private List originalIndexs = new List(); + private List translatedIndexs = new List(); + private int translatedLength = 0; + + public string key { get; private set; } + + public void setKey(string key) + { + this.key = key; + } + + public void AddNewIndex(int originalIndex, int translatedIndex, int length) + { + if (constructed) + throw new InvalidOperationException("Mapping shouldn't be changed after constructed"); + + originalIndexs.Add(originalIndex); + translatedIndexs.Add(translatedIndex); + translatedIndexs.Add(translatedIndex + length); + translatedLength += length - 1; + } + + public int MapToOriginalIndex(int translatedIndex) + { + if (translatedIndex > translatedIndexs.Last()) + return translatedIndex - translatedLength - 1; + + int lowerBound = 0; + int upperBound = originalIndexs.Count - 1; + + int count = 0; + + // Corner case handle + if (translatedIndex < translatedIndexs[0]) + return translatedIndex; + if (translatedIndex > translatedIndexs.Last()) + { + int indexDef = 0; + for (int k = 0; k < originalIndexs.Count; k++) + { + indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2]; + } + + return translatedIndex - indexDef - 1; + } + + // Binary Search with Range + for (int i = originalIndexs.Count / 2;; count++) + { + if (translatedIndex < translatedIndexs[i * 2]) + { + // move to lower middle + upperBound = i; + i = (i + lowerBound) / 2; + } + else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1) + { + lowerBound = i; + // move to upper middle + // due to floor of integer division, move one up on corner case + i = (i + upperBound + 1) / 2; + } + else + return originalIndexs[i]; + + if (upperBound - lowerBound <= 1 && + translatedIndex > translatedIndexs[lowerBound * 2 + 1] && + translatedIndex < translatedIndexs[upperBound * 2]) + { + int indexDef = 0; + + for (int j = 0; j < upperBound; j++) + { + indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2]; + } + + return translatedIndex - indexDef - 1; + } + } + } + + public void endConstruct() + { + if (constructed) + throw new InvalidOperationException("Mapping has already been constructed"); + constructed = true; + } + } +} From 6807afbe6d753eed6984e1b1ab6019e2864767cd Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:03:00 +0800 Subject: [PATCH 08/13] Remove unused alphabet arg in PublicAPIInstance --- Flow.Launcher/App.xaml.cs | 2 +- Flow.Launcher/PublicAPIInstance.cs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index d74ea62fb1b..560bb052b19 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -74,7 +74,7 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () => PluginManager.LoadPlugins(_settings.PluginSettings); _mainVM = new MainViewModel(_settings); - API = new PublicAPIInstance(_settingsVM, _mainVM, _alphabet); + API = new PublicAPIInstance(_settingsVM, _mainVM); Http.API = API; Http.Proxy = _settings.Proxy; diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 952ce0edb9b..e14d692cd65 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -32,15 +32,13 @@ public class PublicAPIInstance : IPublicAPI { private readonly SettingWindowViewModel _settingsVM; private readonly MainViewModel _mainVM; - private readonly DoublePinAlphabet _alphabet; #region Constructor - public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, DoublePinAlphabet alphabet) + public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM) { _settingsVM = settingsVM; _mainVM = mainVM; - _alphabet = alphabet; GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback; WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); } From a2efa11699fed4b3aac21bdff26555ce57b274aa Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:19:27 +0800 Subject: [PATCH 09/13] Merge DoublePinAlphabet logic --- .../DoublePinAlphabet.cs | 194 ------------------ .../PinyinAlphabet.cs | 127 +++++++++++- .../UserSettings/Settings.cs | 3 +- Flow.Launcher/App.xaml.cs | 2 +- 4 files changed, 121 insertions(+), 205 deletions(-) delete mode 100644 Flow.Launcher.Infrastructure/DoublePinAlphabet.cs diff --git a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs b/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs deleted file mode 100644 index 945f47a56b0..00000000000 --- a/Flow.Launcher.Infrastructure/DoublePinAlphabet.cs +++ /dev/null @@ -1,194 +0,0 @@ -using System; -using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Diagnostics.CodeAnalysis; -using System.Linq; -using System.Text; -using Flow.Launcher.Infrastructure.UserSettings; -using ToolGood.Words.Pinyin; - -namespace Flow.Launcher.Infrastructure -{ - public class DoublePinAlphabet : IAlphabet - { - private ConcurrentDictionary _doublePinCache = - new ConcurrentDictionary(); - - private Settings _settings; - - public void Initialize([NotNull] Settings settings) - { - _settings = settings ?? throw new ArgumentNullException(nameof(settings)); - } - - public bool ShouldTranslate(string stringToTranslate) - { - return stringToTranslate.Length % 2 == 0 && !WordsHelper.HasChinese(stringToTranslate); - } - - public (string translation, TranslationMapping map) Translate(string content) - { - if (_settings.ShouldUsePinyin) - { - if (!_doublePinCache.ContainsKey(content)) - { - return BuildCacheFromContent(content); - } - else - { - return _doublePinCache[content]; - } - } - return (content, null); - } - - private (string translation, TranslationMapping map) BuildCacheFromContent(string content) - { - if (WordsHelper.HasChinese(content)) - { - var resultList = WordsHelper.GetPinyinList(content); - StringBuilder resultBuilder = new StringBuilder(); - TranslationMapping map = new TranslationMapping(); - - bool pre = false; - - for (int i = 0; i < resultList.Length; i++) - { - if (content[i] >= 0x3400 && content[i] <= 0x9FD5) - { - string dp = _settings.ShouldUseDoublePin ? ToDoublePin(resultList[i].ToLower()) : resultList[i]; - map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); - resultBuilder.Append(' '); - resultBuilder.Append(dp); - pre = true; - } - else - { - if (pre) - { - pre = false; - resultBuilder.Append(' '); - } - - resultBuilder.Append(resultList[i]); - } - } - - map.endConstruct(); - - var key = resultBuilder.ToString(); - map.setKey(key); - - return _doublePinCache[content] = (key, map); - } - else - { - return (content, null); - } - } - - private static readonly ReadOnlyDictionary special = new(new Dictionary(){ - {"a", "aa"}, - {"ai", "ai"}, - {"an", "an"}, - {"ang", "ah"}, - {"ao", "ao"}, - {"e", "ee"}, - {"ei", "ei"}, - {"en", "en"}, - {"er", "er"}, - {"o", "oo"}, - {"ou", "ou"} - }); - - - private static readonly ReadOnlyDictionary first = new(new Dictionary(){ - {"ch", "i"}, - {"sh", "u"}, - {"zh", "v"} - }); - - - private static readonly ReadOnlyDictionary second = new(new Dictionary() - { - {"ua", "x"}, - {"ei", "w"}, - {"e", "e"}, - {"ou", "z"}, - {"iu", "q"}, - {"ve", "t"}, - {"ue", "t"}, - {"u", "u"}, - {"i", "i"}, - {"o", "o"}, - {"uo", "o"}, - {"ie", "p"}, - {"a", "a"}, - {"ong", "s"}, - {"iong", "s"}, - {"ai", "d"}, - {"ing", "k"}, - {"uai", "k"}, - {"ang", "h"}, - {"uan", "r"}, - {"an", "j"}, - {"en", "f"}, - {"ia", "x"}, - {"iang", "l"}, - {"uang", "l"}, - {"eng", "g"}, - {"in", "b"}, - {"ao", "c"}, - {"v", "v"}, - {"ui", "v"}, - {"un", "y"}, - {"iao", "n"}, - {"ian", "m"} - }); - - private static string ToDoublePin(string fullPinyin) - { - // Assuming s is valid - StringBuilder doublePin = new StringBuilder(); - - if (fullPinyin.Length <= 3 && (fullPinyin[0] == 'a' || fullPinyin[0] == 'e' || fullPinyin[0] == 'o')) - { - if (special.ContainsKey(fullPinyin)) - { - return special[fullPinyin]; - } - } - - // zh, ch, sh - if (fullPinyin.Length >= 2 && first.ContainsKey(fullPinyin[..2])) - { - doublePin.Append(first[fullPinyin[..2]]); - - if (second.TryGetValue(fullPinyin[2..], out string tmp)) - { - doublePin.Append(tmp); - } - else - { - doublePin.Append(fullPinyin[2..]); - } - } - else - { - doublePin.Append(fullPinyin[0]); - - if (second.TryGetValue(fullPinyin[1..], out string tmp)) - { - doublePin.Append(tmp); - } - else - { - doublePin.Append(fullPinyin[1..]); - } - } - - return doublePin.ToString(); - } - } -} diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index d98d823d7b9..37e4f93d292 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -1,18 +1,18 @@ using System; using System.Collections.Concurrent; -using System.Collections.Generic; -using System.Linq; using System.Text; using JetBrains.Annotations; using Flow.Launcher.Infrastructure.UserSettings; using ToolGood.Words.Pinyin; +using System.Collections.Generic; +using System.Collections.ObjectModel; namespace Flow.Launcher.Infrastructure { public class PinyinAlphabet : IAlphabet { - private ConcurrentDictionary _pinyinCache = - new ConcurrentDictionary(); + private readonly ConcurrentDictionary _pinyinCache = + new(); private Settings _settings; @@ -23,20 +23,22 @@ public void Initialize([NotNull] Settings settings) public bool ShouldTranslate(string stringToTranslate) { - return WordsHelper.HasChinese(stringToTranslate); + return _settings.UseDoublePinyin ? + (WordsHelper.HasChinese(stringToTranslate) && stringToTranslate.Length % 2 == 0) : + WordsHelper.HasChinese(stringToTranslate); } public (string translation, TranslationMapping map) Translate(string content) { if (_settings.ShouldUsePinyin) { - if (!_pinyinCache.ContainsKey(content)) + if (!_pinyinCache.TryGetValue(content, out var value)) { return BuildCacheFromContent(content); } else { - return _pinyinCache[content]; + return value; } } return (content, null); @@ -57,9 +59,10 @@ public bool ShouldTranslate(string stringToTranslate) { if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { - map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1); + string dp = _settings.UseDoublePinyin ? ToDoublePin(resultList[i].ToLower()) : resultList[i]; + map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); resultBuilder.Append(' '); - resultBuilder.Append(resultList[i]); + resultBuilder.Append(dp); pre = true; } else @@ -86,5 +89,111 @@ public bool ShouldTranslate(string stringToTranslate) return (content, null); } } + + #region Double Pinyin + + private static readonly ReadOnlyDictionary special = new(new Dictionary(){ + {"a", "aa"}, + {"ai", "ai"}, + {"an", "an"}, + {"ang", "ah"}, + {"ao", "ao"}, + {"e", "ee"}, + {"ei", "ei"}, + {"en", "en"}, + {"er", "er"}, + {"o", "oo"}, + {"ou", "ou"} + }); + + + private static readonly ReadOnlyDictionary first = new(new Dictionary(){ + {"ch", "i"}, + {"sh", "u"}, + {"zh", "v"} + }); + + + private static readonly ReadOnlyDictionary second = new(new Dictionary() + { + {"ua", "x"}, + {"ei", "w"}, + {"e", "e"}, + {"ou", "z"}, + {"iu", "q"}, + {"ve", "t"}, + {"ue", "t"}, + {"u", "u"}, + {"i", "i"}, + {"o", "o"}, + {"uo", "o"}, + {"ie", "p"}, + {"a", "a"}, + {"ong", "s"}, + {"iong", "s"}, + {"ai", "d"}, + {"ing", "k"}, + {"uai", "k"}, + {"ang", "h"}, + {"uan", "r"}, + {"an", "j"}, + {"en", "f"}, + {"ia", "x"}, + {"iang", "l"}, + {"uang", "l"}, + {"eng", "g"}, + {"in", "b"}, + {"ao", "c"}, + {"v", "v"}, + {"ui", "v"}, + {"un", "y"}, + {"iao", "n"}, + {"ian", "m"} + }); + + private static string ToDoublePin(string fullPinyin) + { + // Assuming s is valid + StringBuilder doublePin = new StringBuilder(); + + if (fullPinyin.Length <= 3 && (fullPinyin[0] == 'a' || fullPinyin[0] == 'e' || fullPinyin[0] == 'o')) + { + if (special.TryGetValue(fullPinyin, out var value)) + { + return value; + } + } + + // zh, ch, sh + if (fullPinyin.Length >= 2 && first.ContainsKey(fullPinyin[..2])) + { + doublePin.Append(first[fullPinyin[..2]]); + + if (second.TryGetValue(fullPinyin[2..], out string tmp)) + { + doublePin.Append(tmp); + } + else + { + doublePin.Append(fullPinyin[2..]); + } + } + else + { + doublePin.Append(fullPinyin[0]); + + if (second.TryGetValue(fullPinyin[1..], out string tmp)) + { + doublePin.Append(tmp); + } + else + { + doublePin.Append(fullPinyin[1..]); + } + } + + return doublePin.ToString(); + } + #endregion } } diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index 8d94cdba56a..e79c3f52d4d 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -186,7 +186,8 @@ public CustomBrowserViewModel CustomBrowser /// public bool ShouldUsePinyin { get; set; } = false; - public bool ShouldUseDoublePin { get; set; } = false; + public bool UseDoublePinyin { get; set; } = false; + public bool AlwaysPreview { get; set; } = false; public bool AlwaysStartEn { get; set; } = false; diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 560bb052b19..83870837a0b 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -30,7 +30,7 @@ public partial class App : IDisposable, ISingleInstanceApp private SettingWindowViewModel _settingsVM; private readonly Updater _updater = new Updater(Flow.Launcher.Properties.Settings.Default.GithubRepo); private readonly Portable _portable = new Portable(); - private readonly DoublePinAlphabet _alphabet = new DoublePinAlphabet(); + private readonly PinyinAlphabet _alphabet = new PinyinAlphabet(); private StringMatcher _stringMatcher; [STAThread] From 12c4e37a95df44fc968a6459e7a3cbf6de4063e6 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:33:49 +0800 Subject: [PATCH 10/13] Developing --- Flow.Launcher.Infrastructure/UserSettings/Settings.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs index e79c3f52d4d..30e3b77beee 100644 --- a/Flow.Launcher.Infrastructure/UserSettings/Settings.cs +++ b/Flow.Launcher.Infrastructure/UserSettings/Settings.cs @@ -186,7 +186,7 @@ public CustomBrowserViewModel CustomBrowser /// public bool ShouldUsePinyin { get; set; } = false; - public bool UseDoublePinyin { get; set; } = false; + public bool UseDoublePinyin { get; set; } = true; //For developing public bool AlwaysPreview { get; set; } = false; public bool AlwaysStartEn { get; set; } = false; From f673000d67e4ff1180e6d2856bf85f1640433a7c Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 14:37:15 +0800 Subject: [PATCH 11/13] Fix ShouldTranslate() --- Flow.Launcher.Infrastructure/PinyinAlphabet.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 37e4f93d292..b48a6109042 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -23,9 +23,9 @@ public void Initialize([NotNull] Settings settings) public bool ShouldTranslate(string stringToTranslate) { - return _settings.UseDoublePinyin ? - (WordsHelper.HasChinese(stringToTranslate) && stringToTranslate.Length % 2 == 0) : - WordsHelper.HasChinese(stringToTranslate); + return _settings.UseDoublePinyin ? + (!WordsHelper.HasChinese(stringToTranslate) && stringToTranslate.Length % 2 == 0) : + !WordsHelper.HasChinese(stringToTranslate); } public (string translation, TranslationMapping map) Translate(string content) From b10a6e19df5afcbd13ca612f6e6952f19a3b9dde Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 15:18:24 +0800 Subject: [PATCH 12/13] Capitalize first letter --- .../PinyinAlphabet.cs | 84 +++++++++---------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index b48a6109042..cbec7feae04 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -46,71 +46,69 @@ public bool ShouldTranslate(string stringToTranslate) private (string translation, TranslationMapping map) BuildCacheFromContent(string content) { - if (WordsHelper.HasChinese(content)) + if (!WordsHelper.HasChinese(content)) { - var resultList = WordsHelper.GetPinyinList(content); + return (content, null); + } - StringBuilder resultBuilder = new StringBuilder(); - TranslationMapping map = new TranslationMapping(); + var resultList = WordsHelper.GetPinyinList(content); - bool pre = false; + StringBuilder resultBuilder = new StringBuilder(); + TranslationMapping map = new TranslationMapping(); - for (int i = 0; i < resultList.Length; i++) + bool pre = false; + + for (int i = 0; i < resultList.Length; i++) + { + if (content[i] >= 0x3400 && content[i] <= 0x9FD5) { - if (content[i] >= 0x3400 && content[i] <= 0x9FD5) + string dp = _settings.UseDoublePinyin ? ToDoublePin(resultList[i]) : resultList[i]; + map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); + resultBuilder.Append(' '); + resultBuilder.Append(dp); + pre = true; + } + else + { + if (pre) { - string dp = _settings.UseDoublePinyin ? ToDoublePin(resultList[i].ToLower()) : resultList[i]; - map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); + pre = false; resultBuilder.Append(' '); - resultBuilder.Append(dp); - pre = true; } - else - { - if (pre) - { - pre = false; - resultBuilder.Append(' '); - } - resultBuilder.Append(resultList[i]); - } + resultBuilder.Append(resultList[i]); } + } - map.endConstruct(); + map.endConstruct(); - var key = resultBuilder.ToString(); - map.setKey(key); + var key = resultBuilder.ToString(); + map.setKey(key); - return _pinyinCache[content] = (key, map); - } - else - { - return (content, null); - } + return _pinyinCache[content] = (key, map); } #region Double Pinyin private static readonly ReadOnlyDictionary special = new(new Dictionary(){ - {"a", "aa"}, - {"ai", "ai"}, - {"an", "an"}, - {"ang", "ah"}, - {"ao", "ao"}, - {"e", "ee"}, - {"ei", "ei"}, - {"en", "en"}, - {"er", "er"}, - {"o", "oo"}, - {"ou", "ou"} + {"A", "aa"}, + {"Ai", "ai"}, + {"An", "an"}, + {"Ang", "ah"}, + {"Ao", "ao"}, + {"E", "ee"}, + {"Ei", "ei"}, + {"En", "en"}, + {"Er", "er"}, + {"O", "oo"}, + {"Ou", "ou"} }); private static readonly ReadOnlyDictionary first = new(new Dictionary(){ - {"ch", "i"}, - {"sh", "u"}, - {"zh", "v"} + {"Ch", "i"}, + {"Sh", "u"}, + {"Zh", "v"} }); From b816d1b866aa93b6d8f2da9f5cef334bda64748b Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Sun, 2 Jun 2024 15:21:04 +0800 Subject: [PATCH 13/13] Remove unused key in pinyin alphabet --- Flow.Launcher.Infrastructure/PinyinAlphabet.cs | 1 - Flow.Launcher.Infrastructure/TranslationMapping.cs | 7 ------- 2 files changed, 8 deletions(-) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index cbec7feae04..10799c676f1 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -83,7 +83,6 @@ public bool ShouldTranslate(string stringToTranslate) map.endConstruct(); var key = resultBuilder.ToString(); - map.setKey(key); return _pinyinCache[content] = (key, map); } diff --git a/Flow.Launcher.Infrastructure/TranslationMapping.cs b/Flow.Launcher.Infrastructure/TranslationMapping.cs index f288c816a64..c976fc522d6 100644 --- a/Flow.Launcher.Infrastructure/TranslationMapping.cs +++ b/Flow.Launcher.Infrastructure/TranslationMapping.cs @@ -12,13 +12,6 @@ public class TranslationMapping private List translatedIndexs = new List(); private int translatedLength = 0; - public string key { get; private set; } - - public void setKey(string key) - { - this.key = key; - } - public void AddNewIndex(int originalIndex, int translatedIndex, int length) { if (constructed)