unstable: initial tags
This commit is contained in:
parent
2e71b60248
commit
b3b62af3a4
28 changed files with 503 additions and 91 deletions
|
|
@ -1,32 +0,0 @@
|
|||
// Copyright (C) 2019 Marius Schellenberger
|
||||
|
||||
package index
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
var classes = []class{
|
||||
{regexp.MustCompile(`(?i)[^e]rechnung|quittung|beleg|invoice|bill|check`), []string{"invoice", "rechnung"}},
|
||||
{regexp.MustCompile(`(?i)steuer|lohn|tax|salary`), []string{"tax", "steuer"}},
|
||||
{regexp.MustCompile(`(?i)vertrag|kündigung|vereinbarung|contract|termination|agreement`), []string{"contract", "vertrag"}},
|
||||
{regexp.MustCompile(`(?i)versicherung|versichert|insurance|insured`), []string{"insurance", "versicherung"}},
|
||||
}
|
||||
|
||||
type class struct {
|
||||
r *regexp.Regexp
|
||||
k []string
|
||||
}
|
||||
|
||||
func classify(text string) (keyword []string) {
|
||||
keyword = []string{"", ""}
|
||||
n := 0
|
||||
for _, v := range classes {
|
||||
num := len(v.r.FindAllString(text, -1))
|
||||
if num > n && num >= 2 {
|
||||
keyword = v.k
|
||||
n = num
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
@ -19,7 +19,7 @@ const docType = "document"
|
|||
|
||||
type document struct {
|
||||
Text string `json:"text"`
|
||||
Keyword []string `json:"keyword"`
|
||||
Tags []string `json:"tags"`
|
||||
Created time.Time `json:"created"`
|
||||
}
|
||||
|
||||
|
|
@ -61,7 +61,7 @@ func (i *Index) Close() error {
|
|||
return i.i.Close()
|
||||
}
|
||||
|
||||
func (i *Index) Add(text string) (id string, err error) {
|
||||
func (i *Index) Add(text string, tags []string) (id string, err error) {
|
||||
id = newID()
|
||||
for {
|
||||
doc, _ := i.i.Document(id)
|
||||
|
|
@ -70,8 +70,7 @@ func (i *Index) Add(text string) (id string, err error) {
|
|||
}
|
||||
id = newID()
|
||||
}
|
||||
k := classify(text)
|
||||
err = i.i.Index(id, &document{text, k, time.Now()})
|
||||
err = i.i.Index(id, &document{text, tags, time.Now()})
|
||||
return
|
||||
}
|
||||
|
||||
|
|
@ -97,12 +96,12 @@ func (i *Index) Search(search string) (results core.Results, err error) {
|
|||
continue
|
||||
}
|
||||
for fragField, frags := range hit.Fragments {
|
||||
if fragField == "keyword" {
|
||||
var k string
|
||||
if fragField == "tags" {
|
||||
var t string
|
||||
for _, f := range frags {
|
||||
k += f
|
||||
t += f
|
||||
}
|
||||
r.Keyword = strings.ReplaceAll(strings.ReplaceAll(k, "<mark>", ""), "</mark>", "")
|
||||
r.Tags = strings.ReplaceAll(strings.ReplaceAll(t, "<mark>", ""), "</mark>", "")
|
||||
}
|
||||
if fragField == "text" || fragField == "created" {
|
||||
for _, f := range frags {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue