Merge branch 'dev' into fix_plugin_depenedency_loading

This commit is contained in:
Jeremy Wu 2020-11-02 08:01:50 +11:00
commit 732f3b410a
10 changed files with 124 additions and 219 deletions

View file

@ -1,178 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using hyjiacan.util.p4n;
using hyjiacan.util.p4n.format;
using JetBrains.Annotations;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher.Infrastructure
{
public interface IAlphabet
{
string Translate(string stringToTranslate);
}
public class Alphabet : IAlphabet
{
private readonly HanyuPinyinOutputFormat Format = new HanyuPinyinOutputFormat();
private ConcurrentDictionary<string, string[][]> PinyinCache;
private BinaryStorage<Dictionary<string, string[][]>> _pinyinStorage;
private Settings _settings;
public void Initialize([NotNull] Settings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
InitializePinyinHelpers();
}
private void InitializePinyinHelpers()
{
Format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
Stopwatch.Normal("|Flow Launcher.Infrastructure.Alphabet.Initialize|Preload pinyin cache", () =>
{
_pinyinStorage = new BinaryStorage<Dictionary<string, string[][]>>("Pinyin");
lock(_pinyinStorage)
{
var loaded = _pinyinStorage.TryLoad(new Dictionary<string, string[][]>());
PinyinCache = new ConcurrentDictionary<string, string[][]>(loaded);
}
// force pinyin library static constructor initialize
PinyinHelper.toHanyuPinyinStringArray('T', Format);
});
Log.Info($"|Flow Launcher.Infrastructure.Alphabet.Initialize|Number of preload pinyin combination<{PinyinCache.Count}>");
}
public string Translate(string str)
{
return ConvertChineseCharactersToPinyin(str);
}
public string ConvertChineseCharactersToPinyin(string source)
{
if (!_settings.ShouldUsePinyin)
return source;
if (string.IsNullOrEmpty(source))
return source;
if (!ContainsChinese(source))
return source;
var combination = PinyinCombination(source);
var pinyinArray=combination.Select(x => string.Join("", x));
var acronymArray = combination.Select(Acronym).Distinct();
var joinedSingleStringCombination = new StringBuilder();
var all = acronymArray.Concat(pinyinArray);
all.ToList().ForEach(x => joinedSingleStringCombination.Append(x));
return joinedSingleStringCombination.ToString();
}
public void Save()
{
if (!_settings.ShouldUsePinyin)
{
return;
}
lock(_pinyinStorage)
{
_pinyinStorage.Save(PinyinCache.ToDictionary(i => i.Key, i => i.Value));
}
}
private static string[] EmptyStringArray = new string[0];
private static string[][] Empty2DStringArray = new string[0][];
/// <summmary>
/// replace chinese character with pinyin, non chinese character won't be modified
/// Because we don't have words dictionary, so we can only return all possiblie pinyin combination
/// e.g. 音乐 will return yinyue and yinle
/// <param name="characters"> should be word or sentence, instead of single character. e.g. 微软 </param>
/// </summmary>
public string[][] PinyinCombination(string characters)
{
if (!_settings.ShouldUsePinyin || string.IsNullOrEmpty(characters))
{
return Empty2DStringArray;
}
if (!PinyinCache.ContainsKey(characters))
{
var allPinyins = new List<string[]>();
foreach (var c in characters)
{
var pinyins = PinyinHelper.toHanyuPinyinStringArray(c, Format);
if (pinyins != null)
{
var r = pinyins.Distinct().ToArray();
allPinyins.Add(r);
}
else
{
var r = new[] { c.ToString() };
allPinyins.Add(r);
}
}
var combination = allPinyins.Aggregate(Combination).Select(c => c.Split(';')).ToArray();
PinyinCache[characters] = combination;
return combination;
}
else
{
return PinyinCache[characters];
}
}
public string Acronym(string[] pinyin)
{
var acronym = string.Join("", pinyin.Select(p => p[0]));
return acronym;
}
public bool ContainsChinese(string word)
{
if (!_settings.ShouldUsePinyin)
{
return false;
}
if (word.Length > 40)
{
//Skip strings that are too long string for Pinyin conversion.
return false;
}
var chinese = word.Select(PinyinHelper.toHanyuPinyinStringArray)
.Any(p => p != null);
return chinese;
}
private string[] Combination(string[] array1, string[] array2)
{
if (!_settings.ShouldUsePinyin)
{
return EmptyStringArray;
}
var combination = (
from a1 in array1
from a2 in array2
select $"{a1};{a2}"
).ToArray();
return combination;
}
}
}

