-
-
Notifications
You must be signed in to change notification settings - Fork 381
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
Open
VictoriousRaptor
wants to merge
45
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.
Open
Double pinyin query #2427
Changes from 38 commits
Commits
Show all changes
45 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 e07b33c
Merge branch 'dev' into double-pin
VictoriousRaptor 672649c
Merge branch 'dev' into double-pin
VictoriousRaptor 74d5499
Delete Flow.Launcher/Properties/Resources.fr-FR.resx
VictoriousRaptor 78ffeb8
Delete Flow.Launcher/Properties/Resources.he-IL.resx
VictoriousRaptor 3eb5fea
remove on tag deployment & change NuGet publish to on master push
jjw24 8f43de6
Support Msix FireFox bookmarks
Jack251970 281e042
Fix IsRelative logic.
Jack251970 ceb05e8
Add error handling for directory operation
Jack251970 aaa8e4d
Use AddRange
Jack251970 44304f2
Change code comments
Jack251970 16fd256
Fix typos
Jack251970 fba42ff
Fix transaltion logic
VictoriousRaptor b31a740
Simple refactor
VictoriousRaptor 818aac7
Use lookup table to translate full pinyin to double pinyin
VictoriousRaptor 31cd894
Compress json
VictoriousRaptor 3c2581d
Merge branch 'dev' into double-pin
VictoriousRaptor 4fb2e3d
Fix translation mapping logic
VictoriousRaptor 4b6231b
Extract methods for readability
VictoriousRaptor 64a3aa5
Fix Off-by-one in index mapping when consecutive Chinese chars
VictoriousRaptor b189595
Add OnPropertyChanged() for double pinyin properties
VictoriousRaptor d2dc307
Fix logic of ShouldTranslate()
VictoriousRaptor f064a81
Merge branch 'dev' into double-pin
VictoriousRaptor 1bc80d5
Fix translated length
VictoriousRaptor 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,141 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Collections.ObjectModel; | ||
using System.IO; | ||
using System.Text; | ||
using JetBrains.Annotations; | ||
using System.Text.Json; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Flow.Launcher.Infrastructure.UserSettings; | ||
using ToolGood.Words.Pinyin; | ||
using CommunityToolkit.Mvvm.DependencyInjection; | ||
using Flow.Launcher.Infrastructure.Logger; | ||
|
||
namespace Flow.Launcher.Infrastructure | ||
{ | ||
public class TranslationMapping | ||
public class PinyinAlphabet : IAlphabet | ||
{ | ||
private bool constructed; | ||
|
||
private List<int> originalIndexs = new List<int>(); | ||
private List<int> translatedIndexs = new List<int>(); | ||
private int translatedLength = 0; | ||
|
||
public string key { get; private set; } | ||
private readonly ConcurrentDictionary<string, (string translation, TranslationMapping map)> _pinyinCache = | ||
new(); | ||
|
||
public void setKey(string key) | ||
{ | ||
this.key = key; | ||
} | ||
private readonly Settings _settings; | ||
|
||
public void AddNewIndex(int originalIndex, int translatedIndex, int length) | ||
{ | ||
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; | ||
} | ||
private ReadOnlyDictionary<string, string> currentDoublePinyinTable; | ||
|
||
public int MapToOriginalIndex(int translatedIndex) | ||
public PinyinAlphabet() | ||
{ | ||
if (translatedIndex > translatedIndexs.Last()) | ||
return translatedIndex - translatedLength - 1; | ||
|
||
int lowerBound = 0; | ||
int upperBound = originalIndexs.Count - 1; | ||
|
||
int count = 0; | ||
_settings = Ioc.Default.GetRequiredService<Settings>(); | ||
LoadDoublePinyinTable(); | ||
|
||
// Corner case handle | ||
if (translatedIndex < translatedIndexs[0]) | ||
return translatedIndex; | ||
if (translatedIndex > translatedIndexs.Last()) | ||
_settings.PropertyChanged += (sender, e) => | ||
{ | ||
int indexDef = 0; | ||
for (int k = 0; k < originalIndexs.Count; k++) | ||
if (e.PropertyName == nameof(Settings.UseDoublePinyin) || | ||
e.PropertyName == nameof(Settings.DoublePinyinSchema)) | ||
{ | ||
indexDef += translatedIndexs[k * 2 + 1] - translatedIndexs[k * 2]; | ||
LoadDoublePinyinTable(); | ||
_pinyinCache.Clear(); | ||
} | ||
}; | ||
} | ||
|
||
return translatedIndex - indexDef - 1; | ||
} | ||
|
||
// Binary Search with Range | ||
for (int i = originalIndexs.Count / 2;; count++) | ||
private void LoadDoublePinyinTable() | ||
{ | ||
if (_settings.UseDoublePinyin) | ||
{ | ||
if (translatedIndex < translatedIndexs[i * 2]) | ||
{ | ||
// move to lower middle | ||
upperBound = i; | ||
i = (i + lowerBound) / 2; | ||
} | ||
else if (translatedIndex > translatedIndexs[i * 2 + 1] - 1) | ||
var tablePath = Path.Join(AppContext.BaseDirectory, "Resources", "double_pinyin.json"); | ||
try | ||
{ | ||
lowerBound = i; | ||
// move to upper middle | ||
// due to floor of integer division, move one up on corner case | ||
i = (i + upperBound + 1) / 2; | ||
} | ||
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++) | ||
using var fs = File.OpenRead(tablePath); | ||
Dictionary<string, Dictionary<string, string>> table = JsonSerializer.Deserialize<Dictionary<string, Dictionary<string, string>>>(fs); | ||
if (!table.TryGetValue(_settings.DoublePinyinSchema, out var value)) | ||
{ | ||
indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2]; | ||
throw new InvalidOperationException("DoublePinyinSchema is invalid."); | ||
} | ||
|
||
return translatedIndex - indexDef - 1; | ||
currentDoublePinyinTable = new ReadOnlyDictionary<string, string>(value); | ||
} | ||
catch (System.Exception e) | ||
{ | ||
Log.Exception(nameof(PinyinAlphabet), "Failed to load double pinyin table from file: " + tablePath, e); | ||
currentDoublePinyinTable = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()); | ||
} | ||
} | ||
else | ||
{ | ||
currentDoublePinyinTable = new ReadOnlyDictionary<string, string>(new Dictionary<string, string>()); | ||
} | ||
} | ||
|
||
public void endConstruct() | ||
public bool ShouldTranslate(string stringToTranslate) | ||
{ | ||
if (constructed) | ||
throw new InvalidOperationException("Mapping has already been constructed"); | ||
constructed = true; | ||
return _settings.UseDoublePinyin ? | ||
(!WordsHelper.HasChinese(stringToTranslate) && stringToTranslate.Length % 2 == 0) : | ||
!WordsHelper.HasChinese(stringToTranslate); | ||
} | ||
} | ||
|
||
/// <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)>(); | ||
|
||
private Settings _settings; | ||
|
||
public PinyinAlphabet() | ||
{ | ||
Initialize(Ioc.Default.GetRequiredService<Settings>()); | ||
} | ||
|
||
private void Initialize([NotNull] Settings settings) | ||
public (string translation, TranslationMapping map) Translate(string content) | ||
{ | ||
_settings = settings ?? throw new ArgumentNullException(nameof(settings)); | ||
} | ||
if (!_settings.ShouldUsePinyin) | ||
return (content, null); | ||
|
||
public bool CanBeTranslated(string stringToTranslate) | ||
{ | ||
return WordsHelper.HasChinese(stringToTranslate); | ||
return _pinyinCache.TryGetValue(content, out var value) | ||
? value | ||
: BuildCacheFromContent(content); | ||
} | ||
|
||
public (string translation, TranslationMapping map) Translate(string content) | ||
private (string translation, TranslationMapping map) BuildCacheFromContent(string content) | ||
{ | ||
if (_settings.ShouldUsePinyin) | ||
if (!WordsHelper.HasChinese(content)) | ||
{ | ||
if (!_pinyinCache.ContainsKey(content)) | ||
{ | ||
return BuildCacheFromContent(content); | ||
} | ||
else | ||
{ | ||
return _pinyinCache[content]; | ||
} | ||
return (content, null); | ||
} | ||
return (content, null); | ||
} | ||
|
||
private (string translation, TranslationMapping map) BuildCacheFromContent(string content) | ||
{ | ||
if (WordsHelper.HasChinese(content)) | ||
{ | ||
var resultList = WordsHelper.GetPinyinList(content); | ||
var resultList = WordsHelper.GetPinyinList(content); | ||
|
||
StringBuilder resultBuilder = new StringBuilder(); | ||
TranslationMapping map = new TranslationMapping(); | ||
var resultBuilder = new StringBuilder(); | ||
var map = new TranslationMapping(); | ||
|
||
bool pre = false; | ||
var previousIsChinese = false; | ||
|
||
for (int i = 0; i < resultList.Length; i++) | ||
for (var i = 0; i < resultList.Length; i++) | ||
{ | ||
if (content[i] >= 0x3400 && content[i] <= 0x9FD5) | ||
{ | ||
if (content[i] >= 0x3400 && content[i] <= 0x9FD5) | ||
string dp = _settings.UseDoublePinyin ? ToDoublePin(resultList[i]) : resultList[i]; | ||
map.AddNewIndex(i, resultBuilder.Length, dp.Length + 1); | ||
if (previousIsChinese) | ||
{ | ||
map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1); | ||
resultBuilder.Append(' '); | ||
resultBuilder.Append(resultList[i]); | ||
pre = true; | ||
} | ||
else | ||
resultBuilder.Append(dp); | ||
} | ||
else | ||
{ | ||
if (previousIsChinese) | ||
{ | ||
if (pre) | ||
{ | ||
pre = false; | ||
resultBuilder.Append(' '); | ||
} | ||
|
||
resultBuilder.Append(resultList[i]); | ||
previousIsChinese = false; | ||
resultBuilder.Append(' '); | ||
} | ||
resultBuilder.Append(resultList[i]); | ||
} | ||
} | ||
|
||
map.endConstruct(); | ||
map.endConstruct(); | ||
|
||
var key = resultBuilder.ToString(); | ||
map.setKey(key); | ||
var key = resultBuilder.ToString(); | ||
|
||
return _pinyinCache[content] = (key, map); | ||
} | ||
else | ||
return _pinyinCache[content] = (key, map); | ||
} | ||
|
||
#region Double Pinyin | ||
|
||
private string ToDoublePin(string fullPinyin) | ||
{ | ||
if (currentDoublePinyinTable.TryGetValue(fullPinyin, out var doublePinyinValue)) | ||
{ | ||
return (content, null); | ||
return doublePinyinValue; | ||
} | ||
return fullPinyin; | ||
} | ||
|
||
#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.