didyoumean

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 8, 2023 License: MIT Imports: 1 Imported by: 0

README

DidYouMean

codecov Go Report Card

Application Logo

DidYouMean is a Go package that suggests the most likely string based on the Levenshtein distance algorithm. It is particularly useful for correcting user typos in command-line tools or offering suggestions in search inputs.

Getting Started

Prerequisites

You need to have Go 1.15 or higher installed on your machine.

Installing

To use DidYouMean in your Go project, run:

go get -u github.com/AL-Kost/didyoumean

This will retrieve the library and add it to your project's dependencies.

Usage

Import DidYouMean in your Go code:

import "github.com/AL-Kost/didyoumean"

Example of suggesting a string:

package main

import (
	"fmt"
	"github.com/AL-Kost/didyoumean"
)

func main() {
	input := "appl"
	candidates := []string{"apple", "application", "apology", "apply", "happy"}
	threshold := 2
	caseInsensitive := true
	
	closestMatch := didyoumean.Suggest(input, candidates, threshold, caseInsensitive)
	
	if closestMatch != "" {
		fmt.Printf("Did you mean '%s'?\n", closestMatch)
	} else {
		fmt.Println("No close match found.")
	}
}

For more examples, please refer to the examples directory.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Suggest

func Suggest(input string, candidates []string, threshold int, caseInsensitive bool) string

Suggest finds the closest match for a given input string from a list of candidate strings. It uses the Levenshtein distance algorithm to determine the closest match. If no candidate is within the specified threshold, an empty string is returned. The comparison can be case-insensitive if the caseInsensitive flag is set.

Parameters: - input: the input string to match. - candidates: a slice of candidate strings to compare against the input. - threshold: the maximum Levenshtein distance considered for a match to be close enough. - caseInsensitive: if set to true, the matching will be case-insensitive.

Returns: - The closest match within the threshold, or an empty string if no match is close enough.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL