From 87639db0b984a979e1eb8b74c593a450e2074ee8 Mon Sep 17 00:00:00 2001 From: VictoriousRaptor <10308169+VictoriousRaptor@users.noreply.github.com> Date: Mon, 3 Jun 2024 23:52:55 +0800 Subject: [PATCH] Test- simplified translation mapping --- .../PinyinAlphabet.cs | 26 ++++---- .../TranslationMapping.cs | 22 ++++++- Flow.Launcher.Test/PinyinTest.cs | 65 +++++++++++++++++++ 3 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 Flow.Launcher.Test/PinyinTest.cs diff --git a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs index 10799c676f1..d5b8c30d601 100644 --- a/Flow.Launcher.Infrastructure/PinyinAlphabet.cs +++ b/Flow.Launcher.Infrastructure/PinyinAlphabet.cs @@ -54,28 +54,28 @@ public bool ShouldTranslate(string stringToTranslate) var resultList = WordsHelper.GetPinyinList(content); StringBuilder resultBuilder = new StringBuilder(); - TranslationMapping map = new TranslationMapping(); - - bool pre = false; + TranslationMappingV2 map = new TranslationMappingV2(); for (int i = 0; i < resultList.Length; i++) { 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) + if (i > 0) { - pre = false; + map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); resultBuilder.Append(' '); + resultBuilder.Append(dp); } - + else + { + map.AddNewIndex(i, resultBuilder.Length, dp.Length); + resultBuilder.Append(dp); + } + } + else + { + map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length); resultBuilder.Append(resultList[i]); } } diff --git a/Flow.Launcher.Infrastructure/TranslationMapping.cs b/Flow.Launcher.Infrastructure/TranslationMapping.cs index c976fc522d6..e16b2bb52a0 100644 --- a/Flow.Launcher.Infrastructure/TranslationMapping.cs +++ b/Flow.Launcher.Infrastructure/TranslationMapping.cs @@ -12,7 +12,7 @@ public class TranslationMapping private List translatedIndexs = new List(); private int translatedLength = 0; - public void AddNewIndex(int originalIndex, int translatedIndex, int length) + public virtual void AddNewIndex(int originalIndex, int translatedIndex, int length) { if (constructed) throw new InvalidOperationException("Mapping shouldn't be changed after constructed"); @@ -23,7 +23,7 @@ public void AddNewIndex(int originalIndex, int translatedIndex, int length) translatedLength += length - 1; } - public int MapToOriginalIndex(int translatedIndex) + public virtual int MapToOriginalIndex(int translatedIndex) { if (translatedIndex > translatedIndexs.Last()) return translatedIndex - translatedLength - 1; @@ -89,4 +89,22 @@ public void endConstruct() constructed = true; } } + + public class TranslationMappingV2 : TranslationMapping + { + // Asssuming one original maps to multi translated + private List originalToTranslated = new(); + + public override void AddNewIndex(int originalIndex, int translatedIndex, int length) + { + originalToTranslated.Add(translatedIndex + length); + } + + public override int MapToOriginalIndex(int translatedIndex) + { + int loc = originalToTranslated.BinarySearch(translatedIndex); + + return loc > 0 ? loc : -loc - 1; + } + } } diff --git a/Flow.Launcher.Test/PinyinTest.cs b/Flow.Launcher.Test/PinyinTest.cs new file mode 100644 index 00000000000..3c572bf8d40 --- /dev/null +++ b/Flow.Launcher.Test/PinyinTest.cs @@ -0,0 +1,65 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Flow.Launcher.Infrastructure; +using Flow.Launcher.Infrastructure.UserSettings; +using NUnit.Framework; + +namespace Flow.Launcher.Test; + +[TestFixture] +internal class PinyinTest +{ + [Test] + public void TestShouldTranslate() + { + var pinyinAlphabet = new PinyinAlphabet(); + var settings = new Settings(); + pinyinAlphabet.Initialize(settings); + + settings.ShouldUsePinyin = true; + settings.UseDoublePinyin = false; + + Assert.IsTrue(pinyinAlphabet.ShouldTranslate("test")); + Assert.IsFalse(pinyinAlphabet.ShouldTranslate("测试")); + Assert.IsFalse(pinyinAlphabet.ShouldTranslate("测试test")); + + settings.UseDoublePinyin = true; + settings.ShouldUsePinyin = true; + + Assert.IsTrue(pinyinAlphabet.ShouldTranslate("test")); + Assert.IsFalse(pinyinAlphabet.ShouldTranslate("测试")); + Assert.IsFalse(pinyinAlphabet.ShouldTranslate("测试test")); + } + + [Test] + public void TestTranslate() + { + var pinyinAlphabet = new PinyinAlphabet(); + var settings = new Settings(); + pinyinAlphabet.Initialize(settings); + + settings.ShouldUsePinyin = true; + settings.UseDoublePinyin = false; + + var result = pinyinAlphabet.Translate("测试"); + Assert.AreEqual("Ce Shi", result.translation); + Assert.AreEqual(result.map.MapToOriginalIndex(0), 0); + Assert.AreEqual(result.map.MapToOriginalIndex(1), 0); + Assert.AreEqual(result.map.MapToOriginalIndex(3), 1); + + result = pinyinAlphabet.Translate("test"); + Assert.AreEqual("test", result.translation); + Assert.IsNull(result.map); + + result = pinyinAlphabet.Translate("test测试你"); + Assert.AreEqual("test Ce Shi Ni", result.translation); + Assert.AreEqual(result.map.MapToOriginalIndex(0), 0); + Assert.AreEqual(result.map.MapToOriginalIndex(2), 1); + Assert.AreEqual(result.map.MapToOriginalIndex(5), 4); + Assert.AreEqual(result.map.MapToOriginalIndex(8), 5); + Assert.AreEqual(result.map.MapToOriginalIndex(14), 6); + } +}