-
Notifications
You must be signed in to change notification settings - Fork 745
/
Copy pathWordModel.go
80 lines (73 loc) · 2.14 KB
/
WordModel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package models
import (
"fmt"
"strings"
"github.com/TruthHun/DocHub/helper"
"github.com/astaxie/beego/orm"
)
// ============================ //
// 废弃!
// 相关文档的功能,以后通过 ElasticSearch 来实现
// 废弃!
// ============================ //
// 关键字记录表,后期用这个来做相关资源功能
type Word struct {
Id int `orm:"column(Id)"`
Wd string `orm:"column(Wd);size(20);unique"` //关键字
Cnt int `orm:"column(Cnt);default(0)"` //统计
Ids string `orm:"column(Ids);default();type(text)"` //存在该关键字的文档id
Status bool `orm:"column(Status);default(true)"` //bool值,默认该关键字合法,否则存在该关键字的都是不合法文档
}
func NewWord() *Word {
return &Word{}
}
func GetTableWord() string {
return getTable("word")
}
//添加关键字,多个关键字用英文逗号分隔
func (this *Word) AddWords(wds string, id interface{}) {
var (
wdMap = make(map[string]string) //词汇map
wdSlice []interface{} //词汇切片
wdData []Word //词汇数据
o = orm.NewOrm()
)
slice := strings.Split(wds, ",")
if len(slice) > 0 {
for _, wd := range slice {
wd = strings.TrimSpace(wd)
cnt := strings.Count(wd, "") - 1
if cnt > 1 && cnt <= 20 { //2-20个字符
wdMap[wd] = wd
wdSlice = append(wdSlice, wd)
}
}
o.QueryTable(GetTableWord()).Filter("Wd__in", wdSlice...).All(&wdData)
for _, w := range wdData { //存在数据,则更新
w.Cnt = w.Cnt + 1
w.Ids = fmt.Sprintf("%v,%v", w.Ids, id)
if _, err := o.Update(&w, "Ids", "Cnt"); err == nil { //更新分词数据
//删除map
delete(wdMap, w.Wd)
} else {
helper.Logger.Error(err.Error())
}
}
if len(wdMap) > 0 { //还存在数据,则新增
var wdDataAdd []Word
for _, w := range wdMap {
wdDataAdd = append(wdDataAdd, Word{
Wd: w,
Cnt: 1,
Ids: fmt.Sprintf("%v", id),
Status: true,
})
}
if len(wdDataAdd) > 0 {
if _, err := o.InsertMulti(len(wdDataAdd), wdDataAdd); err != nil {
helper.Logger.Error(err.Error())
}
}
}
}
}