From f17571e835997aa52feba1b6dd3f6e86675cdbf4 Mon Sep 17 00:00:00 2001 From: Vic <10308169+VictoriousRaptor@users.noreply.github.com> Date: Wed, 16 Nov 2022 23:32:27 +0800 Subject: [PATCH] Fix alphabet translation logic --- .../PinyinAlphabet.cs | 108 +++++++++++------- Flow.Launcher.Infrastructure/StringMatcher.cs | 11 +- Flow.Launcher.Test/FuzzyMatcherTest.cs | 2 +- 3 files changed, 74 insertions(+), 47 deletions(-) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 6c2a94e82..c43e8691a 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -83,7 +83,7 @@ namespace Flow.Launcher.Infrastructure translatedIndex < translatedIndexs[upperBound * 2]) { int indexDef = 0; - + for (int j = 0; j < upperBound; j++) { indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2]; @@ -102,9 +102,24 @@ 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 CanBeTranslated(string stringToTranslate); } public class PinyinAlphabet : IAlphabet @@ -119,63 +134,70 @@ namespace Flow.Launcher.Infrastructure _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.ShouldUsePinyin) { if (!_pinyinCache.ContainsKey(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) - { - 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 _pinyinCache[content] = (key, map); - } - else - { - return (content, null); - } + return BuildCacheFromContent(content); } else { return _pinyinCache[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) + { + 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 _pinyinCache[content] = (key, map); + } else { return (content, null); } } } -} \ No newline at end of file +} diff --git a/Flow.Launcher.Infrastructure/StringMatcher.cs b/Flow.Launcher.Infrastructure/StringMatcher.cs index 46165a849..bd5dbdda9 100644 --- a/Flow.Launcher.Infrastructure/StringMatcher.cs +++ b/Flow.Launcher.Infrastructure/StringMatcher.cs @@ -1,4 +1,4 @@ -using Flow.Launcher.Plugin.SharedModels; +using Flow.Launcher.Plugin.SharedModels; using System; using System.Collections.Generic; using System.Linq; @@ -60,8 +60,13 @@ namespace Flow.Launcher.Infrastructure return new MatchResult(false, UserSettingSearchPrecision); query = query.Trim(); - TranslationMapping translationMapping; - (stringToCompare, translationMapping) = _alphabet?.Translate(stringToCompare) ?? (stringToCompare, null); + TranslationMapping translationMapping = null; + if (_alphabet is not null && !_alphabet.CanBeTranslated(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. + (stringToCompare, translationMapping) = _alphabet.Translate(stringToCompare); + } var currentAcronymQueryIndex = 0; var acronymMatchData = new List(); diff --git a/Flow.Launcher.Test/FuzzyMatcherTest.cs b/Flow.Launcher.Test/FuzzyMatcherTest.cs index 46c848c7a..d7f143218 100644 --- a/Flow.Launcher.Test/FuzzyMatcherTest.cs +++ b/Flow.Launcher.Test/FuzzyMatcherTest.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq;