suggestions
lacuna.utils.suggestions
¶
Fuzzy string matching utilities for improved error messages.
This module provides helpers for suggesting similar strings when users make typos or mistakes in key/attribute names. Used to enhance error messages with "Did you mean?" suggestions.
Examples:
>>> from lacuna.utils.suggestions import suggest_similar
>>> available = ["rmap", "zscoremap", "damagescore"]
>>> suggestions = suggest_similar("rmp", available, max_suggestions=2)
>>> suggestions
['rmap']
format_suggestions(suggestions)
¶
Format a list of suggestions for inclusion in an error message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
suggestions
|
list[str]
|
List of suggested strings. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Formatted string for error message, or empty string if no suggestions. |
Examples:
Source code in src/lacuna/utils/suggestions.py
suggest_similar(query, candidates, max_suggestions=3, min_similarity=0.4)
¶
Find candidates most similar to the query string.
Uses difflib.SequenceMatcher for similarity scoring. Results are sorted by similarity (most similar first) and filtered by minimum threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
The string to find matches for (e.g., user's typo). |
required |
candidates
|
list[str]
|
Available options to suggest from. |
required |
max_suggestions
|
int
|
Maximum number of suggestions to return. |
3
|
min_similarity
|
float
|
Minimum similarity ratio (0.0 to 1.0) to include a suggestion. Higher values require closer matches. |
0.4
|
Returns:
| Type | Description |
|---|---|
list[str]
|
Up to |
Examples: