Improved wording and variable names.

This commit is contained in:
David Alasow 2018-12-20 12:32:01 +01:00
parent 948c861e3a
commit 9a473c9887
1 changed files with 13 additions and 22 deletions

View File

@ -1,21 +1,20 @@
package search
import (
"fmt"
"sort"
"strings"
)
// ID ...
type ID struct {
ID int
Key interface{}
Score float64
Title []string
}
// Search ...
type Search struct {
Titles map[interface{}][][]string
Words map[interface{}][][]string
IgnoreWords []string
RemoveCharacters []string
}
@ -43,14 +42,12 @@ func (DB *Search) CleanSearch(title string) []string {
return s
}
// IndexSearch ...
func (DB *Search) IndexSearch(id interface{}, st [][]string) {
if DB.Titles == nil {
DB.Titles = make(map[interface{}][][]string)
}
for _, t := range st {
DB.Titles[id] = append(DB.Titles[id], t)
// Add ...
func (DB *Search) Add(key interface{}, st [][]string) {
if DB.Words == nil {
DB.Words = make(map[interface{}][][]string)
}
DB.Words[key] = append(DB.Words[key], st...)
}
@ -69,30 +66,24 @@ func (DB *Search) Match(Title string) []interface{} {
IDS := []ID{}
title := DB.CleanSearch(strings.ToLower(Title))
mainLoop:
for i := range DB.Titles {
for _, t := range DB.Titles[i] {
if len(t) == 0 {
continue
}
for k, v := range DB.Words {
for _, t := range v {
sim := similarity(t, title)
if sim > 0 {
IDS = append(IDS, ID{ID: i.(int), Score: sim, Title: t})
IDS = append(IDS, ID{Key: k, Score: sim, Title: t})
continue mainLoop
}
}
}
fmt.Println(DB.Titles[1])
fmt.Println(DB.Titles[`1`])
sort.Slice(IDS, func(i, j int) bool {
return IDS[i].Score > IDS[j].Score
})
ids := []interface{}{}
keys := []interface{}{}
for _, v := range IDS {
ids = append(ids, v.ID)
keys = append(keys, v.Key)
}
return ids
return keys
}
func similarity(a, b []string) float64 {