Class FuzzyMatcher<T>

java.lang.Object
cloud.opencode.base.string.match.FuzzyMatcher<T>
Type Parameters:
T - the type of items to match | 要匹配的项目类型

public final class FuzzyMatcher<T> extends Object
Fuzzy String Matcher - Provides fuzzy matching and search suggestions 模糊字符串匹配器 - 提供模糊匹配和搜索建议功能

Supports multiple fuzzy matching algorithms including Levenshtein distance, Jaro-Winkler similarity, and prefix matching.

支持多种模糊匹配算法,包括 Levenshtein 距离、Jaro-Winkler 相似度和前缀匹配。

Features | 主要功能:

  • Fuzzy search with customizable threshold - 可自定义阈值的模糊搜索
  • Search suggestions with ranking - 带排名的搜索建议
  • Typo tolerance - 错误容忍
  • Multiple matching algorithms - 多种匹配算法
  • Case-insensitive matching - 不区分大小写匹配

Usage Examples | 使用示例:

// Build a fuzzy matcher
FuzzyMatcher<String> matcher = FuzzyMatcher.<String>builder()
    .addAll(List.of("apple", "application", "banana", "apply"))
    .threshold(0.6)
    .maxResults(5)
    .build();

// Find matches
List<FuzzyMatch<String>> results = matcher.match("aple");
// Returns: apple, apply, application (sorted by similarity)

// Get suggestions
List<String> suggestions = matcher.suggest("app");

Security | 安全性:

  • Thread-safe: Yes (immutable after construction) - 线程安全: 是(构造后不可变)
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-string V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • builder

      public static FuzzyMatcher.Builder<String> builder()
      Creates a new builder for String items. 为字符串项创建新的构建器。
      Returns:
      a new builder | 新的构建器
    • builder

      public static <T> FuzzyMatcher.Builder<T> builder(Function<T,String> keyExtractor)
      Creates a new builder with custom key extractor. 使用自定义键提取器创建新的构建器。
      Type Parameters:
      T - the item type | 项目类型
      Parameters:
      keyExtractor - function to extract string key from item | 从项目中提取字符串键的函数
      Returns:
      a new builder | 新的构建器
    • match

      public List<FuzzyMatch<T>> match(String query)
      Finds all items that match the query above the threshold. 查找所有匹配度高于阈值的项目。
      Parameters:
      query - the search query | 搜索查询
      Returns:
      list of matches sorted by similarity (descending) | 按相似度降序排列的匹配列表
    • matchBest

      public Optional<FuzzyMatch<T>> matchBest(String query)
      Finds the best matching item. 查找最佳匹配项。
      Parameters:
      query - the search query | 搜索查询
      Returns:
      the best match, or empty if no match above threshold | 最佳匹配,如果没有高于阈值的匹配则为空
    • suggest

      public List<T> suggest(String query)
      Gets search suggestions for the query. 获取查询的搜索建议。
      Parameters:
      query - the search query | 搜索查询
      Returns:
      list of suggested items | 建议项目列表
    • suggestStrings

      public List<String> suggestStrings(String query)
      Gets search suggestions as strings. 获取字符串形式的搜索建议。
      Parameters:
      query - the search query | 搜索查询
      Returns:
      list of suggested strings | 建议字符串列表
    • hasMatch

      public boolean hasMatch(String query)
      Checks if any item matches the query. 检查是否有任何项目匹配查询。
      Parameters:
      query - the search query | 搜索查询
      Returns:
      true if at least one match exists | 如果至少存在一个匹配则返回true
    • size

      public int size()
      Returns the number of items in the matcher. 返回匹配器中的项目数。
      Returns:
      item count | 项目数