Made ignored words and replaced characters a setting

This commit is contained in:
David Alasow 2018-12-20 10:37:44 +01:00
parent 3f2bd39bcf
commit 948c861e3a

View File

@ -1,13 +1,11 @@
package search
import (
"fmt"
"sort"
"strings"
)
var ignoreWords = []string{`a`, `of`, `in`, `the`, `wa`, `ga`, `no`, `ni`, `wo`, `he`, `o`, `ka`}
var replaceCharacters = strings.Split(`〜☆♪・〈〉「」!『』²Ⅱ+[](),.!?\/{}+-_=~"'@#$%^&*|;:<>`, ``)
// ID ...
type ID struct {
ID int
@ -23,9 +21,9 @@ type Search struct {
}
// CleanSearch ...
func CleanSearch(title string) []string {
func (DB *Search) CleanSearch(title string) []string {
s := []string{}
for _, c := range replaceCharacters {
for _, c := range DB.RemoveCharacters {
title = strings.Replace(title, c, ` `, -1)
}
for _, v := range strings.Split(title, ` `) {
@ -33,7 +31,7 @@ func CleanSearch(title string) []string {
if v == `` || v == ` ` {
continue
}
for _, w := range ignoreWords {
for _, w := range DB.IgnoreWords {
if v == w {
matched = true
}
@ -56,10 +54,20 @@ func (DB *Search) IndexSearch(id interface{}, st [][]string) {
}
// Ignore ...
func (DB *Search) Ignore(list []string) {
DB.IgnoreWords = list
}
// Replace ...
func (DB *Search) Replace(list []string) {
DB.RemoveCharacters = list
}
// Match ...
func (DB *Search) Match(Title string) []interface{} {
IDS := []ID{}
title := CleanSearch(strings.ToLower(Title))
title := DB.CleanSearch(strings.ToLower(Title))
mainLoop:
for i := range DB.Titles {
for _, t := range DB.Titles[i] {
@ -74,6 +82,8 @@ 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
})