2021-08-31 18:49:08 +02:00
|
|
|
//go:build ignore
|
2020-01-09 01:21:09 +01:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// This generates stopwords.go by running "go generate"
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
"unicode"
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
"github.com/photoprism/photoprism/pkg/fs"
|
2020-05-03 18:00:50 +02:00
|
|
|
"github.com/photoprism/photoprism/pkg/txt"
|
2020-01-09 01:21:09 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// LabelRule defines the rule for a given Label
|
2020-01-09 01:21:09 +01:00
|
|
|
type LabelRule struct {
|
|
|
|
Label string
|
|
|
|
See string
|
|
|
|
Threshold float32
|
|
|
|
Categories []string
|
|
|
|
Priority int
|
|
|
|
}
|
|
|
|
|
|
|
|
type LabelRules map[string]LabelRule
|
|
|
|
|
2020-02-21 01:14:45 +01:00
|
|
|
// This function generates the rules.go file containing rule extracted from rules.yml file
|
2020-01-09 01:21:09 +01:00
|
|
|
func main() {
|
|
|
|
rules := make(LabelRules)
|
|
|
|
|
|
|
|
fileName := "rules.yml"
|
|
|
|
|
2020-01-12 14:00:56 +01:00
|
|
|
if !fs.FileExists(fileName) {
|
2020-07-14 18:00:32 +02:00
|
|
|
log.Panicf("classify: label rules not found in %s", txt.Quote(filepath.Base(fileName)))
|
2020-01-09 01:21:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
yamlConfig, err := ioutil.ReadFile(fileName)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(yamlConfig, rules)
|
|
|
|
|
2020-04-20 14:17:24 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2020-01-09 01:21:09 +01:00
|
|
|
for label, rule := range rules {
|
|
|
|
for _, char := range label {
|
|
|
|
if unicode.IsUpper(char) {
|
2020-07-14 18:00:32 +02:00
|
|
|
log.Panicf("classify: %s must be lowercase", label)
|
2020-01-09 01:21:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-09-23 23:46:17 +02:00
|
|
|
var Rules = LabelRules{
|
2020-01-09 01:21:09 +01:00
|
|
|
{{- 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 }}
|
|
|
|
}`))
|