ai_xiaopei
6 days ago 1196f409d86bc61e7596eb274840244a62ce84ba
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package search
 
import (
    "strings"
)
 
// GenericWords 通用词列表(降低权重避免匹配所有文件)
var GenericWords = []string{
    "问题", "故障", "报错", "异常", "无法", "不能",
    "怎么", "如何", "为什么", "是什么", "哪里", "什么",
    "可以", "需要", "应该", "是否", "有没有",
}
 
// EntityWords 实体词列表(进一步降低权重,因为这些词会匹配大量文件)
var EntityWords = []string{
    "智能枪", "电子秤", "智能阀", "艾信盒子",
    "电子秤平台", "运营管理平台", "易配送", "lpg",
    "安全用气", "艾信助手", "艾信发货",
}
 
// IsGenericWord 判断是否为通用词
func IsGenericWord(word string) bool {
    word = strings.ToLower(word)
    for _, g := range GenericWords {
        if word == g {
            return true
        }
    }
    return false
}
 
// IsEntityWord 判断是否为实体词
func IsEntityWord(word string) bool {
    wordLower := strings.ToLower(word)
    for _, e := range EntityWords {
        if wordLower == strings.ToLower(e) {
            return true
        }
    }
    return false
}
 
// ScoreType 评分类型
type ScoreType int
 
const (
    ScoreNormal  ScoreType = iota // 普通词
    ScoreExpanded                 // 扩展词
    ScoreSymptom                  // 症状词
)
 
// ScoreTable 评分表
var ScoreTable = map[ScoreType]map[string]int{
    ScoreNormal: {
        "path":    2,
        "title":   3,
        "tag":     2,
        "content": 1,
    },
    ScoreExpanded: {
        "path":    40,
        "title":   30,
        "tag":     20,
        "content": 5,
    },
    ScoreSymptom: {
        "path":    60,
        "title":   50,
        "tag":     35,
        "content": 15,
    },
}
 
// CalcScore 计算单个关键词在某个位置的得分
func CalcScore(keyword string, position string, scoreType ScoreType) int {
    base := ScoreTable[scoreType][position]
    if base == 0 {
        return 0
    }
    // 实体词降权(匹配太多文件),但保证至少 1 分
    if IsEntityWord(keyword) {
        score := base / 5
        if score < 1 {
            score = 1
        }
        return score
    }
    // 通用词降权,但保证至少 1 分
    if IsGenericWord(keyword) {
        score := base / 3
        if score < 1 {
            score = 1
        }
        return score
    }
    return base
}