Class FuzzyMatcher<T>
java.lang.Object
cloud.opencode.base.string.match.FuzzyMatcher<T>
- Type Parameters:
T- the type of items to match | 要匹配的项目类型
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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder for FuzzyMatcher.static enumMatching algorithm enumeration. -
Method Summary
Modifier and TypeMethodDescriptionstatic FuzzyMatcher.Builder<String> builder()Creates a new builder for String items.static <T> FuzzyMatcher.Builder<T> Creates a new builder with custom key extractor.booleanChecks if any item matches the query.List<FuzzyMatch<T>> Finds all items that match the query above the threshold.Finds the best matching item.intsize()Returns the number of items in the matcher.Gets search suggestions for the query.suggestStrings(String query) Gets search suggestions as strings.
-
Method Details
-
builder
Creates a new builder for String items. 为字符串项创建新的构建器。- Returns:
- a new builder | 新的构建器
-
builder
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
Finds all items that match the query above the threshold. 查找所有匹配度高于阈值的项目。- Parameters:
query- the search query | 搜索查询- Returns:
- list of matches sorted by similarity (descending) | 按相似度降序排列的匹配列表
-
matchBest
Finds the best matching item. 查找最佳匹配项。- Parameters:
query- the search query | 搜索查询- Returns:
- the best match, or empty if no match above threshold | 最佳匹配,如果没有高于阈值的匹配则为空
-
suggest
-
suggestStrings
-
hasMatch
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 | 项目数
-