-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
147 lines (122 loc) · 3.07 KB
/
list.go
File metadata and controls
147 lines (122 loc) · 3.07 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
package llm
import (
"regexp"
"strings"
"unicode"
)
// ListStyle represents different types of list markers
type ListStyle uint8
const (
invalidListStyle ListStyle = iota // Invalid or no list style
numbersListStyle // e.g., "1." or "1:"
dashListStyle // e.g., "- "
starListStyle // e.g., "* "
plusListStyle // e.g., "+ "
)
var (
// Precompiled regex patterns
numberPattern = regexp.MustCompile(`^\s*\d+\s*[:.]\s*`)
dashPattern = regexp.MustCompile(`^\s*-\s+`)
starPattern = regexp.MustCompile(`^\s*\*\s+`)
plusPattern = regexp.MustCompile(`^\s*\+\s+`)
)
// ListFromResponse processes a string message and returns a list of suggestions
func ListFromResponse(resp string) []string {
lines := strings.Split(resp, "\n")
listKind := detectListStyleKind(lines)
seen := make(map[string]bool)
var result []string
withinList := false
notWithinListCount := 0
for _, line := range lines {
if len(strings.TrimSpace(line)) == 0 {
continue
}
content := isListItem(line, listKind)
if content != "" {
// Check for Chinese characters
hasHan := false
for _, r := range content {
if unicode.Is(unicode.Han, r) {
hasHan = true
break
}
}
if hasHan {
continue
}
withinList = true
notWithinListCount = 0
// Handle duplicate detection
seenKey := strings.ToLower(content)
if seen[seenKey] {
continue
}
seen[seenKey] = true
result = append(result, content)
} else if withinList {
notWithinListCount++
if notWithinListCount > 2 {
break
}
}
}
return result
}
// detectListStyleKind determines the predominant list style in the text
func detectListStyleKind(lines []string) ListStyle {
styleCounter := make(map[ListStyle]int)
for _, line := range lines {
kind := lineListKind(line)
if kind != invalidListStyle {
styleCounter[kind]++
}
}
selectedKind := numbersListStyle
selectedCount := 0
for kind, count := range styleCounter {
if count > selectedCount {
selectedKind = kind
selectedCount = count
}
}
return selectedKind
}
// lineListKind detects the type of list style for a single line
func lineListKind(line string) ListStyle {
if numberPattern.MatchString(line) {
return numbersListStyle
}
if dashPattern.MatchString(line) {
return dashListStyle
}
if starPattern.MatchString(line) {
return starListStyle
}
if plusPattern.MatchString(line) {
return plusListStyle
}
return invalidListStyle
}
// isListItem checks if the line is a list item and returns its content
func isListItem(line string, kind ListStyle) string {
switch kind {
case numbersListStyle:
if numberPattern.MatchString(line) {
return numberPattern.ReplaceAllString(line, "")
}
case dashListStyle:
if dashPattern.MatchString(line) {
return dashPattern.ReplaceAllString(line, "")
}
case starListStyle:
if starPattern.MatchString(line) {
return starPattern.ReplaceAllString(line, "")
}
case plusListStyle:
if plusPattern.MatchString(line) {
return plusPattern.ReplaceAllString(line, "")
}
}
return ""
}