Skip to content

Commit

Permalink
words: new package Damerau Levenshtein distance function. (minio#3929)
Browse files Browse the repository at this point in the history
  • Loading branch information
balamurugana authored and harshavardhana committed Mar 19, 2017
1 parent 1c97dcb commit 7ebf11b
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/minio/cli"
"github.com/minio/mc/pkg/console"
"github.com/minio/minio/pkg/trie"
"github.com/minio/minio/pkg/words"
)

// global flags for minio.
Expand Down Expand Up @@ -86,7 +87,7 @@ func newApp() *cli.App {
}
// 2 is arbitrary and represents the max
// allowed number of typed errors
if DamerauLevenshteinDistance(command, value.(string)) < 2 {
if words.DamerauLevenshteinDistance(command, value.(string)) < 2 {
closestCommands = append(closestCommands, value.(string))
}
}
Expand Down
10 changes: 4 additions & 6 deletions cmd/damerau-levenshtein.go → pkg/words/damerau-levenshtein.go
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -14,11 +14,9 @@
* limitations under the License.
*/

package cmd
package words

import (
"math"
)
import "math"

// Returns the minimum value of a slice of integers
func minimum(integers []int) (minVal int) {
Expand All @@ -34,6 +32,7 @@ func minimum(integers []int) (minVal int) {
// DamerauLevenshteinDistance calculates distance between two strings using an algorithm
// described in https://en.wikipedia.org/wiki/Damerau-Levenshtein_distance
func DamerauLevenshteinDistance(a string, b string) int {
var cost int
d := make([][]int, len(a)+1)
for i := 1; i <= len(a)+1; i++ {
d[i-1] = make([]int, len(b)+1)
Expand All @@ -44,7 +43,6 @@ func DamerauLevenshteinDistance(a string, b string) int {
for j := 0; j <= len(b); j++ {
d[0][j] = j
}
var cost int
for i := 1; i <= len(a); i++ {
for j := 1; j <= len(b); j++ {
if a[i-1] == b[j-1] {
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
* you may not use this file except in compliance with the License.
Expand All @@ -14,7 +14,7 @@
* limitations under the License.
*/

package cmd
package words

import (
"math"
Expand Down

0 comments on commit 7ebf11b

Please sign in to comment.