Skip to content

Suggest the correct name when no key matches in the dataset #9943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jan 17, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add "did you mean" function
  • Loading branch information
Illviljan committed Jan 12, 2025
commit fc552711dd4baaa4278fcb41f00aff46be7ec605
25 changes: 25 additions & 0 deletions xarray/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,31 @@ def wrapper(*args, **kwargs):
return wrapper


def did_you_mean(word: Hashable, possibilities: Iterable[Hashable]) -> str:
"""
Get a suggested word.

Examples
--------
>>> did_you_mean("bluch", ("blech", "gray_r", 1, None, (2, 56)))
"Did you mean one of ('blech',)?"
>>> did_you_mean(1, ("blech", "gray_r", 1, None, (2, 56)))
'Did you mean one of (1,)?'
"""
import difflib

possibilites_str: dict[str, Hashable] = {str(k): k for k in possibilities}

msg = ""
if len(
best_str := difflib.get_close_matches(str(word), list(possibilites_str.keys()))
):
best = tuple(possibilites_str[k] for k in best_str)
msg = f"Did you mean one of {best}?"

return msg


def get_valid_numpy_dtype(array: np.ndarray | pd.Index) -> np.dtype:
"""Return a numpy compatible dtype from either
a numpy array or a pandas.Index.
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy