countLevenshtein static method

int countLevenshtein({
  1. required dynamic searchTerms,
  2. required Iterable searchOnItems,
  3. required int levenshteinDistance,
  4. bool ignoreCase = true,
  5. bool ignoreDiacritics = true,
  6. bool ignoreWordSplitters = true,
  7. bool splitCamelCase = true,
})

Calculates the Levenshtein distance between an searchOnItems items and a list of search terms.

The Levenshtein distance is a measure of the difference between two strings. It represents the minimum number of single-character edits (insertions, deletions, or substitutions) required to change one string into another.

The searchOnItems function is used to extract the relevant terms from the item for comparison. The levenshteinDistance is the maximum allowed Levenshtein distance for a match to be considered. The optional parameters ignoreCase, ignoreDiacritics, ignoreWordSplitters, splitCamelCase, and useWildcards control various aspects of the comparison.

Returns the count of searchTerms that have a Levenshtein distance less than or equal to levenshteinDistance.

Implementation

static int countLevenshtein({
  required dynamic searchTerms,
  required Iterable searchOnItems,
  required int levenshteinDistance,
  bool ignoreCase = true,
  bool ignoreDiacritics = true,
  bool ignoreWordSplitters = true,
  bool splitCamelCase = true,
}) {
  if (levenshteinDistance <= 0) {
    return 0;
  } else {
    var terms = <dynamic>[...searchOnItems];
    var searches = forceRecursiveList(searchTerms);
    return [
      for (var searchTerm in searches)
        ...terms.expand((e) {
          if (e == null) return [];
          return e.toString().getUniqueWords.map((keyword) {
            keyword = generateKeyword(
              keyword,
              forceLowerCase: ignoreCase,
              removeDiacritics: ignoreDiacritics,
              removeWordSplitters: ignoreWordSplitters,
              splitCamelCase: splitCamelCase,
            );
            var searchword = generateKeyword(
              searchTerm,
              forceLowerCase: ignoreCase,
              removeDiacritics: ignoreDiacritics,
              removeWordSplitters: ignoreWordSplitters,
              splitCamelCase: splitCamelCase,
            );
            return searchword.getLevenshtein(keyword, !ignoreCase);
          });
        })
    ].count((e) {
      return e <= levenshteinDistance;
    });
  }
}