mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-07 19:57:07 +02:00
add fuzzy string match support for Programs plugin
This commit is contained in:
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" />
|
||||
|
||||
Reference in New Issue
Block a user