Made ignored words and replaced characters a setting
This commit is contained in:
parent
3f2bd39bcf
commit
948c861e3a
24
search.go
24
search.go
@ -1,13 +1,11 @@
|
|||||||
package search
|
package search
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var ignoreWords = []string{`a`, `of`, `in`, `the`, `wa`, `ga`, `no`, `ni`, `wo`, `he`, `o`, `ka`}
|
|
||||||
var replaceCharacters = strings.Split(`〜☆♪・〈〉「」!『』²Ⅱ+[](),.!?\/{}+-_=~"'@#$%^&*|;:<>`, ``)
|
|
||||||
|
|
||||||
// ID ...
|
// ID ...
|
||||||
type ID struct {
|
type ID struct {
|
||||||
ID int
|
ID int
|
||||||
@ -23,9 +21,9 @@ type Search struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CleanSearch ...
|
// CleanSearch ...
|
||||||
func CleanSearch(title string) []string {
|
func (DB *Search) CleanSearch(title string) []string {
|
||||||
s := []string{}
|
s := []string{}
|
||||||
for _, c := range replaceCharacters {
|
for _, c := range DB.RemoveCharacters {
|
||||||
title = strings.Replace(title, c, ` `, -1)
|
title = strings.Replace(title, c, ` `, -1)
|
||||||
}
|
}
|
||||||
for _, v := range strings.Split(title, ` `) {
|
for _, v := range strings.Split(title, ` `) {
|
||||||
@ -33,7 +31,7 @@ func CleanSearch(title string) []string {
|
|||||||
if v == `` || v == ` ` {
|
if v == `` || v == ` ` {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, w := range ignoreWords {
|
for _, w := range DB.IgnoreWords {
|
||||||
if v == w {
|
if v == w {
|
||||||
matched = true
|
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 ...
|
// Match ...
|
||||||
func (DB *Search) Match(Title string) []interface{} {
|
func (DB *Search) Match(Title string) []interface{} {
|
||||||
IDS := []ID{}
|
IDS := []ID{}
|
||||||
title := CleanSearch(strings.ToLower(Title))
|
title := DB.CleanSearch(strings.ToLower(Title))
|
||||||
mainLoop:
|
mainLoop:
|
||||||
for i := range DB.Titles {
|
for i := range DB.Titles {
|
||||||
for _, t := range DB.Titles[i] {
|
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 {
|
sort.Slice(IDS, func(i, j int) bool {
|
||||||
return IDS[i].Score > IDS[j].Score
|
return IDS[i].Score > IDS[j].Score
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user