-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
236 lines (195 loc) · 5.64 KB
/
parser.go
File metadata and controls
236 lines (195 loc) · 5.64 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package strings2
import (
"strings"
"unicode"
)
// Parse parses the input string into a slice of Words based on detection or provided options.
// It follows the pipeline: String -> SubParts -> Parts -> Words.
//
// opts can be:
// - ParserOption interface
// - Partitioner function
// - PartitionerConfig
// - ParserSmartAcronyms bool
func Parse(input string, opts ...any) ([]Word, error) {
// Level 5: Scan
subs, stats := StringToSubParts(input)
p := &ParserConfig{
SmartAcronyms: true,
NumberSplitting: false,
}
for _, opt := range opts {
switch o := opt.(type) {
case Partitioner:
p.Partitioner = o
case PartitionerConfig:
p.Partitioner = NewPartitioner(o)
case ParserOption:
o.Apply(p)
}
}
// Level 4: Partition
// If partitioner is not set, try to detect
partitioner := p.Partitioner
if partitioner == nil {
partitioner = DetectPartitioner(stats, p)
}
parts := SubPartsToParts(subs, partitioner)
// Level 3: Words
words := PartsToWords(parts, p)
return words, nil
}
// ParserConfig holds configuration for the parsing pipeline.
type ParserConfig struct {
Partitioner Partitioner
// SmartAcronyms controls whether all-uppercase words (longer than 1 char)
// should be treated as AcronymWord instead of UpperCaseWord.
// Defaults to true.
SmartAcronyms bool
// NumberSplitting controls whether to split on letter-digit boundaries.
NumberSplitting bool
}
// ParserOption configures the parser.
type ParserOption interface {
Apply(*ParserConfig)
}
type funcParserOption func(*ParserConfig)
func (f funcParserOption) Apply(p *ParserConfig) { f(p) }
// ParserSmartAcronyms is a typed option for SmartAcronyms configuration.
// It allows passing a boolean-like type directly to Parse.
type ParserSmartAcronyms bool
func (b ParserSmartAcronyms) Apply(p *ParserConfig) {
p.SmartAcronyms = bool(b)
}
// WithPartitioner sets a specific partitioner strategy.
func WithPartitioner(pt Partitioner) ParserOption {
return funcParserOption(func(p *ParserConfig) {
p.Partitioner = pt
})
}
// WithSmartAcronyms enables or disables smart acronym detection.
func WithSmartAcronyms(enabled bool) ParserOption {
return funcParserOption(func(p *ParserConfig) {
p.SmartAcronyms = enabled
})
}
// WithNumberSplitting enables or disables splitting on letter-digit boundaries.
func WithNumberSplitting(enabled bool) ParserOption {
return funcParserOption(func(p *ParserConfig) {
p.NumberSplitting = enabled
})
}
// DetectPartitioner uses stats to guess the best partitioner.
// config is optional, if provided it uses settings like NumberSplitting.
func DetectPartitioner(stats Stats, config ...*ParserConfig) Partitioner {
delimiters := make(map[rune]bool)
if stats.Spaces > 0 {
delimiters[' '] = true
}
// Add known delimiters if present
known := []rune{'_', '-', '+', '/', '\\', '|', ',', ';', ':'}
for _, r := range known {
if stats.SymbolCounts[r] > 0 {
delimiters[r] = true
}
}
// Check for dots.
// If we have spaces, dots might be punctuation (end of sentence).
// If no spaces, dots are likely delimiters (user.id).
if stats.SymbolCounts['.'] > 0 {
if stats.Spaces == 0 {
// likely delimiter
delimiters['.'] = true
}
}
splitNumber := false
if len(config) > 0 && config[0] != nil {
splitNumber = config[0].NumberSplitting
}
return NewPartitioner(PartitionerConfig{
Delimiters: delimiters,
SplitCamel: true,
SplitNumber: splitNumber,
})
}
// PartsToWords converts Parts to Words using classification logic.
func PartsToWords(parts []Part, config *ParserConfig) []Word {
var words []Word
for _, part := range parts {
words = append(words, ClassifyPart(part, config))
}
return words
}
// ClassifyPart converts a Part into a Word.
func ClassifyPart(part Part, config *ParserConfig) Word {
s := part.String()
// Separator handling: if we have SeparatorPart, we might return it as a SeparatorWord?
if _, ok := part.(*SeparatorPart); ok {
return SeparatorWord(s)
}
if s == "" {
return ExactCaseWord("")
}
// Check for dots -> Acronym
if strings.Contains(s, ".") {
return AcronymWord(s)
}
// Check casing
isAllUpper := true
isAllLower := true
isTitle := false
runes := []rune(s)
if len(runes) > 0 && unicode.IsUpper(runes[0]) {
isTitle = true
}
for i, r := range runes {
if !unicode.IsUpper(r) && unicode.IsLetter(r) {
isAllUpper = false
}
if !unicode.IsLower(r) && unicode.IsLetter(r) {
isAllLower = false
}
if i > 0 && unicode.IsUpper(r) {
isTitle = false
}
}
if isAllUpper {
// Use SmartAcronyms config or default
smartAcronyms := true
if config != nil {
smartAcronyms = config.SmartAcronyms
}
if smartAcronyms && len(runes) > 1 {
return AcronymWord(s)
}
return UpperCaseWord(s)
}
if isAllLower {
return SingleCaseWord(s)
}
if isTitle {
return FirstUpperCaseWord(s)
}
return ExactCaseWord(s)
}
// Level 1 / 2 Helpers
func ParseSnakeCase(input string, opts ...any) ([]Word, error) {
// Snake case implies split by underscore.
// Users might want to override this or add other options.
// But essentially we want to enforce the SnakeCasePartitioner.
// We can reuse Parse logic but force the partitioner?
// Default options for SnakeCase
combinedOpts := []any{SnakeCasePartitioner}
combinedOpts = append(combinedOpts, opts...)
return Parse(input, combinedOpts...)
}
func ParseCamelCase(input string, opts ...any) ([]Word, error) {
combinedOpts := []any{CamelCasePartitioner}
combinedOpts = append(combinedOpts, opts...)
return Parse(input, combinedOpts...)
}
func ParseKebabCase(input string, opts ...any) ([]Word, error) {
combinedOpts := []any{KebabCasePartitioner}
combinedOpts = append(combinedOpts, opts...)
return Parse(input, combinedOpts...)
}