2

I am wondering if there is a package available to help create something similar to what git has built in when you misspell a command, and it suggests existing commands. I am currently writing a CLI, and one of the features of the CLI is dynamically defining commands to dispatch here is the link for context, so misspellings are quite common.

Ideally I am looking for a way to pass a word, with a list of target words and get suggestions based on how close the input word is to one of the target words.

So for example:

def suggest(input_word:str, valid_words:list) -> str:
  suggestion = "" # The word in list valid_words that input_word is closest to 
  ... # Do stuff
  return suggestion

Which can be used like so:

suggest("biild", ["build", "init", "preview"]) # Returns "build"

Then with the cli I can use the function to print a message if a command does not exist and is likely misspelled like:

$ ahd biild
Command "biild" does not exist, did you mean:
    
    ahd build

I am not even looking for a full implementation, even just a suggestion on libraries that could help to do the string similarity check would be appreciated. Everything I have found so far is only a CLI with no API to hook into.

Kieran Wood
  • 1,297
  • 8
  • 15
  • 2
    [This](https://github.com/seatgeek/fuzzywuzzy) looks promising. I used something similar in the far past. – edd Aug 07 '21 at 19:01
  • For anyone wondering I wrote an in-depth gist based on the responses of @edd and the suggested solution above https://gist.github.com/Descent098/dae85d0235acce5322bf1277d1372a7e – Kieran Wood Aug 08 '21 at 19:24

0 Answers0