MCPcopy Create free account
hub / github.com/TruthHun/DocHub / AddWords

Method AddWords

models/WordModel.go:35–80  ·  view source on GitHub ↗

添加关键字,多个关键字用英文逗号分隔

(wds string, id interface{})

Source from the content-addressed store, hash-verified

33
34//添加关键字,多个关键字用英文逗号分隔
35func (this *Word) AddWords(wds string, id interface{}) {
36 var (
37 wdMap = make(map[string]string) //词汇map
38 wdSlice []interface{} //词汇切片
39 wdData []Word //词汇数据
40 o = orm.NewOrm()
41 )
42 slice := strings.Split(wds, ",")
43 if len(slice) > 0 {
44 for _, wd := range slice {
45 wd = strings.TrimSpace(wd)
46 cnt := strings.Count(wd, "") - 1
47 if cnt > 1 && cnt <= 20 { //2-20个字符
48 wdMap[wd] = wd
49 wdSlice = append(wdSlice, wd)
50 }
51 }
52 o.QueryTable(GetTableWord()).Filter("Wd__in", wdSlice...).All(&wdData)
53 for _, w := range wdData { //存在数据,则更新
54 w.Cnt = w.Cnt + 1
55 w.Ids = fmt.Sprintf("%v,%v", w.Ids, id)
56 if _, err := o.Update(&w, "Ids", "Cnt"); err == nil { //更新分词数据
57 //删除map
58 delete(wdMap, w.Wd)
59 } else {
60 helper.Logger.Error(err.Error())
61 }
62 }
63 if len(wdMap) > 0 { //还存在数据,则新增
64 var wdDataAdd []Word
65 for _, w := range wdMap {
66 wdDataAdd = append(wdDataAdd, Word{
67 Wd: w,
68 Cnt: 1,
69 Ids: fmt.Sprintf("%v", id),
70 Status: true,
71 })
72 }
73 if len(wdDataAdd) > 0 {
74 if _, err := o.InsertMulti(len(wdDataAdd), wdDataAdd); err != nil {
75 helper.Logger.Error(err.Error())
76 }
77 }
78 }
79 }
80}

Callers

nothing calls this directly

Calls 4

GetTableWordFunction · 0.85
CountMethod · 0.80
AllMethod · 0.80
UpdateMethod · 0.80

Tested by

no test coverage detected