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