add fuzzy string match support for Programs plugin
This commit is contained in:
parent
8c7547ab03
commit
a9fa1fb66b
50
Wox.Infrastructure/FuzzyMatcher.cs
Normal file
50
Wox.Infrastructure/FuzzyMatcher.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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" />
|
||||
|
|
|
@ -37,7 +37,8 @@ namespace Wox.Plugin.System
|
|||
{
|
||||
if (string.IsNullOrEmpty(query.RawQuery) || query.RawQuery.EndsWith(" ") || query.RawQuery.Length <= 1) return new List<Result>();
|
||||
|
||||
List<Program> returnList = installedList.Where(o => MatchProgram(o, query)).ToList();
|
||||
var fuzzyMather = FuzzyMatcher.Create(query.RawQuery);
|
||||
List<Program> returnList = installedList.Where(o => MatchProgram(o, fuzzyMather)).ToList();
|
||||
returnList.ForEach(ScoreFilter);
|
||||
|
||||
return returnList.Select(c => new Result()
|
||||
|
@ -71,10 +72,12 @@ namespace Wox.Plugin.System
|
|||
}).ToList();
|
||||
}
|
||||
|
||||
private bool MatchProgram(Program program, Query query)
|
||||
private bool MatchProgram(Program program, FuzzyMatcher matcher)
|
||||
{
|
||||
if (program.Title.ToLower().Contains(query.RawQuery.ToLower())) return true;
|
||||
if (ChineseToPinYin.ToPinYin(program.Title).Replace(" ", "").ToLower().Contains(query.RawQuery.ToLower())) return true;
|
||||
program.Score = matcher.Score(program.Title);
|
||||
if (program.Score > 0) return true;
|
||||
program.Score = matcher.Score(ChineseToPinYin.ToPinYin(program.Title).Replace(" ", ""));
|
||||
if (program.Score > 0) return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
|
44
Wox.Test/FuzzyMatcherTest.cs
Normal file
44
Wox.Test/FuzzyMatcherTest.cs
Normal file
|
@ -0,0 +1,44 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using NUnit.Framework;
|
||||
using Wox.Infrastructure;
|
||||
|
||||
namespace Wox.Test
|
||||
{
|
||||
[TestFixture]
|
||||
public class FuzzyMatcherTest
|
||||
{
|
||||
[Test]
|
||||
public void MatchTest()
|
||||
{
|
||||
var sources = new List<string>()
|
||||
{
|
||||
"file open in browser-test",
|
||||
"Install Package",
|
||||
"add new bsd",
|
||||
"Inste",
|
||||
"aac",
|
||||
};
|
||||
|
||||
|
||||
var results = new List<Wox.Plugin.Result>();
|
||||
foreach (var str in sources)
|
||||
{
|
||||
results.Add(new Plugin.Result()
|
||||
{
|
||||
Title = str,
|
||||
Score = FuzzyMatcher.Create("inst").Score(str)
|
||||
});
|
||||
}
|
||||
|
||||
results = results.Where(x => x.Score > 0).OrderByDescending(x => x.Score).ToList();
|
||||
|
||||
Assert.IsTrue(results.Count == 3);
|
||||
Assert.IsTrue(results[0].Title == "Inste");
|
||||
Assert.IsTrue(results[1].Title == "Install Package");
|
||||
Assert.IsTrue(results[2].Title == "file open in browser-test");
|
||||
}
|
||||
}
|
||||
}
|
|
@ -43,6 +43,7 @@
|
|||
<Reference Include="System.Xml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="FuzzyMatcherTest.cs" />
|
||||
<Compile Include="PyImportTest.cs" />
|
||||
<Compile Include="QueryTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
|
@ -55,6 +56,10 @@
|
|||
<Project>{097B4AC0-74E9-4C58-BCF8-C69746EC8271}</Project>
|
||||
<Name>Python.Runtime</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Wox.Infrastructure\Wox.Infrastructure.csproj">
|
||||
<Project>{4FD29318-A8AB-4D8F-AA47-60BC241B8DA3}</Project>
|
||||
<Name>Wox.Infrastructure</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\Wox.Plugin\Wox.Plugin.csproj">
|
||||
<Project>{8451ECDD-2EA4-4966-BB0A-7BBC40138E80}</Project>
|
||||
<Name>Wox.Plugin</Name>
|
||||
|
|
Loading…
Reference in a new issue