words: new package Damerau Levenshtein distance function. (#3929)

master
Bala FA 8 years ago committed by Harshavardhana
parent 1c97dcb10a
commit 7ebf11b202
  1. 3
      cmd/main.go
  2. 10
      pkg/words/damerau-levenshtein.go
  3. 4
      pkg/words/damerau-levenshtein_test.go

@ -23,6 +23,7 @@ import (
"github.com/minio/cli" "github.com/minio/cli"
"github.com/minio/mc/pkg/console" "github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/trie" "github.com/minio/minio/pkg/trie"
"github.com/minio/minio/pkg/words"
) )
// global flags for minio. // global flags for minio.
@ -86,7 +87,7 @@ func newApp() *cli.App {
} }
// 2 is arbitrary and represents the max // 2 is arbitrary and represents the max
// allowed number of typed errors // allowed number of typed errors
if DamerauLevenshteinDistance(command, value.(string)) < 2 { if words.DamerauLevenshteinDistance(command, value.(string)) < 2 {
closestCommands = append(closestCommands, value.(string)) closestCommands = append(closestCommands, value.(string))
} }
} }

@ -1,5 +1,5 @@
/* /*
* Minio Client (C) 2014-2016 Minio, Inc. * Minio Client (C) 2014, 2015, 2016, 2017 Minio, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,11 +14,9 @@
* limitations under the License. * limitations under the License.
*/ */
package cmd package words
import ( import "math"
"math"
)
// Returns the minimum value of a slice of integers // Returns the minimum value of a slice of integers
func minimum(integers []int) (minVal int) { func minimum(integers []int) (minVal int) {
@ -34,6 +32,7 @@ func minimum(integers []int) (minVal int) {
// DamerauLevenshteinDistance calculates distance between two strings using an algorithm // DamerauLevenshteinDistance calculates distance between two strings using an algorithm
// described in https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance // described in https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance
func DamerauLevenshteinDistance(a string, b string) int { func DamerauLevenshteinDistance(a string, b string) int {
var cost int
d := make([][]int, len(a)+1) d := make([][]int, len(a)+1)
for i := 1; i <= len(a)+1; i++ { for i := 1; i <= len(a)+1; i++ {
d[i-1] = make([]int, len(b)+1) d[i-1] = make([]int, len(b)+1)
@ -44,7 +43,6 @@ func DamerauLevenshteinDistance(a string, b string) int {
for j := 0; j <= len(b); j++ { for j := 0; j <= len(b); j++ {
d[0][j] = j d[0][j] = j
} }
var cost int
for i := 1; i <= len(a); i++ { for i := 1; i <= len(a); i++ {
for j := 1; j <= len(b); j++ { for j := 1; j <= len(b); j++ {
if a[i-1] == b[j-1] { if a[i-1] == b[j-1] {

@ -1,5 +1,5 @@
/* /*
* Minio Cloud Storage, (C) 2016 Minio, Inc. * Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -14,7 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
package cmd package words
import ( import (
"math" "math"
Loading…
Cancel
Save