-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_summary.go
More file actions
152 lines (136 loc) · 3.7 KB
/
Copy pathsearch_summary.go
File metadata and controls
152 lines (136 loc) · 3.7 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package contexting
import (
"encoding/json"
"fmt"
"path/filepath"
"sort"
"strings"
)
type DirectorySummary struct {
Path string `json:"path"`
TotalScore int `json:"total_score"`
MatchCount int `json:"match_count"`
Rationale string `json:"rationale"`
TopMatches []SearchResult `json:"top_matches"`
}
func SummarizeDirectories(results []SearchResult, dirLimit int, drillLimit int) []DirectorySummary {
if dirLimit <= 0 {
dirLimit = 5
}
if drillLimit <= 0 {
drillLimit = 3
}
if len(results) == 0 {
return nil
}
type aggregate struct {
Path string
TotalScore int
MatchCount int
TopMatches []SearchResult
Tokens map[string]struct{}
}
byDir := make(map[string]*aggregate)
for _, result := range results {
dir := directoryForResultPath(result)
entry := byDir[dir]
if entry == nil {
entry = &aggregate{
Path: dir,
Tokens: make(map[string]struct{}),
}
byDir[dir] = entry
}
entry.TotalScore += result.Score
entry.MatchCount++
entry.TopMatches = append(entry.TopMatches, result)
for _, match := range result.Matches {
token := tokenFromMatch(match)
if token != "" {
entry.Tokens[token] = struct{}{}
}
}
}
summaries := make([]DirectorySummary, 0, len(byDir))
for _, entry := range byDir {
sort.Slice(entry.TopMatches, func(i, j int) bool {
if entry.TopMatches[i].Score != entry.TopMatches[j].Score {
return entry.TopMatches[i].Score > entry.TopMatches[j].Score
}
return entry.TopMatches[i].Path < entry.TopMatches[j].Path
})
if len(entry.TopMatches) > drillLimit {
entry.TopMatches = entry.TopMatches[:drillLimit]
}
tokens := make([]string, 0, len(entry.Tokens))
for token := range entry.Tokens {
tokens = append(tokens, token)
}
sort.Strings(tokens)
if len(tokens) > 3 {
tokens = tokens[:3]
}
rationale := fmt.Sprintf("matched %d result(s), total score %d", entry.MatchCount, entry.TotalScore)
if len(tokens) > 0 {
rationale += fmt.Sprintf(", top terms: %s", strings.Join(tokens, ", "))
}
summaries = append(summaries, DirectorySummary{
Path: entry.Path,
TotalScore: entry.TotalScore,
MatchCount: entry.MatchCount,
Rationale: rationale,
TopMatches: entry.TopMatches,
})
}
sort.Slice(summaries, func(i, j int) bool {
if summaries[i].TotalScore != summaries[j].TotalScore {
return summaries[i].TotalScore > summaries[j].TotalScore
}
if summaries[i].MatchCount != summaries[j].MatchCount {
return summaries[i].MatchCount > summaries[j].MatchCount
}
return summaries[i].Path < summaries[j].Path
})
if len(summaries) > dirLimit {
summaries = summaries[:dirLimit]
}
return summaries
}
func directoryForResultPath(result SearchResult) string {
path := filepath.ToSlash(result.Path)
if result.Type == "directory" {
return path
}
dir := filepath.ToSlash(filepath.Dir(path))
if dir == "." {
return "."
}
return dir
}
func tokenFromMatch(match string) string {
parts := strings.SplitN(match, ":", 2)
if len(parts) != 2 {
return ""
}
return strings.TrimSpace(parts[1])
}
func printDirectorySummaries(summaries []DirectorySummary) {
if len(summaries) == 0 {
fmt.Println("No directory summaries found")
return
}
for i, summary := range summaries {
fmt.Printf("%d. %s score=%d hits=%d\n", i+1, summary.Path, summary.TotalScore, summary.MatchCount)
fmt.Printf(" rationale=%s\n", summary.Rationale)
for _, hit := range summary.TopMatches {
fmt.Printf(" - %s (%s) score=%d\n", hit.Path, hit.Type, hit.Score)
}
}
}
func directorySummariesToJSON(summaries []DirectorySummary) (string, error) {
bytes, err := json.MarshalIndent(summaries, "", " ")
if err != nil {
return "", err
}
return string(bytes), nil
}