-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathquery.go
More file actions
46 lines (37 loc) · 971 Bytes
/
query.go
File metadata and controls
46 lines (37 loc) · 971 Bytes
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
package agent
import (
"regexp"
"strings"
"unicode"
)
type QueryType int
const (
QueryComplex QueryType = iota
QueryShortFactoid
QueryMath
)
var (
simpleMathRegex = regexp.MustCompile(`^\s*\d+(\s*[\+\-\*\/]\s*\d+)+\s*$`)
shortFactRegex = regexp.MustCompile(`^[\p{L}\p{N}\s\?\!\.\,\-]{1,32}$`)
)
// classifyQuery analyzes input and returns a QueryType
func classifyQuery(input string) QueryType {
in := strings.TrimSpace(input)
if in == "" {
return QueryComplex
}
// 🧮 1. Math expressions → skip memory
if simpleMathRegex.MatchString(in) {
return QueryMath
}
// 💬 2. Short factoid — short length, simple text structure
if len([]rune(in)) <= 32 && shortFactRegex.MatchString(in) {
return QueryShortFactoid
}
// 💬 3. Single word heuristic also → short factoid
if len(strings.FieldsFunc(in, unicode.IsSpace)) == 1 {
return QueryShortFactoid
}
// 🧠 4. Default — complex query, keep full memory
return QueryComplex
}