View file

@ -52,8 +52,8 @@
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NLog.Schema" Version="4.7.0-rc1" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
<PackageReference Include="Pinyin4DotNet" Version="2016.4.23.4" />
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
<PackageReference Include="ToolGood.Words.Pinyin" Version="3.0.1.4" />
</ItemGroup>
<ItemGroup>

View file

@ -0,0 +1,65 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JetBrains.Annotations;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Infrastructure.UserSettings;
using ToolGood.Words.Pinyin;
using System.Threading.Tasks;
namespace Flow.Launcher.Infrastructure
{
public interface IAlphabet
{
string Translate(string stringToTranslate);
}
public class PinyinAlphabet : IAlphabet
{
private ConcurrentDictionary<string, string> _pinyinCache = new ConcurrentDictionary<string, string>();
private Settings _settings;
public void Initialize([NotNull] Settings settings)
{
_settings = settings ?? throw new ArgumentNullException(nameof(settings));
}
public string Translate(string content)
{
if (_settings.ShouldUsePinyin)
{
if (!_pinyinCache.ContainsKey(content))
{
if (WordsHelper.HasChinese(content))
{
var result = WordsHelper.GetPinyin(content, ";");
result = GetFirstPinyinChar(result) + result.Replace(";", "");
_pinyinCache[content] = result;
return result;
}
else
{
return content;
}
}
else
{
return _pinyinCache[content];
}
}
else
{
return content;
}
}
private string GetFirstPinyinChar(string content)
{
return string.Concat(content.Split(';').Select(x => x.First()));
}
}
}

View file

@ -29,7 +29,7 @@ namespace Flow.Launcher
private SettingWindowViewModel _settingsVM;
private readonly Updater _updater = new Updater(Flow.Launcher.Properties.Settings.Default.GithubRepo);
private readonly Portable _portable = new Portable();
private readonly Alphabet _alphabet = new Alphabet();
private readonly PinyinAlphabet _alphabet = new PinyinAlphabet();
private StringMatcher _stringMatcher;
[STAThread]

View file

@ -21,11 +21,11 @@ namespace Flow.Launcher
{
private readonly SettingWindowViewModel _settingsVM;
private readonly MainViewModel _mainVM;
private readonly Alphabet _alphabet;
private readonly PinyinAlphabet _alphabet;
#region Constructor
public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, Alphabet alphabet)
public PublicAPIInstance(SettingWindowViewModel settingsVM, MainViewModel mainVM, PinyinAlphabet alphabet)
{
_settingsVM = settingsVM;
_mainVM = mainVM;
@ -76,7 +76,6 @@ namespace Flow.Launcher
_settingsVM.Save();
PluginManager.Save();
ImageLoader.Save();
_alphabet.Save();
}
public void ReloadAllPluginData()

View file

@ -19,10 +19,8 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
set
{
m_Name = value;
PinyinName = m_Name.Unidecode();
}
}
public string PinyinName { get; private set; }
public string Url { get; set; }
public string Source { get; set; }
public int Score { get; set; }

View file

