Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test- simplified translation mapping #2753

Draft
wants to merge 1 commit into
base: double-pin
Choose a base branch
from
Draft
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
26 changes: 13 additions & 13 deletions Flow.Launcher.Infrastructure/PinyinAlphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
22 changes: 20 additions & 2 deletions Flow.Launcher.Infrastructure/TranslationMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class TranslationMapping
private List<int> translatedIndexs = new List<int>();
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");
Expand All @@ -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;
Expand Down Expand Up @@ -89,4 +89,22 @@ public void endConstruct()
constructed = true;
}
}

public class TranslationMappingV2 : TranslationMapping
{
// Asssuming one original maps to multi translated
private List<int> 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;
}
}
}
65 changes: 65 additions & 0 deletions Flow.Launcher.Test/PinyinTest.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading