PgRest.SchemaGraph.Fuzzy (PgRest v0.2.0)

Copy Markdown View Source

Fuzzy name matching for the "Perhaps you meant ..." hints, modelled on the fuzzyset library PostgREST uses: gram-count cosine similarity (gram size 3, then 2), rescored with a Levenshtein ratio.

Summary

Functions

The best match scoring at least min_score (default 0.33), or nil.

Returns [{score, candidate}], best first, for candidates scoring at least 0.33 against value.

Like get/2 with a caller-chosen minimum score: PostgREST asks for 0.75 when suggesting a table or function name and 0.33 for a parameter list. The first gram size with any candidate at or above min_score wins.

render.(best) for the closest candidate scoring at least min_score, or nil: the shape of every "Perhaps you meant ..." hint.

Levenshtein edit distance between two strings (by grapheme).

Functions

closest(candidates, value, min_score \\ 0.33)

@spec closest([String.t()], String.t(), float()) :: String.t() | nil

The best match scoring at least min_score (default 0.33), or nil.

get(candidates, value)

@spec get([String.t()], String.t()) :: [{float(), String.t()}]

Returns [{score, candidate}], best first, for candidates scoring at least 0.33 against value.

get(candidates, value, min_score)

@spec get([String.t()], String.t(), float()) :: [{float(), String.t()}]

Like get/2 with a caller-chosen minimum score: PostgREST asks for 0.75 when suggesting a table or function name and 0.33 for a parameter list. The first gram size with any candidate at or above min_score wins.

hint(candidates, value, min_score, render)

@spec hint([String.t()], String.t(), float(), (String.t() -> String.t())) ::
  String.t() | nil

render.(best) for the closest candidate scoring at least min_score, or nil: the shape of every "Perhaps you meant ..." hint.

levenshtein(left, right)

@spec levenshtein(String.t(), String.t()) :: non_neg_integer()

Levenshtein edit distance between two strings (by grapheme).