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.Collections.Generic;
|
|
|
|
|
|
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.Logger;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.Storage;
|
|
|
|
|
|
using Flow.Launcher.Infrastructure.UserSettings;
|
2020-10-15 13:06:57 +00:00
|
|
|
|
using ToolGood.Words.Pinyin;
|
|
|
|
|
|
using System.Threading.Tasks;
|
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
|
|
|
|
}
|
|
|
|
|
|
|
2019-11-15 22:34:27 +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-10-18 03:48:17 +00:00
|
|
|
|
var result = WordsHelper.GetPinyin(content, ";");
|
|
|
|
|
|
result = GetFirstPinyinChar(result) + result.Replace(";", "");
|
2020-10-15 13:06:57 +00:00
|
|
|
|
_pinyinCache[content] = result;
|
2020-10-18 03:46:56 +00:00
|
|
|
|
return result;
|
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
|
|
|
|
private string GetFirstPinyinChar(string content)
|
2016-04-23 23:37:25 +00:00
|
|
|
|
{
|
2020-10-17 06:17:53 +00:00
|
|
|
|
return string.Concat(content.Split(';').Select(x => x.First()));
|
2016-04-23 23:37:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-10-15 13:06:57 +00:00
|
|
|
|
}
|