Use elixir internal distance function instead of a library for command similarity comparison.

Elixir provides String.jaro_distance function, which is similar to
the levenstain distance.
This allows us to remove a dependency.
This commit is contained in:
Daniil Fedotov 2019-02-12 15:22:02 -05:00
parent 9231b21305
commit 7edf3d87ca
2 changed files with 7 additions and 10 deletions

View File

@ -16,9 +16,8 @@
defmodule RabbitMQ.CLI.Core.Parser do defmodule RabbitMQ.CLI.Core.Parser do
alias RabbitMQ.CLI.Core.{CommandModules, Config} alias RabbitMQ.CLI.Core.{CommandModules, Config}
# This assumes the average word letter count in # Use the same jaro distance limit as in Elixir `did_you_mean`
# the English language is 5. @jaro_distance_limit 0.77
@levenshtein_distance_limit 5
def default_switches() do def default_switches() do
[ [
@ -167,19 +166,18 @@ defmodule RabbitMQ.CLI.Core.Parser do
nil nil
end end
defp command_suggestion(cmd_name, module_map) do defp command_suggestion(typed, module_map) do
suggestion = suggestion =
module_map module_map
|> Map.keys() |> Map.keys()
|> Enum.map(fn cmd -> |> Enum.map(fn existing ->
{cmd, Simetric.Levenshtein.compare(cmd, cmd_name)} {existing, String.jaro_distance(existing, typed)}
end) end)
|> Enum.min_by(fn {_, distance} -> distance end) |> Enum.max_by(fn {_, distance} -> distance end)
case suggestion do case suggestion do
{cmd, distance} when distance <= @levenshtein_distance_limit -> {cmd, distance} when distance >= @jaro_distance_limit ->
{:suggest, cmd} {:suggest, cmd}
_ -> _ ->
nil nil
end end

View File

@ -88,7 +88,6 @@ defmodule RabbitMQCtl.MixfileBase do
elixir_deps = [ elixir_deps = [
{:json, "~> 1.0.0"}, {:json, "~> 1.0.0"},
{:csv, "~> 2.0.0"}, {:csv, "~> 2.0.0"},
{:simetric, "~> 0.2.0"},
{:amqp, "~> 0.2.2", only: :test}, {:amqp, "~> 0.2.2", only: :test},
{:dialyxir, "~> 0.5", only: :test, runtime: false}, {:dialyxir, "~> 0.5", only: :test, runtime: false},