Merge pull request #10 from aditya-K2/fuzzy

Fuzzy Searching
This commit is contained in:
Aditya Kurdunkar 2021-11-29 15:07:13 +05:30 committed by GitHub
commit 49d7063399
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 24 additions and 108 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module github.com/aditya-K2/goMP
go 1.16 go 1.16
require ( require (
github.com/aditya-K2/fuzzy v0.1.1-0.20211128173834-d0023b66cdd4 // indirect
github.com/aditya-K2/tview v0.0.0-20211115161300-6b99c2c2658c github.com/aditya-K2/tview v0.0.0-20211115161300-6b99c2c2658c
github.com/bogem/id3v2 v1.2.0 github.com/bogem/id3v2 v1.2.0
github.com/fhs/gompd v1.0.1 github.com/fhs/gompd v1.0.1

3
go.sum
View File

@ -52,6 +52,8 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym
github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e h1:4ZrkT/RzpnROylmoQL57iVUL57wGKTR5O6KpVnbm2tA= github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e h1:4ZrkT/RzpnROylmoQL57iVUL57wGKTR5O6KpVnbm2tA=
github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k= github.com/BurntSushi/xgbutil v0.0.0-20160919175755-f7c97cef3b4e/go.mod h1:uw9h2sd4WWHOPdJ13MQpwK5qYWKYDumDqxWWIknEQ+k=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/aditya-K2/fuzzy v0.1.1-0.20211128173834-d0023b66cdd4 h1:GEy4yHiudZCZyhwhP/2IWkcHCSkuUv/A4vKgktkIRYE=
github.com/aditya-K2/fuzzy v0.1.1-0.20211128173834-d0023b66cdd4/go.mod h1:82s+GWKOo176xBYRWstoAM6Fl5jpXNuZ1syuJvRwVN8=
github.com/aditya-K2/tview v0.0.0-20211115155623-217bcbdc952c h1:HE/P4zSd5GE7wdCicacdo5m2vZk6E7BSS5NOY00gaPM= github.com/aditya-K2/tview v0.0.0-20211115155623-217bcbdc952c h1:HE/P4zSd5GE7wdCicacdo5m2vZk6E7BSS5NOY00gaPM=
github.com/aditya-K2/tview v0.0.0-20211115155623-217bcbdc952c/go.mod h1:nPfBlFYx4SBcLlONif45KWw2mvEbRgP/nNCkH/dMhIQ= github.com/aditya-K2/tview v0.0.0-20211115155623-217bcbdc952c/go.mod h1:nPfBlFYx4SBcLlONif45KWw2mvEbRgP/nNCkH/dMhIQ=
github.com/aditya-K2/tview v0.0.0-20211115161300-6b99c2c2658c h1:UG9OxyXccanOiC83FEonB3+iLYvA3aAYgiX1N7Vurm0= github.com/aditya-K2/tview v0.0.0-20211115161300-6b99c2c2658c h1:UG9OxyXccanOiC83FEonB3+iLYvA3aAYgiX1N7Vurm0=
@ -217,6 +219,7 @@ github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfn
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls= github.com/magiconair/properties v1.8.5 h1:b6kJs+EmPFMYGkow9GiUyCyOvIwYetYJ3fSaWak/Gls=

24
main.go
View File

@ -1,13 +1,12 @@
package main package main
import ( import (
"sort"
"strconv" "strconv"
"time" "time"
"github.com/aditya-K2/fuzzy"
"github.com/aditya-K2/goMP/cache" "github.com/aditya-K2/goMP/cache"
"github.com/aditya-K2/goMP/config" "github.com/aditya-K2/goMP/config"
"github.com/aditya-K2/goMP/search"
"github.com/fhs/gompd/mpd" "github.com/fhs/gompd/mpd"
"github.com/gdamore/tcell/v2" "github.com/gdamore/tcell/v2"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -58,6 +57,7 @@ func main() {
Repeat, _ = strconv.ParseBool(_v["repeat"]) Repeat, _ = strconv.ParseBool(_v["repeat"])
ARTIST_TREE, err = GenerateArtistTree() ARTIST_TREE, err = GenerateArtistTree()
ARTIST_TREE_CONTENT := ConvertToArray()
NOTIFICATION_SERVER = NewNotificationServer() NOTIFICATION_SERVER = NewNotificationServer()
NOTIFICATION_SERVER.Start() NOTIFICATION_SERVER.Start()
@ -201,26 +201,14 @@ func main() {
UI.SearchBar.SetAutocompleteFunc(func(c string) []string { UI.SearchBar.SetAutocompleteFunc(func(c string) []string {
if c != "" && c != " " && c != " " { if c != "" && c != " " && c != " " {
var p search.PairList _, _, w, _ := UI.SearchBar.GetRect()
for k2, v := range ARTIST_TREE { matches := fuzzy.Find(c, ARTIST_TREE_CONTENT)
p = append(p, search.Pair{Key: k2, Value: search.GetLevenshteinDistance(c, k2)})
for k1, v1 := range v {
p = append(p, search.Pair{Key: k1, Value: search.GetLevenshteinDistance(c, k1)})
for k := range v1 {
p = append(p, search.Pair{Key: k, Value: search.GetLevenshteinDistance(c, k)})
}
}
}
sort.Sort(p)
var suggestions []string var suggestions []string
i := 0 for i, match := range matches {
for _, k := range p {
if i == 10 { if i == 10 {
break break
} }
_, _, w, _ := UI.SearchBar.GetRect() suggestions = append(suggestions, getFormattedString(match.Str, w-2))
suggestions = append(suggestions, getFormattedString(k.Key, w-2))
i++
} }
return suggestions return suggestions
} else { } else {

View File

@ -1,90 +0,0 @@
package search
import (
"fmt"
"math"
"math/bits"
"strings"
)
type Pair struct {
Key string
Value int
}
type PairList []Pair
func (p PairList) Len() int { return len(p) }
func (p PairList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p PairList) Less(i, j int) bool { return p[i].Value < p[j].Value }
func ConvertToBitMap(b string) uint64 {
b = strings.ToLower(b)
var a uint64 = 0
for _, i := range b {
asc := (int(i) - 97)
if asc >= 97 && asc <= 122 {
a |= 1 << asc
}
}
return a
}
func CalculateUpperBound(a, b string) float64 {
aB := ConvertToBitMap(a)
bB := ConvertToBitMap(b)
lengthAf64 := float64(len(a))
lengthBf64 := float64(len(b))
m := math.Min(lengthAf64, lengthBf64) - float64(bits.OnesCount64(aB&(^bB)))
return (1.0 / 3.0) * ((float64(m) / float64(len(a))) + (float64(m) / float64(len(b))) + 1)
}
func min(a, b, c int) int {
if a > b && b < c {
return b
} else if b > a && a < c {
return a
} else {
return c
}
}
func cost(i, j rune) int {
if i != j {
return 1
} else {
return 0
}
}
func GetLevenshteinDistance(a, b string) int {
c := []rune(a)
e := []rune(b)
m, n := len(c), len(e)
d := make([][]int, m+1)
for i := range d {
d[i] = make([]int, n+1)
}
for i := range d {
for j := range d[i] {
if j == 0 {
d[i][j] = i
} else if i == 0 {
d[i][j] = j
} else {
d[i][j] = 0
}
}
}
for j := 1; j < n+1; j++ {
for i := 1; i < m+1; i++ {
d[i][j] = min(d[i-1][j-1]+cost(c[i-1], e[j-1]),
d[i][j-1]+1,
d[i-1][j]+1)
}
}
return d[m][n]
}
func main() {
fmt.Println(GetLevenshteinDistance("cat", "wildcat"))
}

View File

@ -67,6 +67,20 @@ func getText(width, percentage float64, eta string) string {
return q return q
} }
func ConvertToArray() []string {
var p []string
for k2, v := range ARTIST_TREE {
p = append(p, k2)
for k1, v1 := range v {
p = append(p, k1)
for k := range v1 {
p = append(p, k)
}
}
}
return p
}
func formatString(a interface{}) string { func formatString(a interface{}) string {
if a == "play" { if a == "play" {
return "Playing" return "Playing"