Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion Flow.Launcher.Infrastructure/PinyinAlphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ namespace Flow.Launcher.Infrastructure
{
public class PinyinAlphabet : IAlphabet
{
private static readonly IReadOnlyDictionary<string, string[]> PolyphonicPhraseOverrides = new Dictionary<string, string[]>
{
["重启"] = ["Chong", "Qi"],
};

private readonly ConcurrentDictionary<string, (string translation, TranslationMapping map)> _pinyinCache = new();
private readonly Settings _settings;
private ReadOnlyDictionary<string, string> currentDoublePinyinTable;

public PinyinAlphabet()
: this(Ioc.Default.GetRequiredService<Settings>())
{
}

public PinyinAlphabet(Settings settings)
{
_settings = Ioc.Default.GetRequiredService<Settings>();
ArgumentNullException.ThrowIfNull(settings);

_settings = settings;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
LoadDoublePinyinTable();

_settings.PropertyChanged += (sender, e) =>
Expand Down Expand Up @@ -118,6 +130,8 @@ public bool ShouldTranslate(string stringToTranslate)
private (string translation, TranslationMapping map) BuildCacheFromContent(string content)
{
var resultList = WordsHelper.GetPinyinList(content);
ApplyPolyphonicPhraseOverrides(content, resultList);

var resultBuilder = new StringBuilder(_settings.UseDoublePinyin ? 3 : 4); // Pre-allocate with estimated capacity
var map = new TranslationMapping();

Expand Down Expand Up @@ -163,6 +177,29 @@ public bool ShouldTranslate(string stringToTranslate)
return _pinyinCache[content] = result;
}

private static void ApplyPolyphonicPhraseOverrides(string content, string[] resultList)
{
foreach (var (phrase, pinyin) in PolyphonicPhraseOverrides)
{
var index = content.IndexOf(phrase, StringComparison.Ordinal);
while (index >= 0)
{
if (pinyin.Length != phrase.Length || index + pinyin.Length > resultList.Length)
{
index = content.IndexOf(phrase, index + phrase.Length, StringComparison.Ordinal);
continue;
}

for (var i = 0; i < pinyin.Length; i++)
{
resultList[index + i] = pinyin[i];
}

index = content.IndexOf(phrase, index + phrase.Length, StringComparison.Ordinal);
}
}
}

/// <summary>
/// Optimized Chinese character detection using the comprehensive CJK Unicode ranges
/// </summary>
Expand Down
40 changes: 40 additions & 0 deletions Flow.Launcher.Test/PinyinAlphabetTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.UserSettings;
using NUnit.Framework;
using NUnit.Framework.Legacy;

namespace Flow.Launcher.Test
{
[TestFixture]
public class PinyinAlphabetTest
{
[TestCase("重启", "Chong Qi")]
[TestCase("重启 Flow Launcher", "Chong Qi Flow Launcher")]
[TestCase("重庆", "Chong Qing")]
public void Translate_ShouldUseExpectedPinyinForPolyphonicPhrases(string content, string expected)
{
var alphabet = new PinyinAlphabet(new Settings
{
ShouldUsePinyin = true
});

var result = alphabet.Translate(content);

ClassicAssert.AreEqual(expected, result.translation);
}

[Test]
public void FuzzyMatch_ShouldMatchRestartByCorrectPinyin()
{
var settings = new Settings
{
ShouldUsePinyin = true
};
var matcher = new StringMatcher(new PinyinAlphabet(settings), settings);

var result = matcher.FuzzyMatch("chongqi", "重启");

ClassicAssert.True(result.Success);
}
}
}
Loading