ArguMend.extract_close_matchesMethod
extract_close_matches(key, candidates; n=3, cutoff=0.6)

Finds and returns up to n close matches from candidates for a given key based on a similarity ratio. The similarity ratio is calculated using the similarity_ratio function, which compares matching subsequences.

Arguments

  • key: The string or sequence for which close matches are sought.
  • candidates: An array of strings or sequences against which the key is compared.

Optional keywords

  • n: The maximum number of close matches to return (default is 3).
  • cutoff: The minimum similarity ratio required for a candidate to be considered a close match (default is 0.6).

Returns

  • An array of up to n candidates that have a similarity ratio above the cutoff.

Examples

julia> mistyped_kw = "iterations";

julia> candidate_kws = ["niterations", "ncycles_per_iteration", "niterations_per_cycle", "abcdef", "iter"];

julia> extract_close_matches(mistyped_kw, candidate_kws)
["niterations", "niterations_per_cycle"]
ArguMend.suggest_alternative_kwsMethod
suggest_alternative_kws(kws, possible_kws; n_suggestions = 3, cutoff = 0.6)

Suggest alternative keywords, where kws is the list of keyword names passed to a function, and possible_kws is the list of true keyword names. These should be either symbols or strings. You can get this from the function kws by using keys(kws).

Examples

function f(; kws...)
    msg = suggest_alternative_kws(keys(kws), [:kw1, :kw2])
    if isempty(msg)
        return "all keywords are valid"
    else
        return "received some invalid keywords, here is the error:" * msg
    end
end
ArguMend.@argumendMacro
@argumend [n_suggestions=3] [cutoff=0.6] funcdef

This macro lets you automatically suggest similarly-spelled keywords:

@argumend function f(a, b; niterations=10, kw2=2)
    a + b - niterations + kw2
end

This results in a nicer mechanism for MethodErrors:

julia> f(1, 2; iterations=1)
ERROR: SuggestiveMethodError: in call to `f`, found unsupported
       keyword argument: `iterations`, perhaps you meant `niterations`

This function computes closeness between the mistyped keyword argument by counting the maximum number of matching subsequences with all the other keyword arguments.

You can customize the number of suggestions by specifying it in the macro. You can also control the closeness threshold with cutoff.