Skip to content

Portable Tool for an Ideal Windows Setup

add fuzzy search with cyrillic

Greedeks63fe40f09e1d
diff --git a/.Source/GTweak/Core/Services/FuzzySearchService.cs b/.Source/GTweak/Core/Services/FuzzySearchService.cs
new file mode 100644
index 00000000..db0abf63
--- /dev/null
+++ b/.Source/GTweak/Core/Services/FuzzySearchService.cs
@@ -0,0 +1,141 @@
+using System;
+
+namespace GTweak.Core.Services
+{
+ internal class FuzzySearchService
+ {
+ private const string CyrillicLayoutMap = "йцукенгшщзхъфывапролджэячсмитьбюёієїўґђјљњћџѕ";
+ private const string QwertyLayoutMap = "qwertyuiop[]asdfghjkl;'zxcvbnm,.`s]['\\][;'/s";
+
+ public bool IsMatch(string sourceText, string searchQuery)
+ {
+ if (string.IsNullOrWhiteSpace(sourceText) || string.IsNullOrWhiteSpace(searchQuery))
+ {
+ return false;
+ }
+
+ string normalizedQuery = searchQuery.Trim().ToLowerInvariant();
+
+ if (CheckExactOrFuzzyMatch(sourceText, normalizedQuery))
+ {
+ return true;
+ }
+
+ string correctedQuery = MapCyrillicToQwerty(normalizedQuery);
+
+ if (!ReferenceEquals(normalizedQuery, correctedQuery))
+ {
+ if (CheckExactOrFuzzyMatch(sourceText, correctedQuery))
+ {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ private bool CheckExactOrFuzzyMatch(string sourceText, string normalizedQuery)
+ {
+ if (sourceText.IndexOf(normalizedQuery, StringComparison.OrdinalIgnoreCase) >= 0)
+ {
+ return true;
+ }
+
+ return CheckWordsFuzzyMatch(sourceText, normalizedQuery);
+ }
+
+ private bool CheckWordsFuzzyMatch(string sourceText, string normalizedQuery)
+ {
+ int maxAllowedErrors = normalizedQuery.Length > 4 ? 2 : 1;
+ int currentWordStartIndex = 0;
+ int sourceLength = sourceText.Length;
+
+ for (int i = 0; i <= sourceLength; i++)
+ {
+ if (i == sourceLength || char.IsWhiteSpace(sourceText[i]) || char.IsPunctuation(sourceText[i]))
+ {
+ int wordLength = i - currentWordStartIndex;
+
+ if (wordLength > 0 && Math.Abs(wordLength - normalizedQuery.Length) <= maxAllowedErrors)
+ {
+ if (CalculateLevenshteinDistance(sourceText, currentWordStartIndex, wordLength, normalizedQuery) <= maxAllowedErrors)
+ {
+ return true;
+ }
+ }
+
+ currentWordStartIndex = i + 1;
+ }
+ }
+ return false;
+ }
+
+ private int CalculateLevenshteinDistance(string sourceText, int wordStart, int wordLength, string normalizedQuery)
+ {
+ int queryLength = normalizedQuery.Length;
+
+ if (queryLength > 128)
+ {
+ return int.MaxValue;
+ }
+
+ Span<int> previousRowCosts = stackalloc int[queryLength + 1];
+ Span<int> currentRowCosts = stackalloc int[queryLength + 1];
+
+ for (int i = 0; i <= queryLength; i++)
+ {
+ previousRowCosts[i] = i;
+ }
+
+ for (int sourceIndex = 0; sourceIndex < wordLength; sourceIndex++)
+ {
+ currentRowCosts[0] = sourceIndex + 1;
+
+ char sourceChar = char.ToLowerInvariant(sourceText[wordStart + sourceIndex]);
+
+ for (int queryIndex = 0; queryIndex < queryLength; queryIndex++)
+ {
+ int substitutionCost = (sourceChar == normalizedQuery[queryIndex]) ? 0 : 1;
+
+ int deletionCost = previousRowCosts[queryIndex + 1] + 1;
+ int insertionCost = currentRowCosts[queryIndex] + 1;
+ int matchOrReplaceCost = previousRowCosts[queryIndex] + substitutionCost;
+
+ currentRowCosts[queryIndex + 1] = Math.Min(Math.Min(insertionCost, deletionCost), matchOrReplaceCost);
+ }
+
+ currentRowCosts.CopyTo(previousRowCosts);
+ }
+
+ return currentRowCosts[queryLength];
+ }
+
+ private string MapCyrillicToQwerty(string normalizedQuery)
+ {
+ int length = normalizedQuery.Length;
+
+ Span<char> correctedBuffer = length <= 128 ? stackalloc char[length] : new char[length];
+
+ bool isLayoutChanged = false;
+
+ for (int i = 0; i < length; i++)
+ {
+ char originalChar = normalizedQuery[i];
+
+ int cyrillicIndex = CyrillicLayoutMap.IndexOf(originalChar);
+
+ if (cyrillicIndex >= 0 && cyrillicIndex < QwertyLayoutMap.Length)
+ {
+ correctedBuffer[i] = QwertyLayoutMap[cyrillicIndex];
+ isLayoutChanged = true;
+ }
+ else
+ {
+ correctedBuffer[i] = originalChar;
+ }
+ }
+
+ return isLayoutChanged ? new string(correctedBuffer.ToArray()) : normalizedQuery;
+ }
+ }
+}
\ No newline at end of file
diff --git a/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs b/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs
index e90b5144..dc54b70d 100644
--- a/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs
+++ b/.Source/GTweak/Core/ViewModel/ToolsetViewModel.cs
@@ -9,6 +9,7 @@ using System.Xml.Linq;
using GTweak.Core.Base;
using GTweak.Core.Item;
using GTweak.Core.Models;
+using GTweak.Core.Services;
using GTweak.Utilities.Controls;
using Ookii.Dialogs.Wpf;
@@ -17,8 +18,9 @@ namespace GTweak.Core.ViewModel
internal class ToolsetViewModel : ViewModelBase
{
public ObservableCollection<ToolsetItem> Tools { get; } = new ObservableCollection<ToolsetItem>();
+ private readonly FuzzySearchService _fuzzyService = new FuzzySearchService();
- private string _searchText;
+ private string _searchText = string.Empty;
public ICollectionView ToolsView { get; }
public ICommand SelectFolderCommand { get; }
@@ -30,11 +32,13 @@ namespace GTweak.Core.ViewModel
get => _searchText;
set
{
+ value ??= string.Empty;
+
if (_searchText != value)
{
_searchText = value;
OnPropertyChanged();
- ToolsView.Refresh();
+ ToolsView?.Refresh();
}
}
}
@@ -43,12 +47,12 @@ namespace GTweak.Core.ViewModel
{
SelectFolderCommand = new RelayCommand(_ => SelectFolder());
OpenFolderCommand = new RelayCommand(_ => OpenFolder());
- ClearCommand = new RelayCommand(obj => { SearchText = string.Empty; });
-
- LoadApps();
+ ClearCommand = new RelayCommand(_ => SearchText = string.Empty);
ToolsView = CollectionViewSource.GetDefaultView(Tools);
ToolsView.Filter = FilterTools;
+
+ LoadApps();
}
private void LoadApps()
@@ -67,26 +71,25 @@ namespace GTweak.Core.ViewModel
Tools.Add(new ToolsetItem(model));
}
}
+
+ ToolsView?.Refresh();
}
catch (Exception ex) { ErrorLogging.LogDebug(ex); }
}
private bool FilterTools(object obj)
{
- if (!string.IsNullOrWhiteSpace(SearchText))
+ if (obj is ToolsetItem item)
{
- if (obj is ToolsetItem item)
+ if (string.IsNullOrWhiteSpace(_searchText))
{
- bool nameMatch = item.AppName?.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) >= 0;
- bool authorMatch = item.AuthorName?.IndexOf(SearchText, StringComparison.OrdinalIgnoreCase) >= 0;
-
- return nameMatch || authorMatch;
+ return true;
}
- return false;
+ return _fuzzyService.IsMatch(item.AppName, _searchText) || _fuzzyService.IsMatch(item.AuthorName, _searchText);
}
- return true;
+ return false;
}
private void SelectFolder()
@@ -102,6 +105,7 @@ namespace GTweak.Core.ViewModel
if (folderDialog.ShowDialog() == true)
{
string selectedPath = folderDialog.SelectedPath;
+
if (!string.IsNullOrWhiteSpace(selectedPath) && Directory.Exists(selectedPath))
{
SettingsEngine.DownloadPath = selectedPath;
diff --git a/.Source/GTweak/GTweak.csproj b/.Source/GTweak/GTweak.csproj
index 1d7d4124..8f387b2f 100644
--- a/.Source/GTweak/GTweak.csproj
+++ b/.Source/GTweak/GTweak.csproj
@@ -148,6 +148,7 @@
<Compile Include="Core\Base\IViewPageBase.cs" />
<Compile Include="Core\Base\ViewModelPageBase.cs" />
<Compile Include="Core\Converters\AngleToPercentConverter.cs" />
+ <Compile Include="Core\Services\FuzzySearchService.cs" />
<Compile Include="Core\ViewModel\Components\DownloadSession.cs" />
<Compile Include="Core\ViewModel\Components\ToolsetItem.cs" />
<Compile Include="Core\Model\AddonsModel.cs" />