-
-
Notifications
You must be signed in to change notification settings - Fork 377
Double pinyin query #2427
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
Draft
VictoriousRaptor
wants to merge
22
commits into
dev
Choose a base branch
from
double-pin
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−149
Draft
Double pinyin query #2427
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3abd05f
Implement double pinyin
VictoriousRaptor fb66353
Test double pinyin
VictoriousRaptor f6ae71a
Only convert to double pinyin when meeting Chinese
VictoriousRaptor e5285b1
Temp: compatibility with full pinyin option
VictoriousRaptor 99ff3b2
Fix wrong condition
VictoriousRaptor 46d49d8
Only translate when string is double pinyin
VictoriousRaptor b1cb852
Extract classes
VictoriousRaptor 6807afb
Remove unused alphabet arg in PublicAPIInstance
VictoriousRaptor a2efa11
Merge DoublePinAlphabet logic
VictoriousRaptor 12c4e37
Developing
VictoriousRaptor f673000
Fix ShouldTranslate()
VictoriousRaptor b10a6e1
Capitalize first letter
VictoriousRaptor b816d1b
Remove unused key in pinyin alphabet
VictoriousRaptor c8a9e5e
Merge branch 'dev' into double-pin
Jack251970 9e8a950
Fix build issue & Improve code quality
Jack251970 a8a305f
Improve code quality
Jack251970 5be732d
Use var when neccessary
Jack251970 1f458d3
Fix typos & Code quality
Jack251970 3f45c6a
Merge branch 'dev' into double-pin
Jack251970 4b7db3c
Make function static
Jack251970 f5fd6b5
Use ReadOnlySpan instead
Jack251970 2cf6f81
Merge branch 'dev' into double-pin
Jack251970 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Flow.Launcher.Infrastructure | ||
{ | ||
/// <summary> | ||
/// Translate a language to English letters using a given rule. | ||
/// </summary> | ||
public interface IAlphabet | ||
{ | ||
/// <summary> | ||
/// Translate a string to English letters, using a given rule. | ||
/// </summary> | ||
/// <param name="stringToTranslate">String to translate.</param> | ||
/// <returns></returns> | ||
public (string translation, TranslationMapping map) Translate(string stringToTranslate); | ||
|
||
/// <summary> | ||
/// Determine if a string can be translated to English letter with this Alphabet. | ||
/// </summary> | ||
/// <param name="stringToTranslate">String to translate.</param> | ||
/// <returns></returns> | ||
public bool ShouldTranslate(string stringToTranslate); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,209 +1,194 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Collections.ObjectModel; | ||
using System.Text; | ||
using JetBrains.Annotations; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Flow.Launcher.Infrastructure.UserSettings; | ||
using ToolGood.Words.Pinyin; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
|
||
namespace Flow.Launcher.Infrastructure | ||
{ | ||
public class TranslationMapping | ||
public class PinyinAlphabet : IAlphabet | ||
{ | ||
private bool constructed; | ||
private readonly ConcurrentDictionary<string, (string translation, TranslationMapping map)> _pinyinCache = | ||
new(); | ||
|
||
private List<int> originalIndexs = new List<int>(); | ||
private List<int> translatedIndexs = new List<int>(); | ||
private int translatedLength = 0; | ||
private readonly Settings _settings; | ||
|
||
public string key { get; private set; } | ||
|
||
public void setKey(string key) | ||
public PinyinAlphabet() | ||
{ | ||
this.key = key; | ||
_settings = Ioc.Default.GetRequiredService<Settings>(); | ||
} | ||
|
||
public void AddNewIndex(int originalIndex, int translatedIndex, int length) | ||
public bool ShouldTranslate(string stringToTranslate) | ||
{ | ||
if (constructed) | ||
throw new InvalidOperationException("Mapping shouldn't be changed after constructed"); | ||
|
||
originalIndexs.Add(originalIndex); | ||
translatedIndexs.Add(translatedIndex); | ||
translatedIndexs.Add(translatedIndex + length); | ||
translatedLength += length - 1; | ||
return _settings.UseDoublePinyin ? | ||
(!WordsHelper.HasChinese(stringToTranslate) && stringToTranslate.Length % 2 == 0) : | ||
!WordsHelper.HasChinese(stringToTranslate); | ||
} | ||
|
||
public int MapToOriginalIndex(int translatedIndex) | ||
public (string translation, TranslationMapping map) Translate(string content) | ||
{ | ||
if (translatedIndex > translatedIndexs.Last()) | ||
return translatedIndex - translatedLength - 1; | ||
|
||
int lowerBound = 0; | ||
int upperBound = originalIndexs.Count - 1; | ||
|
||
int count = 0; | ||
|
||
// Corner case handle | ||
if (translatedIndex < translatedIndexs[0]) | ||
return translatedIndex; | ||
if (translatedIndex > translatedIndexs.Last()) | ||
if (_settings.ShouldUsePinyin) | ||
{ | ||
int indexDef = 0; | ||
for (int k = 0; k < originalIndexs.Count; k++) | ||
if (!_pinyinCache.TryGetValue(content, out var value)) | ||
{ | ||
return BuildCacheFromContent(content); | ||
} | ||
else | ||
{ | ||
indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2]; | ||
return value; | ||
} | ||
} | ||
return (content, null); | ||
} | ||
|
||
return translatedIndex - indexDef - 1; | ||
private (string translation, TranslationMapping map) BuildCacheFromContent(string content) | ||
{ | ||
if (!WordsHelper.HasChinese(content)) | ||
{ | ||
return (content, null); | ||
} | ||
|
||
// Binary Search with Range | ||
for (int i = originalIndexs.Count / 2;; count++) | ||
var resultList = WordsHelper.GetPinyinList(content); | ||
|
||
var resultBuilder = new StringBuilder(); | ||
var map = new TranslationMapping(); | ||
|
||
var pre = false; | ||
|
||
for (var i = 0; i < resultList.Length; i++) | ||
{ | ||
if (translatedIndex < translatedIndexs[i * 2]) | ||
if (content[i] >= 0x3400 && content[i] <= 0x9FD5) | ||
{ | ||
// move to lower middle | ||
upperBound = i; | ||
i = (i + lowerBound) / 2; | ||
} | ||
else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1) | ||
{ | ||
lowerBound = i; | ||
// move to upper middle | ||
// due to floor of integer division, move one up on corner case | ||
i = (i + upperBound + 1) / 2; | ||
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 | ||
return originalIndexs[i]; | ||
|
||
if (upperBound - lowerBound <= 1 && | ||
translatedIndex > translatedIndexs[lowerBound * 2 + 1] && | ||
translatedIndex < translatedIndexs[upperBound * 2]) | ||
{ | ||
int indexDef = 0; | ||
|
||
for (int j = 0; j < upperBound; j++) | ||
if (pre) | ||
{ | ||
indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2]; | ||
pre = false; | ||
resultBuilder.Append(' '); | ||
} | ||
|
||
return translatedIndex - indexDef - 1; | ||
resultBuilder.Append(resultList[i]); | ||
} | ||
} | ||
} | ||
|
||
public void endConstruct() | ||
{ | ||
if (constructed) | ||
throw new InvalidOperationException("Mapping has already been constructed"); | ||
constructed = true; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Translate a language to English letters using a given rule. | ||
/// </summary> | ||
public interface IAlphabet | ||
{ | ||
/// <summary> | ||
/// Translate a string to English letters, using a given rule. | ||
/// </summary> | ||
/// <param name="stringToTranslate">String to translate.</param> | ||
/// <returns></returns> | ||
public (string translation, TranslationMapping map) Translate(string stringToTranslate); | ||
|
||
/// <summary> | ||
/// Determine if a string can be translated to English letter with this Alphabet. | ||
/// </summary> | ||
/// <param name="stringToTranslate">String to translate.</param> | ||
/// <returns></returns> | ||
public bool CanBeTranslated(string stringToTranslate); | ||
} | ||
|
||
public class PinyinAlphabet : IAlphabet | ||
{ | ||
private ConcurrentDictionary<string, (string translation, TranslationMapping map)> _pinyinCache = | ||
new ConcurrentDictionary<string, (string translation, TranslationMapping map)>(); | ||
map.endConstruct(); | ||
|
||
private Settings _settings; | ||
var key = resultBuilder.ToString(); | ||
|
||
public PinyinAlphabet() | ||
{ | ||
Initialize(Ioc.Default.GetRequiredService<Settings>()); | ||
return _pinyinCache[content] = (key, map); | ||
} | ||
|
||
private void Initialize([NotNull] Settings settings) | ||
#region Double Pinyin | ||
|
||
private static readonly ReadOnlyDictionary<string, string> special = new(new Dictionary<string, string>(){ | ||
{"A", "aa"}, | ||
{"Ai", "ai"}, | ||
{"An", "an"}, | ||
{"Ang", "ah"}, | ||
{"Ao", "ao"}, | ||
{"E", "ee"}, | ||
{"Ei", "ei"}, | ||
{"En", "en"}, | ||
{"Er", "er"}, | ||
{"O", "oo"}, | ||
{"Ou", "ou"} | ||
}); | ||
|
||
private static readonly ReadOnlyDictionary<string, string> first = new(new Dictionary<string, string>(){ | ||
{"Ch", "i"}, | ||
{"Sh", "u"}, | ||
{"Zh", "v"} | ||
}); | ||
|
||
private static readonly ReadOnlyDictionary<string, string> second = new(new Dictionary<string, string>() | ||
{ | ||
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
} | ||
|
||
public bool CanBeTranslated(string stringToTranslate) | ||
{"ua", "x"}, | ||
{"ei", "w"}, | ||
{"e", "e"}, | ||
{"ou", "z"}, | ||
{"iu", "q"}, | ||
{"ve", "t"}, | ||
{"ue", "t"}, | ||
{"u", "u"}, | ||
{"i", "i"}, | ||
{"o", "o"}, | ||
{"uo", "o"}, | ||
{"ie", "p"}, | ||
{"a", "a"}, | ||
{"ong", "s"}, | ||
{"iong", "s"}, | ||
{"ai", "d"}, | ||
{"ing", "k"}, | ||
{"uai", "k"}, | ||
{"ang", "h"}, | ||
{"uan", "r"}, | ||
{"an", "j"}, | ||
{"en", "f"}, | ||
{"ia", "x"}, | ||
{"iang", "l"}, | ||
{"uang", "l"}, | ||
{"eng", "g"}, | ||
{"in", "b"}, | ||
{"ao", "c"}, | ||
{"v", "v"}, | ||
{"ui", "v"}, | ||
{"un", "y"}, | ||
{"iao", "n"}, | ||
{"ian", "m"} | ||
}); | ||
|
||
private static string ToDoublePin(string fullPinyin) | ||
{ | ||
return WordsHelper.HasChinese(stringToTranslate); | ||
} | ||
// Assuming s is valid | ||
var doublePin = new StringBuilder(); | ||
|
||
public (string translation, TranslationMapping map) Translate(string content) | ||
{ | ||
if (_settings.ShouldUsePinyin) | ||
if (fullPinyin.Length <= 3 && (fullPinyin[0] == 'a' || fullPinyin[0] == 'e' || fullPinyin[0] == 'o')) | ||
{ | ||
if (!_pinyinCache.ContainsKey(content)) | ||
{ | ||
return BuildCacheFromContent(content); | ||
} | ||
else | ||
if (special.TryGetValue(fullPinyin, out var value)) | ||
{ | ||
return _pinyinCache[content]; | ||
return value; | ||
} | ||
} | ||
return (content, null); | ||
} | ||
|
||
private (string translation, TranslationMapping map) BuildCacheFromContent(string content) | ||
{ | ||
if (WordsHelper.HasChinese(content)) | ||
// zh, ch, sh | ||
if (fullPinyin.Length >= 2 && first.ContainsKey(fullPinyin[..2])) | ||
{ | ||
var resultList = WordsHelper.GetPinyinList(content); | ||
|
||
StringBuilder resultBuilder = new StringBuilder(); | ||
TranslationMapping map = new TranslationMapping(); | ||
|
||
bool pre = false; | ||
doublePin.Append(first[fullPinyin[..2]]); | ||
|
||
for (int i = 0; i < resultList.Length; i++) | ||
if (second.TryGetValue(fullPinyin[2..], out var tmp)) | ||
{ | ||
if (content[i] >= 0x3400 && content[i] <= 0x9FD5) | ||
{ | ||
map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1); | ||
resultBuilder.Append(' '); | ||
resultBuilder.Append(resultList[i]); | ||
pre = true; | ||
} | ||
else | ||
{ | ||
if (pre) | ||
{ | ||
pre = false; | ||
resultBuilder.Append(' '); | ||
} | ||
|
||
resultBuilder.Append(resultList[i]); | ||
} | ||
doublePin.Append(tmp); | ||
} | ||
else | ||
{ | ||
doublePin.Append(fullPinyin[2..]); | ||
} | ||
|
||
map.endConstruct(); | ||
|
||
var key = resultBuilder.ToString(); | ||
map.setKey(key); | ||
|
||
return _pinyinCache[content] = (key, map); | ||
} | ||
else | ||
{ | ||
return (content, null); | ||
doublePin.Append(fullPinyin[0]); | ||
|
||
if (second.TryGetValue(fullPinyin[1..], out var tmp)) | ||
{ | ||
doublePin.Append(tmp); | ||
} | ||
else | ||
{ | ||
doublePin.Append(fullPinyin[1..]); | ||
} | ||
} | ||
|
||
return doublePin.ToString(); | ||
} | ||
|
||
#endregion | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.