add fuzzy string match support for Programs plugin

This commit is contained in:
cxfksword
2014-02-09 00:33:10 +08:00
committed by Yeechan Lu
parent 8c7547ab03
commit a9fa1fb66b
5 changed files with 107 additions and 4 deletions

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Wox.Infrastructure
{
//From:http://crossplatform.net/sublime-text-ctrl-p-fuzzy-matching-in-python/
public class FuzzyMatcher
{
private Regex reg = null;
private string rawQuery = "";
private FuzzyMatcher(string query)
{
this.rawQuery = query;
this.reg = GetPattern(query);
}
private Regex GetPattern(string query)
{
var pattern = string.Join(".*?", query.ToCharArray().Select(x => Regex.Escape(x.ToString())).ToArray());
return new Regex(pattern, RegexOptions.IgnoreCase);
}
public int Score(string str)
{
var match = reg.Match(str);
if (!match.Success)
return 0;
//a match found near the beginning of a string is scored more than a match found near the end
//a match is scored more if the characters in the patterns are closer to each other, while the score is lower if they are more spread out
var score = 100 * (this.rawQuery.Length + 1) / ((1 + match.Index) + (match.Length + 1));
//a match with less characters assigning more weights
if (str.Length - this.rawQuery.Length < 5)
score = score + 20;
else if (str.Length - this.rawQuery.Length < 10)
score = score + 10;
return score;
}
public static FuzzyMatcher Create(string query)
{
return new FuzzyMatcher(query);
}
}
}

View File

@@ -49,6 +49,7 @@
<ItemGroup>
<Compile Include="ChineseToPinYin.cs" />
<Compile Include="CommonStorage.cs" />
<Compile Include="FuzzyMatcher.cs" />
<Compile Include="IniParser.cs" />
<Compile Include="UAC.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />