Documentation
¶
Overview ¶
Package snippetclassifier decides whether a SCANOSS snippet match is a false positive: a match whose reported OSS line range does not correspond to the reported local line range (a wrong candidate assignment).
It compares the two ranges line by line and token by token — no semantic analysis — and scores the result with a small logistic regression trained on human-reviewed matches. It runs locally, needs no network and no state, and is safe for concurrent use.
What it judges is correspondence only. The component the match is attributed to, and whether the code is generated, boilerplate or a table, do not matter: if the reported lines correspond, the match is real.
Index ¶
Constants ¶
const DefaultThreshold = 0.7
DefaultThreshold is the P(false positive) at or above which Classify reports a false positive. On the reviewed corpus it gives FP precision 0.95 and recall 0.89 (leave-one-project-out).
Variables ¶
var ErrNoSpans = errors.New("snippetclassifier: match has no spans")
ErrNoSpans is returned for a match without any line range.
Functions ¶
func ComputeFeatures ¶
ComputeFeatures returns every feature the classifier knows, keyed by the names the training code uses. local and oss are the lines of the two files (see SplitLines).
func SplitLines ¶
SplitLines decodes data as UTF-8, replacing invalid bytes with U+FFFD, and splits it the way Python's str.splitlines() does: on \n, \r, \r\n, \v, \f, \x1c, \x1d, \x1e, \x85, U+2028 and U+2029, without a trailing empty line. Line numbers in a Span index into this slice (1-based).
Types ¶
type Classifier ¶
type Classifier struct {
// contains filtered or unexported fields
}
Classifier scores snippet matches. The zero value is not usable; call New.
func New ¶
func New(opts ...Option) (*Classifier, error)
New returns a Classifier using the embedded model and DefaultThreshold unless options say otherwise.
func (*Classifier) Classify ¶
func (c *Classifier) Classify(m Match) (Result, error)
Classify scores one match.
func (*Classifier) ClassifyLines ¶
func (c *Classifier) ClassifyLines(local, oss []string, spans []Span) Result
ClassifyLines is Classify for files already split with SplitLines.
func (*Classifier) ModelVersion ¶
func (c *Classifier) ModelVersion() string
ModelVersion identifies the model in use.
func (*Classifier) Threshold ¶
func (c *Classifier) Threshold() float64
Threshold is the configured decision threshold.
type Contribution ¶
type Contribution struct {
Feature string `json:"feature"`
Value float64 `json:"value"`
Logit float64 `json:"logit"`
}
Contribution is how much one feature pushed the score, in logit units: positive towards "false positive", negative towards "real".
type Match ¶
Match is one snippet match: the full contents of the local file and of the matched OSS file, and the ranges the scanner paired between them.
type Model ¶
type Model struct {
Version string `json:"version"`
Features []string `json:"features"`
Stats map[string][2]float64 `json:"stats"` // feature -> [mean, standard deviation]
Coef []float64 `json:"coef"`
Intercept float64 `json:"intercept"`
// TrainedOn and the evaluations are informational; nothing reads them.
TrainedOn json.RawMessage `json:"trained_on,omitempty"`
EvalHuman json.RawMessage `json:"eval_human,omitempty"`
}
Model is a standardised logistic regression over named features, in the JSON shape the training code writes. Its positive class is "false positive": Probability returns P(false positive).
func ParseModel ¶
ParseModel decodes and validates a model.
func (*Model) Probability ¶
func (m *Model) Probability(features map[string]float64) (float64, []Contribution)
Probability returns P(false positive) and the per-feature contributions, largest first.
type Option ¶
type Option func(*Classifier) error
Option configures a Classifier.
func WithModel ¶
WithModel replaces the embedded model with one in the JSON shape the training code writes (see Model) — for example a per-organisation model.
func WithThreshold ¶
WithThreshold sets the P(false positive) at or above which the verdict is VerdictFalsePositive.
type Result ¶
type Result struct {
// Probability is P(false positive), in [0, 1].
Probability float64 `json:"probability"`
// Verdict is Probability compared with the classifier's threshold.
Verdict Verdict `json:"verdict"`
// Contributions explain Probability, largest effect first.
Contributions []Contribution `json:"contributions"`
// Features holds every computed feature, including ones the model does not use.
Features map[string]float64 `json:"features"`
// ModelVersion identifies the model that produced the score.
ModelVersion string `json:"model_version"`
}
Result is the classifier's answer for one match.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
snippet-classify
command
Command snippet-classify scores one SCANOSS snippet match from the command line and prints the result as JSON.
|
Command snippet-classify scores one SCANOSS snippet match from the command line and prints the result as JSON. |