Repository navigation
Expand file tree
/
Copy pathsearch_signals.go
More file actions
85 lines (82 loc) · 2.48 KB
/
Copy pathsearch_signals.go
File metadata and controls
85 lines (82 loc) · 2.48 KB
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
package contexting
import (
"sort"
"strings"
)
// Each query token receives only its strongest synonym match. Repeating a
// concept across synonyms must not increase a file's relevance.
func scoreSynonymToken(synonyms []string, token string) (int, []string, []string) {
score := 0
var matches, breakdown []string
for _, synonym := range synonyms {
lower := strings.ToLower(synonym)
if lower == "" {
continue
}
if lower == token {
return 8, []string{"syn-exact:" + synonym}, []string{"syn exact +8: " + synonym}
}
if score == 0 {
for _, word := range tokenize(lower) {
if word == token {
score = 5
matches = []string{"syn:" + synonym}
breakdown = []string{"syn word +5: " + synonym}
break
}
}
}
}
return score, matches, breakdown
}
// Keep at most three independent symbol matches per token. This preserves
// supporting evidence without allowing large files to accumulate unbounded scores.
func scoreSymbolToken(symbols []string, token string) (int, []string, []string) {
type signal struct {
score int
matches, breakdown []string
}
candidates := make([]signal, 0, 3)
seen := make(map[string]bool)
for _, symbol := range symbols {
lower := strings.ToLower(symbol)
if seen[lower] {
continue
}
seen[lower] = true
score := 0
var candidateMatches, candidateBreakdown []string
if lower == token {
score = 24
candidateMatches = append(candidateMatches, "sym-exact:"+symbol)
candidateBreakdown = append(candidateBreakdown, "sym exact +24: "+symbol)
} else if strings.Contains(lower, token) {
score = 5
candidateMatches = append(candidateMatches, "sym:"+symbol)
candidateBreakdown = append(candidateBreakdown, "sym contains +5: "+symbol)
}
for _, part := range tokenizeIdentifier(symbol) {
if part == token {
score += 4
candidateMatches = append(candidateMatches, "sym-token:"+part)
candidateBreakdown = append(candidateBreakdown, "sym token +4: "+part)
break
}
}
if score > 0 {
candidates = append(candidates, signal{score, candidateMatches, candidateBreakdown})
}
}
sort.SliceStable(candidates, func(i, j int) bool { return candidates[i].score > candidates[j].score })
if len(candidates) > 3 {
candidates = candidates[:3]
}
score := 0
var matches, breakdown []string
for _, candidate := range candidates {
score += candidate.score
matches = append(matches, candidate.matches...)
breakdown = append(breakdown, candidate.breakdown...)
}
return score, matches, breakdown
}