vHanda / fuzzy_search

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fuzzy Search

CI

Fuzzy search is typically used to search a large list of file names by typing a few relevant characters. Such as Quick Open in VSCode or Sublime.

Unlike classical fuzzy string matching which matches strings which approximately match (implemented by Fuse.js). The matching here requires the matching characters to exist one after another at some point in the string.

Example -

source/string.dart
source/test/regression/graph.dart
sour/strinf.dart

When searching for string, only the first two strings match. The matching characters are indicated in uppercase -

source/STRING.dart
Source/Test/RegressIoN/Graph.dart

This library implements the algorithm explained in these wonderful posts.

Example

  var list = [
    'source/string.dart',
    'source/test/regression/graph.dart',
    'sour/strinf.dart',
  ];
  for (var l in list) {
    var r = l.fuzzyMatch('string');
    if (r == null) {
      continue;
    }

    var cp = l;
    for (var index in r.indexes) {
      var char = String.fromCharCode(cp.codeUnitAt(index));
      cp = cp.replaceRange(index, index + 1, char.toUpperCase());
    }

    print('${r.score} $l -> $cp');
  }

The higher the score, the better the match.

For automatic diacritics and case-removal look at the API in example.

About

License:Apache License 2.0


Languages

Language:Dart 99.9%Language:HTML 0.0%Language:Swift 0.0%Language:Kotlin 0.0%Language:Objective-C 0.0%