-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrag.go
More file actions
143 lines (118 loc) · 3.13 KB
/
rag.go
File metadata and controls
143 lines (118 loc) · 3.13 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
// Package rag provides a tag filtering language for tagged text
package rag
import (
"strings"
"github.com/thehungry-dev/rag/internal/set"
)
const TagSeparator = ","
const TagRequiredToken = "+"
const TagExcludedToken = "-"
const AllTagsKeyword = "_all"
const UntaggedKeyword = "_untagged"
// Filter for which Select always returns true
var FilterNothing *TagFilter
type TagFilter struct {
requiredTags set.Set
oneOfTags set.Set
excludedTags set.Set
untagged bool
}
// Converts rag language into a filtering object
func Parse(filterText string) *TagFilter {
text := strings.TrimSpace(filterText)
if text == "" || text == AllTagsKeyword {
return FilterNothing
}
tags := strings.Split(text, TagSeparator)
maxTags := len(tags)
requiredTags := set.BuildSetOfSize(maxTags)
oneOfTags := set.BuildSetOfSize(maxTags)
excludedTags := set.BuildSetOfSize(maxTags)
filter := &TagFilter{requiredTags, oneOfTags, excludedTags, false}
for _, tag := range tags {
tag = strings.TrimSpace(tag)
if tag == "" {
continue
}
switch {
case tag == UntaggedKeyword:
filter.untagged = true
case strings.HasPrefix(tag, TagRequiredToken):
trimmedTag := strings.TrimPrefix(tag, TagRequiredToken)
requiredTags.Add(trimmedTag)
case strings.HasPrefix(tag, TagExcludedToken):
trimmedTag := strings.TrimPrefix(tag, TagExcludedToken)
excludedTags.Add(trimmedTag)
default:
oneOfTags.Add(tag)
}
}
return filter
}
func (tagFilter *TagFilter) IsRequiredTag(tag string) bool {
if tagFilter == nil {
return false
}
return tagFilter.requiredTags.Include(tag)
}
func (tagFilter *TagFilter) IsExcludedTag(tag string) bool {
if tagFilter == nil {
return false
}
return tagFilter.excludedTags.Include(tag)
}
func (tagFilter *TagFilter) IsRequiredOneOfTag(tag string) bool {
if tagFilter == nil {
return false
}
return tagFilter.oneOfTags.Include(tag)
}
func (tagFilter *TagFilter) String() string {
if tagFilter == nil || tagFilter == FilterNothing {
return "_all"
}
tags := make([]string, 0)
if tagFilter.untagged {
tags = append(tags, UntaggedKeyword)
}
for tag := range tagFilter.oneOfTags {
tags = append(tags, tag)
}
for tag := range tagFilter.requiredTags {
tags = append(tags, "+"+tag)
}
for tag := range tagFilter.excludedTags {
tags = append(tags, "-"+tag)
}
return strings.Join(tags, TagSeparator)
}
func (tagFilter *TagFilter) IsUntaggedOnly() bool {
return tagFilter.oneOfTags.IsEmpty() &&
tagFilter.excludedTags.IsEmpty() &&
tagFilter.requiredTags.IsEmpty() &&
tagFilter.untagged
}
func (tagFilter *TagFilter) Select(inputTags []string) bool {
if tagFilter == nil || tagFilter == FilterNothing {
return true
}
if len(inputTags) == 0 {
return tagFilter.untagged
}
if !tagFilter.requiredTags.SubsetOf(inputTags) {
return false
}
if !tagFilter.excludedTags.IsEmpty() && tagFilter.excludedTags.IncludeAny(inputTags) {
return false
}
if !tagFilter.oneOfTags.IncludeAny(inputTags) {
return false
}
if tagFilter.IsUntaggedOnly() && len(inputTags) > 0 {
return false
}
return true
}
func (tagFilter *TagFilter) Reject(inputTags []string) bool {
return !tagFilter.Select(inputTags)
}