From aaa938191ad3e69aff5ae9de1daabfc69b5a9b74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Fri, 11 Dec 2020 22:08:46 +0800 Subject: [PATCH] Manually add sapce to only the cinese character and the surrounding. --- .../PinyinAlphabet.cs | 42 +++++++++++++++++-- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 9396226c7..d35250398 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -9,6 +9,7 @@ using Flow.Launcher.Infrastructure.Storage; using Flow.Launcher.Infrastructure.UserSettings; using ToolGood.Words.Pinyin; using System.Threading.Tasks; +using Microsoft.AspNetCore.Localization; namespace Flow.Launcher.Infrastructure { @@ -36,10 +37,43 @@ namespace Flow.Launcher.Infrastructure { if (WordsHelper.HasChinese(content)) { - var result = WordsHelper.GetPinyin(content, " "); - result = GetFirstPinyinChar(result) + result; - _pinyinCache[content] = result; - return result; + var resultList = WordsHelper.GetPinyinList(content); + + List chineseIndexs = new List(); + + for (int i = 0; i < content.Length; i++) + { + if (resultList[i].Length != 1 || !(resultList[i][0] == content[i])) + chineseIndexs.Add(i); + } + StringBuilder resultBuilder = new StringBuilder(); + resultBuilder.Append(string.Concat(resultList.Where((r, i) => chineseIndexs.Contains(i)).Select(s => s.First()))); + resultBuilder.Append(' '); + + int currentChineseIndex = 0; + int lastChineseIndex = -1; + for (int i = 0; i < resultList.Length; i++) + { + if (currentChineseIndex < chineseIndexs.Count && chineseIndexs[currentChineseIndex] == i) + { + resultBuilder.Append(' '); + + resultBuilder.Append(resultList[i]); + currentChineseIndex++; + lastChineseIndex = i; + } + else + { + if (i == lastChineseIndex + 1) + { + resultBuilder.Append(' '); + } + resultBuilder.Append(resultList[i]); + } + } + + + return _pinyinCache[content] = resultBuilder.ToString(); } else {