-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce sql transaction for data entry (#9)
Signed-off-by: Masudur Rahman <masudjuly02@gmail.com>
- Loading branch information
1 parent
a4e2382
commit 41412be
Showing
24 changed files
with
876 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package pkg | ||
|
||
import "strings" | ||
|
||
// LevenshteinDistance compares two strings and returns the levenshtein distance between them. | ||
func LevenshteinDistance(s, t string, ignoreCase bool) int { | ||
if ignoreCase { | ||
s = strings.ToLower(s) | ||
t = strings.ToLower(t) | ||
} | ||
d := make([][]int, len(s)+1) | ||
for i := range d { | ||
d[i] = make([]int, len(t)+1) | ||
} | ||
for i := range d { | ||
d[i][0] = i | ||
} | ||
for j := range d[0] { | ||
d[0][j] = j | ||
} | ||
for j := 1; j <= len(t); j++ { | ||
for i := 1; i <= len(s); i++ { | ||
if s[i-1] == t[j-1] { | ||
d[i][j] = d[i-1][j-1] | ||
} else { | ||
mn := d[i-1][j] | ||
if d[i][j-1] < mn { | ||
mn = d[i][j-1] | ||
} | ||
if d[i-1][j-1] < mn { | ||
mn = d[i-1][j-1] | ||
} | ||
d[i][j] = mn + 1 | ||
} | ||
} | ||
|
||
} | ||
return d[len(s)][len(t)] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package pkg_test | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/masudur-rahman/expense-tracker-bot/models" | ||
"github.com/masudur-rahman/expense-tracker-bot/pkg" | ||
) | ||
|
||
func ExampleLevenshteinDistance() { | ||
a := "snack" | ||
|
||
md := 10000 | ||
var rs string | ||
|
||
for _, subcat := range models.TxnSubcategories { | ||
ld := min(pkg.LevenshteinDistance(subcat.ID, a, true), | ||
pkg.LevenshteinDistance(subcat.Name, a, true)) | ||
suggestByLevenshtein := ld <= 3 | ||
suggestByPrefix := strings.HasPrefix(strings.ToLower(subcat.Name), strings.ToLower(a)) | ||
if suggestByLevenshtein && ld < md || suggestByPrefix { | ||
md = ld | ||
rs = subcat.Name | ||
} | ||
} | ||
|
||
fmt.Printf("Distance between `%v` and `%v` is %v\n", a, rs, md) | ||
// Output: Hello | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.