diff options
author | Google Open Source <noreply+opensource@google.com> | 2019-02-11 13:50:25 -0800 |
---|---|---|
committer | Robert Spier <rspier@google.com> | 2019-05-01 14:19:49 -0700 |
commit | 57ecb7c44031f8d66cc9316e6e51e9303c65d9e5 (patch) | |
tree | e84e4a4d7a15f15477d4c90836b2465d7f01265a /classifier.go | |
parent | 5ad369b304e6e6273cc519f568fb0b29f5eb7bd6 (diff) | |
download | licenseclassifier-57ecb7c44031f8d66cc9316e6e51e9303c65d9e5.tar.gz |
Benchmark for diagnosing license classifier performance problems.
PiperOrigin-RevId: 233471726
Diffstat (limited to 'classifier.go')
-rw-r--r-- | classifier.go | 46 |
1 files changed, 14 insertions, 32 deletions
diff --git a/classifier.go b/classifier.go index 0af9c1c..8d39caf 100644 --- a/classifier.go +++ b/classifier.go @@ -75,49 +75,31 @@ type License struct { // Threshold is the lowest confidence percentage acceptable for the // classifier. Threshold float64 - - // archive is the path to the license archive - archive string -} - -// OptionFunc set options on a License struct. -type OptionFunc func(l *License) error - -// Archive is an OptionFunc to specify the location of the license archive file. -func Archive(f string) OptionFunc { - return func(l *License) error { - l.archive = f - return nil - } } // New creates a license classifier and pre-loads it with known open source licenses. -func New(threshold float64, options ...OptionFunc) (*License, error) { +func New(threshold float64) (*License, error) { classifier := &License{ c: stringclassifier.New(threshold, Normalizers...), Threshold: threshold, - archive: LicenseArchive, } - - for _, o := range options { - err := o(classifier) - if err != nil { - return nil, fmt.Errorf("error setting option %v: %v", o, err) - } - } - - if err := classifier.registerLicenses(); err != nil { - return nil, fmt.Errorf("cannot register licenses from %q: %v", classifier.archive, err) + if err := classifier.registerLicenses(LicenseArchive); err != nil { + return nil, fmt.Errorf("cannot register licenses: %v", err) } return classifier, nil } // NewWithForbiddenLicenses creates a license classifier and pre-loads it with // known open source licenses which are forbidden. -func NewWithForbiddenLicenses(threshold float64, options ...OptionFunc) (*License, error) { - opts := []OptionFunc{Archive(ForbiddenLicenseArchive)} - opts = append(opts, options...) - return New(threshold, opts...) +func NewWithForbiddenLicenses(threshold float64) (*License, error) { + classifier := &License{ + c: stringclassifier.New(threshold, Normalizers...), + Threshold: threshold, + } + if err := classifier.registerLicenses(ForbiddenLicenseArchive); err != nil { + return nil, fmt.Errorf("cannot register licenses: %v", err) + } + return classifier, nil } // WithinConfidenceThreshold returns true if the confidence value is above or @@ -196,8 +178,8 @@ type archivedValue struct { // registerLicenses loads all known licenses and adds them to c as known values // for comparison. The allocated space after ingesting the 'licenses.db' // archive is ~167M. -func (c *License) registerLicenses() error { - contents, err := ReadLicenseFile(c.archive) +func (c *License) registerLicenses(archive string) error { + contents, err := ReadLicenseFile(archive) if err != nil { return err } |