Skip to content

Commit 3dceb7b

Browse files
Merge pull request #1545 from VictoriousRaptor/RefactorPinyin
Refactor pinyin alphabet and fix #995
2 parents 31beb26 + ea81138 commit 3dceb7b

File tree

7 files changed

+73
-27116
lines changed

7 files changed

+73
-27116
lines changed

Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,5 @@
6464
<PackageReference Include="ToolGood.Words.Pinyin" Version="3.0.1.4" />
6565
</ItemGroup>
6666

67-
<ItemGroup>
68-
<None Update="pinyindb\pinyin_gwoyeu_mapping.xml">
69-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
70-
</None>
71-
<None Update="pinyindb\pinyin_mapping.xml">
72-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
73-
</None>
74-
<None Update="pinyindb\unicode_to_hanyu_pinyin.txt">
75-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
76-
</None>
77-
</ItemGroup>
7867

7968
</Project>

Flow.Launcher.Infrastructure/PinyinAlphabet.cs

Lines changed: 64 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class TranslationMapping
1515

1616
private List<int> originalIndexs = new List<int>();
1717
private List<int> translatedIndexs = new List<int>();
18-
private int translaedLength = 0;
18+
private int translatedLength = 0;
1919

2020
public string key { get; private set; }
2121

@@ -32,13 +32,13 @@ public void AddNewIndex(int originalIndex, int translatedIndex, int length)
3232
originalIndexs.Add(originalIndex);
3333
translatedIndexs.Add(translatedIndex);
3434
translatedIndexs.Add(translatedIndex + length);
35-
translaedLength += length - 1;
35+
translatedLength += length - 1;
3636
}
3737

3838
public int MapToOriginalIndex(int translatedIndex)
3939
{
4040
if (translatedIndex > translatedIndexs.Last())
41-
return translatedIndex - translaedLength - 1;
41+
return translatedIndex - translatedLength - 1;
4242

4343
int lowerBound = 0;
4444
int upperBound = originalIndexs.Count - 1;
@@ -83,7 +83,7 @@ public int MapToOriginalIndex(int translatedIndex)
8383
translatedIndex < translatedIndexs[upperBound * 2])
8484
{
8585
int indexDef = 0;
86-
86+
8787
for (int j = 0; j < upperBound; j++)
8888
{
8989
indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2];
@@ -102,9 +102,24 @@ public void endConstruct()
102102
}
103103
}
104104

105+
/// <summary>
106+
/// Translate a language to English letters using a given rule.
107+
/// </summary>
105108
public interface IAlphabet
106109
{
110+
/// <summary>
111+
/// Translate a string to English letters, using a given rule.
112+
/// </summary>
113+
/// <param name="stringToTranslate">String to translate.</param>
114+
/// <returns></returns>
107115
public (string translation, TranslationMapping map) Translate(string stringToTranslate);
116+
117+
/// <summary>
118+
/// Determine if a string can be translated to English letter with this Alphabet.
119+
/// </summary>
120+
/// <param name="stringToTranslate">String to translate.</param>
121+
/// <returns></returns>
122+
public bool CanBeTranslated(string stringToTranslate);
108123
}
109124

110125
public class PinyinAlphabet : IAlphabet
@@ -119,63 +134,70 @@ public void Initialize([NotNull] Settings settings)
119134
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
120135
}
121136

137+
public bool CanBeTranslated(string stringToTranslate)
138+
{
139+
return WordsHelper.HasChinese(stringToTranslate);
140+
}
141+
122142
public (string translation, TranslationMapping map) Translate(string content)
123143
{
124144
if (_settings.ShouldUsePinyin)
125145
{
126146
if (!_pinyinCache.ContainsKey(content))
127147
{
128-
if (WordsHelper.HasChinese(content))
129-
{
130-
var resultList = WordsHelper.GetPinyinList(content);
131-
132-
StringBuilder resultBuilder = new StringBuilder();
133-
TranslationMapping map = new TranslationMapping();
134-
135-
bool pre = false;
148+
return BuildCacheFromContent(content);
149+
}
150+
else
151+
{
152+
return _pinyinCache[content];
153+
}
154+
}
155+
return (content, null);
156+
}
136157

137-
for (int i = 0; i < resultList.Length; i++)
138-
{
139-
if (content[i] >= 0x3400 && content[i] <= 0x9FD5)
140-
{
141-
map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1);
142-
resultBuilder.Append(' ');
143-
resultBuilder.Append(resultList[i]);
144-
pre = true;
145-
}
146-
else
147-
{
148-
if (pre)
149-
{
150-
pre = false;
151-
resultBuilder.Append(' ');
152-
}
153-
154-
resultBuilder.Append(resultList[i]);
155-
}
156-
}
158+
private (string translation, TranslationMapping map) BuildCacheFromContent(string content)
159+
{
160+
if (WordsHelper.HasChinese(content))
161+
{
162+
var resultList = WordsHelper.GetPinyinList(content);
157163

158-
map.endConstruct();
164+
StringBuilder resultBuilder = new StringBuilder();
165+
TranslationMapping map = new TranslationMapping();
159166

160-
var key = resultBuilder.ToString();
161-
map.setKey(key);
167+
bool pre = false;
162168

163-
return _pinyinCache[content] = (key, map);
169+
for (int i = 0; i < resultList.Length; i++)
170+
{
171+
if (content[i] >= 0x3400 && content[i] <= 0x9FD5)
172+
{
173+
map.AddNewIndex(i, resultBuilder.Length, resultList[i].Length + 1);
174+
resultBuilder.Append(' ');
175+
resultBuilder.Append(resultList[i]);
176+
pre = true;
164177
}
165178
else
166179
{
167-
return (content, null);
180+
if (pre)
181+
{
182+
pre = false;
183+
resultBuilder.Append(' ');
184+
}
185+
186+
resultBuilder.Append(resultList[i]);
168187
}
169188
}
170-
else
171-
{
172-
return _pinyinCache[content];
173-
}
189+
190+
map.endConstruct();
191+
192+
var key = resultBuilder.ToString();
193+
map.setKey(key);
194+
195+
return _pinyinCache[content] = (key, map);
174196
}
175197
else
176198
{
177199
return (content, null);
178200
}
179201
}
180202
}
181-
}
203+
}

Flow.Launcher.Infrastructure/StringMatcher.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Flow.Launcher.Plugin.SharedModels;
1+
using Flow.Launcher.Plugin.SharedModels;
22
using System;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -60,8 +60,13 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
6060
return new MatchResult(false, UserSettingSearchPrecision);
6161

6262
query = query.Trim();
63-
TranslationMapping translationMapping;
64-
(stringToCompare, translationMapping) = _alphabet?.Translate(stringToCompare) ?? (stringToCompare, null);
63+
TranslationMapping translationMapping = null;
64+
if (_alphabet is not null && !_alphabet.CanBeTranslated(query))
65+
{
66+
// We assume that if a query can be translated (containing characters of a language, like Chinese)
67+
// it actually means user doesn't want it to be translated to English letters.
68+
(stringToCompare, translationMapping) = _alphabet.Translate(stringToCompare);
69+
}
6570

6671
var currentAcronymQueryIndex = 0;
6772
var acronymMatchData = new List<int>();

0 commit comments

Comments
 (0)