-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.go
207 lines (183 loc) · 5.76 KB
/
parse.go
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package promptparser
import (
"fmt"
"strconv"
"strings"
)
func (p *PromptParser) parseExtraNetworks(text string) (PromptNode, error) {
content := text[1 : len(text)-1]
parts := strings.SplitN(content, ":", 2)
if len(parts) != 2 {
return PromptNode{}, fmt.Errorf("invalid extra networks format: %q", text)
}
return PromptNode{
Type: "extra_networks",
Value: parts[0],
Args: []PromptNode{{Type: "plain", Value: parts[1]}},
}, nil
}
func (p *PromptParser) parsePositive(text string) (PromptNode, error) {
content := text[1 : len(text)-1]
depth := 1
for strings.HasPrefix(content, "(") && strings.HasSuffix(content, ")") {
content = content[1 : len(content)-1]
depth++
}
return PromptNode{
Type: "positive",
Value: depth,
Args: []PromptNode{{Type: "plain", Value: content}},
}, nil
}
func (p *PromptParser) parseNegative(text string) (PromptNode, error) {
depth := 0
content := text
for strings.HasPrefix(content, "[") && strings.HasSuffix(content, "]") {
content = content[1 : len(content)-1]
depth++
}
return PromptNode{
Type: "negative",
Value: depth,
Args: []PromptNode{{Type: "plain", Value: strings.TrimSpace(content)}},
}, nil
}
func (p *PromptParser) parseWeighted(text string) (PromptNode, error) {
// Remove surrounding parentheses if present
text = strings.TrimPrefix(text, "(")
text = strings.TrimSuffix(text, ")")
parts := strings.SplitN(text, ":", 2)
if len(parts) != 2 {
return PromptNode{}, fmt.Errorf("invalid weighted format: %s", text)
}
weight, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return PromptNode{}, fmt.Errorf("invalid weight: %s", parts[1])
}
return PromptNode{
Type: "weighted",
Value: weight,
Args: []PromptNode{{Type: "plain", Value: parts[0]}},
}, nil
}
func (p *PromptParser) Parse(text string) ([]PromptNode, error) {
parts := strings.Split(text, ",")
result := make([]PromptNode, 0, len(parts))
for _, part := range parts {
node, err := p.parseNodeNonRecursive(strings.TrimSpace(part))
if err != nil {
return nil, fmt.Errorf("error parsing node %q: %w", part, err)
}
result = append(result, node)
}
return result, nil
}
func (p *PromptParser) parseNodeNonRecursive(text string) (PromptNode, error) {
text = strings.TrimSpace(text)
switch {
case strings.HasPrefix(text, "<") && strings.HasSuffix(text, ">"):
return p.parseExtraNetworks(text)
case strings.HasPrefix(text, "(") && strings.HasSuffix(text, ")"):
if strings.Contains(text, ":") {
return p.parseWeighted(text)
}
return p.parsePositive(text)
case strings.HasPrefix(text, "[") && strings.HasSuffix(text, "]"):
return p.parseSquareBrackets(text)
default:
return PromptNode{Type: "plain", Value: text}, nil
}
}
func (p *PromptParser) parseSquareBrackets(text string) (PromptNode, error) {
switch {
case strings.Contains(text, "|"):
return p.parseAlternate(text)
case strings.Count(text, ":") == 2:
return p.parseScheduledFull(text)
case strings.HasPrefix(text[1:], ":"):
return p.parseScheduledTo(text)
case strings.Contains(text, "::"):
return p.parseScheduledFrom(text)
case strings.Contains(text, ":"):
return p.parseScheduledTo(text)
default:
return p.parseNegative(text)
}
}
func (p *PromptParser) parseAlternate(text string) (PromptNode, error) {
content := text[1 : len(text)-1]
parts := strings.Split(content, "|")
args := make([]PromptNode, len(parts))
for i, part := range parts {
args[i] = PromptNode{Type: "plain", Value: strings.TrimSpace(part)}
}
return PromptNode{
Type: "alternate",
Args: args,
}, nil
}
// This function is no longer needed and can be removed
func (p *PromptParser) parseScheduledFull(text string) (PromptNode, error) {
content := text[1 : len(text)-1]
parts := strings.SplitN(content, ":", 3)
if len(parts) != 3 {
return PromptNode{}, fmt.Errorf("invalid scheduled full format: %s", text)
}
from := PromptNode{Type: "plain", Value: strings.TrimSpace(parts[0])}
to := PromptNode{Type: "plain", Value: strings.TrimSpace(parts[1])}
number, err := strconv.ParseFloat(parts[2], 64)
if err != nil {
return PromptNode{}, fmt.Errorf("invalid number in scheduled full: %s", parts[2])
}
return PromptNode{
Type: "scheduled_full",
Value: number,
Args: []PromptNode{from, to},
}, nil
}
func (p *PromptParser) parseScheduledFrom(text string) (PromptNode, error) {
content := text[1 : len(text)-1]
parts := strings.SplitN(content, "::", 2)
if len(parts) != 2 {
return PromptNode{}, fmt.Errorf("invalid scheduled from format: %s", text)
}
from := PromptNode{Type: "plain", Value: strings.TrimSpace(parts[0])}
number, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return PromptNode{}, fmt.Errorf("invalid number in scheduled from: %s", parts[1])
}
return PromptNode{
Type: "scheduled_from",
Value: number,
Args: []PromptNode{from},
}, nil
}
func (p *PromptParser) parseScheduledTo(text string) (PromptNode, error) {
content := text[1 : len(text)-1]
parts := strings.SplitN(content, ":", 3)
if len(parts) == 2 {
to := PromptNode{Type: "plain", Value: strings.TrimSpace(parts[0])}
number, err := strconv.ParseFloat(parts[1], 64)
if err != nil {
return PromptNode{}, fmt.Errorf("invalid number in scheduled to: %s", parts[1])
}
return PromptNode{
Type: "scheduled_to",
Value: number,
Args: []PromptNode{to},
}, nil
} else if len(parts) == 3 && parts[0] == "" {
to := PromptNode{Type: "plain", Value: strings.TrimSpace(parts[1])}
number, err := strconv.ParseFloat(parts[2], 64)
if err != nil {
return PromptNode{}, fmt.Errorf("invalid number in scheduled to: %s", parts[2])
}
return PromptNode{
Type: "scheduled_to",
Value: number,
Args: []PromptNode{to},
}, nil
}
return PromptNode{}, fmt.Errorf("invalid scheduled to format: %s", text)
}
// This function is no longer needed and can be removed