Class OpenFuzzyMatch

java.lang.Object
cloud.opencode.base.string.match.OpenFuzzyMatch

public final class OpenFuzzyMatch extends Object
Fuzzy Match Facade - Provides convenient static methods for fuzzy matching 模糊匹配门面 - 提供便捷的模糊匹配静态方法

Quick access to fuzzy matching functionality without building a matcher.

无需构建匹配器即可快速访问模糊匹配功能。

Features | 主要功能:

  • Quick fuzzy search - 快速模糊搜索
  • Search suggestions - 搜索建议
  • Find best match - 查找最佳匹配
  • Typo correction - 拼写纠正

Usage Examples | 使用示例:

// Quick fuzzy search
List<String> results = OpenFuzzyMatch.search("aple",
    List.of("apple", "banana", "apply"));

// Find best match
String best = OpenFuzzyMatch.findBest("javscript",
    List.of("Java", "JavaScript", "Python"));

// Get suggestions with scores
List<FuzzyMatch<String>> suggestions = OpenFuzzyMatch.suggest("pythn",
    List.of("Python", "Ruby", "Perl"), 0.5);

// Did you mean?
String correction = OpenFuzzyMatch.didYouMean("recieve",
    List.of("receive", "believe", "achieve"));

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具类)
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(n * m) where n, m = string lengths (Levenshtein) - O(n * m), n, m为字符串长度(编辑距离)
  • Space complexity: O(min(n, m)) for DP row - 动态规划行 O(min(n, m))
Since:
JDK 25, opencode-base-string V1.2.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • search

      public static List<String> search(String query, Collection<String> candidates)
      Performs a quick fuzzy search. 执行快速模糊搜索。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates to search | 要搜索的候选项
      Returns:
      list of matching strings | 匹配字符串列表
    • search

      public static List<String> search(String query, Collection<String> candidates, double threshold)
      Performs a quick fuzzy search with custom threshold. 使用自定义阈值执行快速模糊搜索。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates to search | 要搜索的候选项
      threshold - the similarity threshold (0.0 - 1.0) | 相似度阈值
      Returns:
      list of matching strings | 匹配字符串列表
    • search

      public static <T> List<T> search(String query, Collection<T> candidates, Function<T,String> keyExtractor)
      Performs fuzzy search on objects with custom key extractor. 使用自定义键提取器对对象执行模糊搜索。
      Type Parameters:
      T - the item type | 项目类型
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates to search | 要搜索的候选项
      keyExtractor - function to extract search key | 提取搜索键的函数
      Returns:
      list of matching items | 匹配项目列表
    • search

      public static <T> List<T> search(String query, Collection<T> candidates, Function<T,String> keyExtractor, double threshold)
      Performs fuzzy search on objects with custom key extractor and threshold. 使用自定义键提取器和阈值对对象执行模糊搜索。
      Type Parameters:
      T - the item type | 项目类型
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates to search | 要搜索的候选项
      keyExtractor - function to extract search key | 提取搜索键的函数
      threshold - the similarity threshold | 相似度阈值
      Returns:
      list of matching items | 匹配项目列表
    • suggest

      public static List<FuzzyMatch<String>> suggest(String query, Collection<String> candidates, double threshold)
      Gets search suggestions with match details. 获取带有匹配详情的搜索建议。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates | 候选项
      threshold - the similarity threshold | 相似度阈值
      Returns:
      list of fuzzy matches | 模糊匹配列表
    • suggest

      public static List<FuzzyMatch<String>> suggest(String query, Collection<String> candidates)
      Gets search suggestions with default threshold. 使用默认阈值获取搜索建议。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates | 候选项
      Returns:
      list of fuzzy matches | 模糊匹配列表
    • findBest

      public static String findBest(String query, Collection<String> candidates)
      Finds the best matching string. 查找最佳匹配字符串。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates | 候选项
      Returns:
      the best match, or null if no match | 最佳匹配,如果没有匹配则返回null
    • findBest

      public static String findBest(String query, Collection<String> candidates, double threshold)
      Finds the best matching string with custom threshold. 使用自定义阈值查找最佳匹配字符串。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates | 候选项
      threshold - the minimum similarity threshold | 最小相似度阈值
      Returns:
      the best match, or null if no match above threshold | 最佳匹配,如果没有高于阈值的匹配则返回null
    • findBest

      public static <T> T findBest(String query, Collection<T> candidates, Function<T,String> keyExtractor)
      Finds the best matching item with custom key extractor. 使用自定义键提取器查找最佳匹配项。
      Type Parameters:
      T - the item type | 项目类型
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates | 候选项
      keyExtractor - function to extract search key | 提取搜索键的函数
      Returns:
      the best match, or null if no match | 最佳匹配,如果没有匹配则返回null
    • didYouMean

      public static String didYouMean(String misspelled, Collection<String> dictionary)
      Suggests a correction for a potentially misspelled string. 为可能拼写错误的字符串建议更正。
      Parameters:
      misspelled - the potentially misspelled string | 可能拼写错误的字符串
      dictionary - the dictionary of correct strings | 正确字符串的词典
      Returns:
      the suggested correction, or null if no good match | 建议的更正,如果没有好的匹配则返回null
    • didYouMean

      public static String didYouMean(String misspelled, Collection<String> dictionary, double threshold)
      Suggests a correction with custom threshold. 使用自定义阈值建议更正。
      Parameters:
      misspelled - the potentially misspelled string | 可能拼写错误的字符串
      dictionary - the dictionary of correct strings | 正确字符串的词典
      threshold - the minimum similarity threshold | 最小相似度阈值
      Returns:
      the suggested correction, or null if no good match | 建议的更正,如果没有好的匹配则返回null
    • similarity

      public static double similarity(String s1, String s2)
      Calculates the fuzzy similarity between two strings. 计算两个字符串之间的模糊相似度。
      Parameters:
      s1 - the first string | 第一个字符串
      s2 - the second string | 第二个字符串
      Returns:
      similarity score (0.0 - 1.0) | 相似度分数(0.0 - 1.0)
    • isSimilar

      public static boolean isSimilar(String s1, String s2, double threshold)
      Checks if two strings are similar above a threshold. 检查两个字符串是否高于阈值相似。
      Parameters:
      s1 - the first string | 第一个字符串
      s2 - the second string | 第二个字符串
      threshold - the similarity threshold | 相似度阈值
      Returns:
      true if similarity >= threshold | 如果相似度 >= 阈值则返回true
    • rankBySimilarity

      public static List<String> rankBySimilarity(String query, Collection<String> candidates)
      Ranks candidates by similarity to the query. 按与查询的相似度对候选项排名。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates to rank | 要排名的候选项
      Returns:
      candidates sorted by similarity (descending) | 按相似度降序排列的候选项
    • rankWithScores

      public static Map<String,Double> rankWithScores(String query, Collection<String> candidates)
      Ranks candidates by similarity with scores. 按相似度对候选项排名并附带分数。
      Parameters:
      query - the search query | 搜索查询
      candidates - the candidates to rank | 要排名的候选项
      Returns:
      map of candidate to similarity score | 候选项到相似度分数的映射