@ -6,19 +6,19 @@ namespace Flow.Launcher.Plugin.BrowserBookmark.Commands
{
internal static class Bookmarks
{
internal static bool MatchProgram(Bookmark bookmark, string queryString)
internal static MatchResult MatchProgram(Bookmark bookmark, string queryString)
{
if (StringMatcher.FuzzySearch(queryString, bookmark.Name).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.PinyinName).IsSearchPrecisionScoreMet()) return true;
if (StringMatcher.FuzzySearch(queryString, bookmark.Url).IsSearchPrecisionScoreMet()) return true;
var match = StringMatcher.FuzzySearch(queryString, bookmark.Name);
if (match.IsSearchPrecisionScoreMet())
return match;
return false;
return StringMatcher.FuzzySearch(queryString, bookmark.Url);
}
internal static List<Bookmark> LoadAllBookmarks()
{
var allbookmarks = new List<Bookmark>();
var chromeBookmarks = new ChromeBookmarks();
var mozBookmarks = new FirefoxBookmarks();
var edgeBookmarks = new EdgeBookmarks();

View file

@ -18,7 +18,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
return bookmarks;
}
private void ParseEdgeBookmarks(String path, string source)
private void ParseEdgeBookmarks(string path, string source)
{
if (!File.Exists(path)) return;
@ -72,12 +72,13 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
private void LoadEdgeBookmarks()
{
String platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
string platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
LoadEdgeBookmarks(Path.Combine(platformPath, @"Microsoft\Edge\User Data"), "Microsoft Edge");
LoadEdgeBookmarks(Path.Combine(platformPath, @"Microsoft\Edge Dev\User Data"), "Microsoft Edge Dev");
LoadEdgeBookmarks(Path.Combine(platformPath, @"Microsoft\Edge SxS\User Data"), "Microsoft Edge Canary");
}
private String DecodeUnicode(String dataStr)
private string DecodeUnicode(string dataStr)
{
Regex reg = new Regex(@"(?i)\\[uU]([0-9a-f]{4})");
return reg.Replace(dataStr, m => ((char)Convert.ToInt32(m.Groups[1].Value, 16)).ToString());

View file

@ -12,7 +12,7 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
public class Main : ISettingProvider, IPlugin, IReloadable, IPluginI18n, ISavable
{
private PluginInitContext context;
private List<Bookmark> cachedBookmarks = new List<Bookmark>();
private readonly Settings _settings;
@ -37,36 +37,56 @@ namespace Flow.Launcher.Plugin.BrowserBookmark
// Should top results be returned? (true if no search parameters have been passed)
var topResults = string.IsNullOrEmpty(param);
var returnList = cachedBookmarks;
if (!topResults)
{
// Since we mixed chrome and firefox bookmarks, we should order them again
returnList = cachedBookmarks.Where(o => Bookmarks.MatchProgram(o, param)).ToList();
returnList = returnList.OrderByDescending(o => o.Score).ToList();
}
return returnList.Select(c => new Result()
{
Title = c.Name,
SubTitle = c.Url,
IcoPath = @"Images\bookmark.png",
Score = 5,
Action = (e) =>
var returnList = cachedBookmarks.Select(c => new Result()
{
if (_settings.OpenInNewBrowserWindow)
Title = c.Name,
SubTitle = c.Url,
IcoPath = @"Images\bookmark.png",
Score = Bookmarks.MatchProgram(c, param).Score,
Action = _ =>
{
c.Url.NewBrowserWindow(_settings.BrowserPath);
}
else
{
c.Url.NewTabInBrowser(_settings.BrowserPath);
}
if (_settings.OpenInNewBrowserWindow)
{
c.Url.NewBrowserWindow(_settings.BrowserPath);
}
else
{
c.Url.NewTabInBrowser(_settings.BrowserPath);
}
return true;
}
}).ToList();
return true;
}
}).Where(r => r.Score > 0);
return returnList.ToList();
}
else
{
return cachedBookmarks.Select(c => new Result()
{
Title = c.Name,
SubTitle = c.Url,
IcoPath = @"Images\bookmark.png",
Score = 5,
Action = _ =>
{
if (_settings.OpenInNewBrowserWindow)
{
c.Url.NewBrowserWindow(_settings.BrowserPath);
}
else
{
c.Url.NewTabInBrowser(_settings.BrowserPath);
}
return true;
}
}).ToList();
}
}
public void ReloadData()

View file

@ -4,7 +4,7 @@
"Name": "Browser Bookmarks",
"Description": "Search your browser bookmarks",
"Author": "qianlifeng, Ioannis G.",
"Version": "1.2.0",
"Version": "1.2.1",
"Language": "csharp",
"Website": "https://github.com/Flow-Launcher/Flow.Launcher",
"ExecuteFileName": "Flow.Launcher.Plugin.BrowserBookmark.dll",