Skip to content

Commit

Permalink
changed formating of message generated by did_you_mean_dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
advieser committed Oct 2, 2024
1 parent fe20c7d commit 6abb5ce
Showing 1 changed file with 35 additions and 30 deletions.
65 changes: 35 additions & 30 deletions R/did_you_mean.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ did_you_mean = function(str, candidates) {
sprintf(" Did you mean %s?", str_collapse(suggestions, quote = "'", sep = " / "))
}

#' @title Suggest Alternatives from Given Dictionaries
#'
#' @description
#' Helps to suggest alternatives for a given key based on the keys of given dictionaries.
#'
#' @param key (`character(1)`) \cr
#' Key to look for in `dicts`.
#' @param dicts (named list)\cr
#' Named list of [dictionaries][Dictionary].
#' @return (`character(1)`). Either a phrase suggesting one or more keys based on the dictionaries in `dicts`,
#' or an empty string if no close match is found.
did_you_mean_dicts = function(key, dicts) {
# @title Suggest Alternatives from Given Dictionaries
#
# @description
# Helps to suggest alternatives for a given key based on the keys of given dictionaries.
#
# @param key (`character(1)`) \cr
# Key to look for in `dicts`.
# @param dicts (named list)\cr
# Named list of [dictionaries][Dictionary].
# @param max_candidates_dicts (`integer(1)`) \cr
# Maximum number of dictionaries for which suggestions are outputted.
# @return (`character(1)`). Either a phrase suggesting one or more keys based on the dictionaries in `dicts`,
# or an empty string if no close match is found.
did_you_mean_dicts = function(key, dicts, max_candidates_dicts = 3L) {
# No message if no dictionaries are given
if (is.null(dicts)) {
return("")
Expand All @@ -50,40 +52,43 @@ did_you_mean_dicts = function(key, dicts) {
if (!length(entries)) {
next
}

# Record the closest distance
min_distance_per_dict[[length(min_distance_per_dict) + 1]] = min(distances)

# Create a suggestion message for the current dictionary
suggestions[[length(suggestions) + 1]] = sprintf("%s: %s", names(dicts)[[i]],
str_collapse(entries, quote = "'", sep = " / "))
suggestions[[length(suggestions) + 1]] = sprintf(
"%s: %s", names(dicts)[[i]], str_collapse(entries, quote = "'", sep = " / ")
)
}

# Order the suggestions by their closest match
suggestions = suggestions[order(min_distance_per_dict)]
# Only show the 3 dictionaries with the best matches
valid_suggestions = head(valid_suggestions, 3L)
suggestions = head(suggestions, max_candidates_dicts)

# If no valid suggestions, return an empty string
if (!length(valid_suggestions)) {
if (!length(suggestions)) {
return("")
}

sprintf("\nSimilar entries in other dictionaries, %s.", str_collapse(valid_suggestions, sep = ", or "))
# add \n
sprintf("\nSimilar entries in other dictionaries:\n %s", str_collapse(suggestions, sep = "\n "))
}

#' @title Find Suggestions
#'
#' @param str (`character(1)`)\cr
#' String.
#' @param candidates (`character()`)\cr
#' Candidate strings.
#' @param threshold (`numeric(1)`)\cr
#' Percentage value of characters when sorting `candidates` by distance
#' @param max_candidates (`integer(1)`)\cr
#' Maximum number of candidates to return.
#' @param ret_similarity (`logical(1)`)\cr
#' Return similarity values instead of names.
#' @return (`character(1)`). Either suggested candidates from `candidates` or an empty string if no close match is found.
# @title Find Suggestions
#
# @param str (`character(1)`)\cr
# String.
# @param candidates (`character()`)\cr
# Candidate strings.
# @param threshold (`numeric(1)`)\cr
# Percentage value of characters when sorting `candidates` by distance
# @param max_candidates (`integer(1)`)\cr
# Maximum number of candidates to return.
# @param ret_similarity (`logical(1)`)\cr
# Return similarity values instead of names.
# @return (`character(1)`). Either suggested candidates from `candidates` or an empty string if no close match is found.
find_suggestions = function(str, candidates, threshold = 0.2, max_candidates = 3L, ret_distances = FALSE) {
candidates = unique(candidates)
D = set_names(adist(str, candidates, ignore.case = TRUE, partial = TRUE)[1L, ], candidates)
Expand Down

0 comments on commit 6abb5ce

Please sign in to comment.