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

Allow the use of Chinese character search, refactor pinyin alphabet and bugfix #1545

Merged
merged 4 commits into from
Nov 20, 2022
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
11 changes: 0 additions & 11 deletions Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,5 @@
<PackageReference Include="ToolGood.Words.Pinyin" Version="3.0.1.4" />
</ItemGroup>

<ItemGroup>
<None Update="pinyindb\pinyin_gwoyeu_mapping.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="pinyindb\pinyin_mapping.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="pinyindb\unicode_to_hanyu_pinyin.txt">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
106 changes: 64 additions & 42 deletions Flow.Launcher.Infrastructure/PinyinAlphabet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class TranslationMapping

private List<int> originalIndexs = new List<int>();
private List<int> translatedIndexs = new List<int>();
private int translaedLength = 0;
private int translatedLength = 0;

public string key { get; private set; }

Expand All @@ -32,13 +32,13 @@ public void AddNewIndex(int originalIndex, int translatedIndex, int length)
originalIndexs.Add(originalIndex);
translatedIndexs.Add(translatedIndex);
translatedIndexs.Add(translatedIndex + length);
translaedLength += length - 1;
translatedLength += length - 1;
}

public int MapToOriginalIndex(int translatedIndex)
{
if (translatedIndex > translatedIndexs.Last())
return translatedIndex - translaedLength - 1;
return translatedIndex - translatedLength - 1;

int lowerBound = 0;
int upperBound = originalIndexs.Count - 1;
Expand Down Expand Up @@ -83,7 +83,7 @@ public int MapToOriginalIndex(int translatedIndex)
translatedIndex < translatedIndexs[upperBound * 2])
{
int indexDef = 0;

for (int j = 0; j < upperBound; j++)
{
indexDef += translatedIndexs[j * 2 + 1] - translatedIndexs[j * 2];
Expand All @@ -102,9 +102,24 @@ public void endConstruct()
}
}

/// <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
Expand All @@ -119,63 +134,70 @@ public void Initialize([NotNull] Settings settings)
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}

public bool CanBeTranslated(string stringToTranslate)
{
return WordsHelper.HasChinese(stringToTranslate);
}

public (string translation, TranslationMapping map) Translate(string content)
{
if (_settings.ShouldUsePinyin)
{
if (!_pinyinCache.ContainsKey(content))
{
if (WordsHelper.HasChinese(content))
{
var resultList = WordsHelper.GetPinyinList(content);

StringBuilder resultBuilder = new StringBuilder();
TranslationMapping map = new TranslationMapping();

bool pre = false;
return BuildCacheFromContent(content);
}
else
{
return _pinyinCache[content];
}
}
return (content, null);
}

for (int i = 0; i < resultList.Length; i++)
{
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]);
}
}
private (string translation, TranslationMapping map) BuildCacheFromContent(string content)
{
if (WordsHelper.HasChinese(content))
{
var resultList = WordsHelper.GetPinyinList(content);

map.endConstruct();
StringBuilder resultBuilder = new StringBuilder();
TranslationMapping map = new TranslationMapping();

var key = resultBuilder.ToString();
map.setKey(key);
bool pre = false;

return _pinyinCache[content] = (key, map);
for (int i = 0; i < resultList.Length; i++)
{
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
{
return (content, null);
if (pre)
{
pre = false;
resultBuilder.Append(' ');
}

resultBuilder.Append(resultList[i]);
}
}
else
{
return _pinyinCache[content];
}

map.endConstruct();

var key = resultBuilder.ToString();
map.setKey(key);

return _pinyinCache[content] = (key, map);
}
else
{
return (content, null);
}
}
}
}
}
11 changes: 8 additions & 3 deletions Flow.Launcher.Infrastructure/StringMatcher.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Flow.Launcher.Plugin.SharedModels;
using Flow.Launcher.Plugin.SharedModels;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -60,8 +60,13 @@ public MatchResult FuzzyMatch(string query, string stringToCompare, MatchOption
return new MatchResult(false, UserSettingSearchPrecision);

query = query.Trim();
TranslationMapping translationMapping;
(stringToCompare, translationMapping) = _alphabet?.Translate(stringToCompare) ?? (stringToCompare, null);
TranslationMapping translationMapping = null;
if (_alphabet is not null && !_alphabet.CanBeTranslated(query))
taooceros marked this conversation as resolved.
Show resolved Hide resolved
{
// We assume that if a query can be translated (containing characters of a language, like Chinese)
// it actually means user doesn't want it to be translated to English letters.
(stringToCompare, translationMapping) = _alphabet.Translate(stringToCompare);
}

var currentAcronymQueryIndex = 0;
var acronymMatchData = new List<int>();
Expand Down
Loading