2016-04-23 23:37:25 +00:00
|
|
|
|
using System;
|
2017-01-12 20:46:40 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2016-04-23 23:37:25 +00:00
|
|
|
|
using System.Linq;
|
2020-01-19 23:06:16 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
using JetBrains.Annotations;
|
2020-04-21 09:12:17 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
2020-10-15 13:06:57 +00:00
|
|
|
|
using ToolGood.Words.Pinyin;
|
2016-04-23 23:37:25 +00:00
|
|
|
|
|
2020-04-21 09:12:17 +00:00
|
|
|
|
namespace Flow.Launcher.Infrastructure
|
2016-04-23 23:37:25 +00:00
|
|
|
|
{
|
2020-01-19 23:06:16 +00:00
|
|
|
|
public interface IAlphabet
|
2016-04-23 23:37:25 +00:00
|
|
|
|
{
|
2020-01-19 23:06:16 +00:00
|
|
|
|
string Translate(string stringToTranslate);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-01 10:07:35 +00:00
|
|
|
|
public class PinyinAlphabet : IAlphabet
|
2020-01-19 23:06:16 +00:00
|
|
|
|
{
|
2020-10-25 02:26:56 +00:00
|
|
|
|
private ConcurrentDictionary<string, string> _pinyinCache = new ConcurrentDictionary<string, string>();
|
2020-01-19 23:06:16 +00:00
|
|
|
|
private Settings _settings;
|
2020-10-15 13:06:57 +00:00
|
|
|
|
|
2020-01-19 23:06:16 +00:00
|
|
|
|
public void Initialize([NotNull] Settings settings)
|
2019-12-03 13:55:22 +00:00
|
|
|
|
{
|
2020-01-19 23:06:16 +00:00
|
|
|
|
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
|
2017-01-12 02:16:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-15 13:06:57 +00:00
|
|
|
|
public string Translate(string content)
|
2016-04-23 23:37:25 +00:00
|
|
|
|
{
|
2020-10-15 13:06:57 +00:00
|
|
|
|
if (_settings.ShouldUsePinyin)
|
2019-12-03 13:55:22 +00:00
|
|
|
|
{
|
2020-10-19 12:37:40 +00:00
|
|
|
|
if (!_pinyinCache.ContainsKey(content))
|
2019-12-03 13:55:22 +00:00
|
|
|
|
{
|
2020-10-15 13:06:57 +00:00
|
|
|
|
if (WordsHelper.HasChinese(content))
|
2017-01-12 20:46:40 +00:00
|
|
|
|
{
|
2020-12-11 14:08:46 +00:00
|
|
|
|
var resultList = WordsHelper.GetPinyinList(content);
|
|
|
|
|
|
|
|
|
|
|
|
StringBuilder resultBuilder = new StringBuilder();
|
2020-12-11 14:20:09 +00:00
|
|
|
|
|
2020-12-12 06:55:41 +00:00
|
|
|
|
for (int i = 0; i < resultList.Length; i++)
|
2020-12-11 14:20:09 +00:00
|
|
|
|
{
|
2020-12-12 06:55:41 +00:00
|
|
|
|
if (content[i] >= 0x3400 && content[i] <= 0x9FD5)
|
|
|
|
|
|
resultBuilder.Append(resultList[i].First());
|
2020-12-11 14:20:09 +00:00
|
|
|
|
}
|
2020-12-12 06:55:41 +00:00
|
|
|
|
|
2020-12-11 14:08:46 +00:00
|
|
|
|
resultBuilder.Append(' ');
|
|
|
|
|
|
|
2020-12-12 06:55:41 +00:00
|
|
|
|
bool pre = false;
|
|
|
|
|
|
|
2020-12-11 14:08:46 +00:00
|
|
|
|
for (int i = 0; i < resultList.Length; i++)
|
|
|
|
|
|
{
|
2020-12-12 06:55:41 +00:00
|
|
|
|
if (content[i] >= 0x3400 && content[i] <= 0x9FD5)
|
2020-12-11 14:08:46 +00:00
|
|
|
|
{
|
|
|
|
|
|
resultBuilder.Append(' ');
|
|
|
|
|
|
resultBuilder.Append(resultList[i]);
|
2020-12-12 06:55:41 +00:00
|
|
|
|
pre = true;
|
2020-12-11 14:08:46 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-12-12 06:55:41 +00:00
|
|
|
|
if (pre)
|
2020-12-11 14:08:46 +00:00
|
|
|
|
{
|
2020-12-12 06:55:41 +00:00
|
|
|
|
pre = false;
|
2020-12-11 14:08:46 +00:00
|
|
|
|
resultBuilder.Append(' ');
|
|
|
|
|
|
}
|
|
|
|
|
|
resultBuilder.Append(resultList[i]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return _pinyinCache[content] = resultBuilder.ToString();
|
2019-12-03 13:55:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-10-18 03:46:56 +00:00
|
|
|
|
return content;
|
2017-01-12 20:46:40 +00:00
|
|
|
|
}
|
2017-01-12 02:16:53 +00:00
|
|
|
|
}
|
2020-10-18 03:48:17 +00:00
|
|
|
|
else
|
2020-10-19 12:40:22 +00:00
|
|
|
|
{
|
2020-10-18 03:48:17 +00:00
|
|
|
|
return _pinyinCache[content];
|
2020-10-19 12:40:22 +00:00
|
|
|
|
}
|
2017-01-12 02:16:53 +00:00
|
|
|
|
}
|
2017-01-12 20:46:40 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
2020-10-15 13:06:57 +00:00
|
|
|
|
return content;
|
2017-01-12 20:46:40 +00:00
|
|
|
|
}
|
2016-04-23 23:37:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-15 13:06:57 +00:00
|
|
|
|
}
|