Skip to content
Merged
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
27 changes: 15 additions & 12 deletions Flow.Launcher.Infrastructure/PinyinAlphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public PinyinAlphabet()
{
switch (e.PropertyName)
{
case nameof (Settings.ShouldUsePinyin):
case nameof(Settings.ShouldUsePinyin):
if (_settings.ShouldUsePinyin)
{
Reload();
Expand All @@ -52,7 +52,7 @@ public void Reload()

private void CreateDoublePinyinTableFromStream(Stream jsonStream)
{
var table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream) ??
var table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonStream) ??
throw new InvalidOperationException("Failed to deserialize double pinyin table: result is null");

var schemaKey = _settings.DoublePinyinSchema.ToString();
Expand Down Expand Up @@ -128,12 +128,12 @@ public bool ShouldTranslate(string stringToTranslate)
if (IsChineseCharacter(content[i]))
{
var translated = _settings.UseDoublePinyin ? ToDoublePinyin(resultList[i]) : resultList[i];
if (i > 0)

if (i > 0 && content[i - 1] != ' ')
{
resultBuilder.Append(' ');
}

map.AddNewIndex(resultBuilder.Length, translated.Length);
resultBuilder.Append(translated);
previousIsChinese = true;
Expand All @@ -144,19 +144,22 @@ public bool ShouldTranslate(string stringToTranslate)
if (previousIsChinese)
{
previousIsChinese = false;
resultBuilder.Append(' ');
if (content[i] != ' ')
{
resultBuilder.Append(' ');
}
}
map.AddNewIndex(resultBuilder.Length, resultList[i].Length);
resultBuilder.Append(resultList[i]);

map.AddNewIndex(resultBuilder.Length, 1);
resultBuilder.Append(content[i]);
}
}

map.EndConstruct();

var translation = resultBuilder.ToString();
var result = (translation, map);

return _pinyinCache[content] = result;
}

Expand Down Expand Up @@ -185,8 +188,8 @@ private static bool IsChineseCharacter(char c)

private string ToDoublePinyin(string fullPinyin)
{
return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)
? doublePinyinValue
return currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)
? doublePinyinValue
: fullPinyin;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Flow.Launcher.Infrastructure/TranslationMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public void AddNewIndex(int translatedIndex, int length)
public int MapToOriginalIndex(int translatedIndex)
{
var searchResult = _originalToTranslated.BinarySearch(translatedIndex);
return searchResult >= 0 ? searchResult : ~searchResult;
return searchResult >= 0 ? searchResult + 1 : ~searchResult;
}

public void EndConstruct()
Expand Down
34 changes: 24 additions & 10 deletions Flow.Launcher.Test/TranslationMappingTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Reflection;
using Flow.Launcher.Infrastructure;
using NUnit.Framework;

Check warning on line 4 in Flow.Launcher.Test/TranslationMappingTest.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`NUnit` is not a recognized word. (unrecognized-spelling)
using NUnit.Framework.Legacy;

Check warning on line 5 in Flow.Launcher.Test/TranslationMappingTest.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`NUnit` is not a recognized word. (unrecognized-spelling)

Check warning on line 5 in Flow.Launcher.Test/TranslationMappingTest.cs

View workflow job for this annotation

GitHub Actions / Check Spelling

`NUnit` is not a recognized word. (unrecognized-spelling)

namespace Flow.Launcher.Test
{
Expand All @@ -22,19 +22,33 @@
ClassicAssert.AreEqual(10, GetOriginalToTranslatedAt(mapping, 1));
}

[TestCase(0, 0)]
[TestCase(2, 1)]
[TestCase(3, 1)]
[TestCase(5, 2)]
[TestCase(6, 2)]

[TestCase(0, 0)] // "F" -> "F"
[TestCase(1, 1)] // "l" -> "l"
[TestCase(2, 2)] // "o" -> "o"
[TestCase(3, 3)] // "w" -> "w"
[TestCase(4, 4)] // " " -> " "
[TestCase(5, 5)] // "Y" (translated from "用") -> original index 5
[TestCase(6, 5)] // "o" (translated from "用") -> original index 5
[TestCase(7, 5)] // "n" (translated from "用") -> original index 5
[TestCase(8, 5)] // "g" (translated from "用") -> original index 5
[TestCase(10, 6)] // "H" (translated from "户") -> original index 6
[TestCase(11, 6)] // "u" (translated from "户") -> original index 6
public void MapToOriginalIndex_ShouldReturnExpectedIndex(int translatedIndex, int expectedOriginalIndex)
{
var mapping = new TranslationMapping();
// a测试
// a Ce Shi
mapping.AddNewIndex(0, 1);
mapping.AddNewIndex(2, 2);
mapping.AddNewIndex(5, 3);
// Test case :
// 0123456
// Flow 用户
// 012345678901
// Flow Yong Hu
mapping.AddNewIndex(0, 1); // F
mapping.AddNewIndex(1, 1); // l
mapping.AddNewIndex(2, 1); // o
mapping.AddNewIndex(3, 1); // w
mapping.AddNewIndex(4, 1); // ' '
mapping.AddNewIndex(5, 4); // 用 -> Yong
mapping.AddNewIndex(10, 2); // 户 -> Hu

var result = mapping.MapToOriginalIndex(translatedIndex);
ClassicAssert.AreEqual(expectedOriginalIndex, result);
Expand Down
Loading