44f7700c0c
* Backend: Enable Go module graph pruning and lazy module loading This commit applies the changes by running `go mod tidy -go=1.17` to enable module graph pruning and lazy module loading supported by Go 1.17 or higher. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com> * Backend: Move from io/ioutil to io and os package The io/ioutil package has been deprecated as of Go 1.16, see https://golang.org/doc/go1.16#ioutil. This commit replaces the existing io/ioutil functions with their new definitions in io and os packages. Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
97 lines
1.8 KiB
Go
97 lines
1.8 KiB
Go
//go:build ignore
|
|
// +build ignore
|
|
|
|
// This generates stopwords.go by running "go generate"
|
|
package main
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"path/filepath"
|
|
"text/template"
|
|
"unicode"
|
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
// LabelRule defines the rule for a given Label
|
|
type LabelRule struct {
|
|
Label string
|
|
See string
|
|
Threshold float32
|
|
Categories []string
|
|
Priority int
|
|
}
|
|
|
|
type LabelRules map[string]LabelRule
|
|
|
|
// This function generates the rules.go file containing rule extracted from rules.yml file
|
|
func main() {
|
|
rules := make(LabelRules)
|
|
|
|
fileName := "rules.yml"
|
|
|
|
if !fs.FileExists(fileName) {
|
|
log.Panicf("classify: found no label rules in %s", txt.Quote(filepath.Base(fileName)))
|
|
}
|
|
|
|
yamlConfig, err := os.ReadFile(fileName)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
err = yaml.Unmarshal(yamlConfig, rules)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
for label, rule := range rules {
|
|
for _, char := range label {
|
|
if unicode.IsUpper(char) {
|
|
log.Panicf("classify: %s must be lowercase", label)
|
|
}
|
|
}
|
|
|
|
if rule.See != "" {
|
|
rule, ok := rules[rule.See]
|
|
|
|
if !ok {
|
|
log.Panicf("missing label: %s", rule.See)
|
|
}
|
|
|
|
rules[label] = rule
|
|
}
|
|
}
|
|
|
|
f, err := os.Create("rules.go")
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
packageTemplate.Execute(f, struct {
|
|
Rules LabelRules
|
|
}{
|
|
Rules: rules,
|
|
})
|
|
}
|
|
|
|
var packageTemplate = template.Must(template.New("").Parse(`// Code generated by go generate; DO NOT EDIT.
|
|
package classify
|
|
|
|
var Rules = LabelRules{
|
|
{{- range $key, $value := .Rules }}
|
|
{{ printf "%q" $key }}: {
|
|
Label: {{ printf "%q" $value.Label }},
|
|
Threshold: {{ printf "%f" $value.Threshold }},
|
|
Priority: {{ $value.Priority }},
|
|
Categories: []string{ {{- range $value.Categories }} {{ printf "%q" . }}, {{- end }} },
|
|
},
|
|
{{- end }}
|
|
}`))
|