FuzzyMatcher for Bookmarks and some tweaks

This commit is contained in:
Yeechan Lu 2014-03-17 03:37:52 +08:00
parent a7159ecf1d
commit 94ed77353e
2 changed files with 76 additions and 25 deletions

View File

@ -17,16 +17,13 @@ namespace Wox.Plugin.System
private List<Bookmark> bookmarks = new List<Bookmark>(); private List<Bookmark> bookmarks = new List<Bookmark>();
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out] StringBuilder lpszPath, int nFolder, bool fCreate);
const int CSIDL_LOCAL_APPDATA = 0x001c;
protected override List<Result> QueryInternal(Query query) protected override List<Result> QueryInternal(Query query)
{ {
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>(); if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
List<Bookmark> returnList = bookmarks.Where(o => MatchProgram(o, query)).ToList(); var fuzzyMather = FuzzyMatcher.Create(query.RawQuery);
List<Bookmark> returnList = bookmarks.Where(o => MatchProgram(o, fuzzyMather)).ToList();
returnList = returnList.OrderByDescending(o => o.Score).ToList();
return returnList.Select(c => new Result() return returnList.Select(c => new Result()
{ {
Title = c.Name, Title = c.Name,
@ -47,26 +44,27 @@ namespace Wox.Plugin.System
} }
}).ToList(); }).ToList();
} }
private bool MatchProgram(Bookmark bookmark, FuzzyMatcher matcher)
private bool MatchProgram(Bookmark bookmark, Query query)
{ {
if (bookmark.Name.ToLower().Contains(query.RawQuery.ToLower()) || bookmark.Url.ToLower().Contains(query.RawQuery.ToLower())) return true; if ((bookmark.Score = matcher.Score(bookmark.Name)) > 0) return true;
if (ChineseToPinYin.ToPinYin(bookmark.Name).Replace(" ", "").ToLower().Contains(query.RawQuery.ToLower())) return true; if ((bookmark.Score = matcher.Score(bookmark.PinyinName)) > 0) return true;
if ((bookmark.Score = matcher.Score(bookmark.Url) / 10) > 0) return true;
return false; return false;
} }
protected override void InitInternal(PluginInitContext context) protected override void InitInternal(PluginInitContext context)
{ {
bookmarks.Clear();
LoadChromeBookmarks(); LoadChromeBookmarks();
bookmarks = bookmarks.Distinct().ToList();
} }
private void LoadChromeBookmarks() private void LoadChromeBookmarks()
{ {
StringBuilder platformPath = new StringBuilder(560); String platformPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
SHGetSpecialFolderPath(IntPtr.Zero, platformPath, CSIDL_LOCAL_APPDATA, false); string path = global::System.IO.Path.Combine(platformPath, @"Google\Chrome\User Data\Default\Bookmarks");
string path = platformPath + @"\Google\Chrome\User Data\Default\Bookmarks";
if (File.Exists(path)) if (File.Exists(path))
{ {
string all = File.ReadAllText(path); string all = File.ReadAllText(path);
@ -91,6 +89,10 @@ namespace Wox.Plugin.System
string url = urls[urlIndex]; string url = urls[urlIndex];
urlIndex++; urlIndex++;
if (url == null) continue;
if (url.StartsWith("javascript:", StringComparison.OrdinalIgnoreCase)) continue;
if (url.StartsWith("vbscript:", StringComparison.OrdinalIgnoreCase)) continue;
bookmarks.Add(new Bookmark() bookmarks.Add(new Bookmark()
{ {
Name = name, Name = name,
@ -109,10 +111,50 @@ namespace Wox.Plugin.System
} }
} }
public class Bookmark public class Bookmark : IEquatable<Bookmark>, IEqualityComparer<Bookmark>
{ {
public string Name { get; set; } private string m_Name;
public string Name {
get{
return m_Name;
}
set
{
m_Name = value;
PinyinName = ChineseToPinYin.ToPinYin(m_Name).Replace(" ", "").ToLower();
}
}
public string PinyinName { get; private set; }
public string Url { get; set; } public string Url { get; set; }
public string Source { get; set; } public string Source { get; set; }
public int Score { get; set; }
/* TODO: since Source maybe unimportant, we just need to compare Name and Url */
public bool Equals(Bookmark other)
{
return Equals(this, other);
}
public bool Equals(Bookmark x, Bookmark y)
{
if (Object.ReferenceEquals(x, y)) return true;
if (Object.ReferenceEquals(x, null) || Object.ReferenceEquals(y, null))
return false;
return x.Name == y.Name && x.Url == y.Url;
}
public int GetHashCode(Bookmark bookmark)
{
if (Object.ReferenceEquals(bookmark, null)) return 0;
int hashName = bookmark.Name == null ? 0 : bookmark.Name.GetHashCode();
int hashUrl = bookmark.Url == null ? 0 : bookmark.Url.GetHashCode();
return hashName ^ hashUrl;
}
public override int GetHashCode()
{
return GetHashCode(this);
}
} }
} }

View File

@ -14,10 +14,24 @@ namespace Wox.Plugin.System
{ {
public class Program public class Program
{ {
public string Title { get; set; } private string m_Title;
public string Title
{
get
{
return m_Title;
}
set
{
m_Title = value;
PinyinTitle = ChineseToPinYin.ToPinYin(m_Title).Replace(" ", "").ToLower();
}
}
public string PinyinTitle { get; private set; }
public string IcoPath { get; set; } public string IcoPath { get; set; }
public string ExecutePath { get; set; } public string ExecutePath { get; set; }
public int Score { get; set; } public int Score { get; set; }
} }
public class Programs : BaseSystemPlugin public class Programs : BaseSystemPlugin
@ -42,13 +56,12 @@ namespace Wox.Plugin.System
//return ordered list instead of return the score, because programs scores will affect other //return ordered list instead of return the score, because programs scores will affect other
//plugins, the weight of program should be less than the plugins when they showed at the same time. //plugins, the weight of program should be less than the plugins when they showed at the same time.
returnList = returnList.OrderByDescending(o => o.Score).ToList(); returnList = returnList.OrderByDescending(o => o.Score).ToList();
returnList.ForEach(o=>o.Score = 0);
return returnList.Select(c => new Result() return returnList.Select(c => new Result()
{ {
Title = c.Title, Title = c.Title,
IcoPath = c.IcoPath, IcoPath = c.IcoPath,
Score = c.Score, Score = 0,
Action = (context) => Action = (context) =>
{ {
if (string.IsNullOrEmpty(c.ExecutePath)) if (string.IsNullOrEmpty(c.ExecutePath))
@ -78,10 +91,8 @@ namespace Wox.Plugin.System
private bool MatchProgram(Program program, FuzzyMatcher matcher) private bool MatchProgram(Program program, FuzzyMatcher matcher)
{ {
program.Score = matcher.Score(program.Title); if ((program.Score = matcher.Score(program.Title)) > 0) return true;
if (program.Score > 0) return true; if ((program.Score = matcher.Score(program.PinyinTitle)) > 0) return true;
program.Score = matcher.Score(ChineseToPinYin.ToPinYin(program.Title).Replace(" ", ""));
if (program.Score > 0) return true;
return false; return false;
} }
@ -141,9 +152,7 @@ namespace Wox.Plugin.System
private string getAppNameFromAppPath(string app) private string getAppNameFromAppPath(string app)
{ {
string temp = app.Substring(app.LastIndexOf('\\') + 1); return global::System.IO.Path.GetFileNameWithoutExtension(app);
string name = temp.Substring(0, temp.LastIndexOf('.'));
return name;
} }
} }
} }