From 50ef9bed8daf4bf9a3a63efca403531e9317ee0b Mon Sep 17 00:00:00 2001 From: AbstractionFactory <179820029+abstractionfactory@users.noreply.github.com> Date: Wed, 4 Sep 2024 10:09:17 +0200 Subject: [PATCH 1/2] Adding a confidence override threshold Signed-off-by: AbstractionFactory <179820029+abstractionfactory@users.noreply.github.com> --- backend/internal/license/license_detector.go | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/backend/internal/license/license_detector.go b/backend/internal/license/license_detector.go index 9b26bdef..fbe48729 100644 --- a/backend/internal/license/license_detector.go +++ b/backend/internal/license/license_detector.go @@ -119,6 +119,16 @@ func WithConfidenceThreshold(threshold float32) Opt { } } +func WithConfidenceOverrideThreshold(threshold float32) Opt { + return func(config *Config) error { + if threshold < 0 || threshold > 1 { + return fmt.Errorf("invalid threshold: %f", threshold) + } + config.ConfidenceOverrideThreshold = threshold + return nil + } +} + func WithCompatibleLicenses(licenses ...string) Opt { return func(config *Config) error { config.CompatibleLicenses = append(config.CompatibleLicenses, licenses...) @@ -129,12 +139,18 @@ func WithCompatibleLicenses(licenses ...string) Opt { type Config struct { CompatibleLicenses []string ConfidenceThreshold float32 + // ConfidenceOverrideThreshold is the limit at which a detected license overrides all other detected licenses. + // Defaults to 98. + ConfidenceOverrideThreshold float32 } func (c *Config) ApplyDefaults() error { if c.ConfidenceThreshold == 0.0 { c.ConfidenceThreshold = 0.9 } + if c.ConfidenceOverrideThreshold == 0 { + c.ConfidenceOverrideThreshold = 0.98 + } return nil } @@ -142,6 +158,9 @@ func (c *Config) Validate() error { if len(c.CompatibleLicenses) == 0 { return fmt.Errorf("no licenses configured") } + if c.ConfidenceOverrideThreshold < c.ConfidenceThreshold { + return fmt.Errorf("the confidence override threshold (%f) is lower than the confidence threshold (%f)", c.ConfidenceOverrideThreshold, c.ConfidenceThreshold) + } return nil } @@ -230,6 +249,11 @@ func (d detector) Detect(_ context.Context, repository fs.ReadDirFS, detectOptio if err := detectCfg.LinkFetcher(&l); err != nil { return nil, err } + if match.Confidence >= d.config.ConfidenceOverrideThreshold { + return []License{ + l, + }, nil + } result = append(result, l) } } From 58bfa3d7370388d63abad06349e77fad60c2b2b3 Mon Sep 17 00:00:00 2001 From: AbstractionFactory <179820029+abstractionfactory@users.noreply.github.com> Date: Wed, 4 Sep 2024 10:10:58 +0200 Subject: [PATCH 2/2] Comment fix Signed-off-by: AbstractionFactory <179820029+abstractionfactory@users.noreply.github.com> --- backend/internal/license/license_detector.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/internal/license/license_detector.go b/backend/internal/license/license_detector.go index fbe48729..de9117c7 100644 --- a/backend/internal/license/license_detector.go +++ b/backend/internal/license/license_detector.go @@ -140,7 +140,7 @@ type Config struct { CompatibleLicenses []string ConfidenceThreshold float32 // ConfidenceOverrideThreshold is the limit at which a detected license overrides all other detected licenses. - // Defaults to 98. + // Defaults to 98%. ConfidenceOverrideThreshold float32 }