Repository navigation
Expand file tree
/
Copy pathexample_test.go
More file actions
74 lines (68 loc) · 1.85 KB
/
Copy pathexample_test.go
File metadata and controls
74 lines (68 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package spam_test
import (
"fmt"
"log"
"github.com/git-pkgs/spam"
)
func ExampleScanFile() {
result, err := spam.ScanFile("package.json", []byte(`{"description":"Click here. Buy now. Download now."}`))
if err != nil {
log.Fatal(err)
}
for _, match := range result.Matches {
fmt.Println(match.Rule, match.Contribution, match.Evidence[0].Path)
}
// Output:
// PROMOTIONAL_PHRASES 1 package.json
}
func ExampleNewScanner() {
scanner, err := spam.NewScanner(spam.Options{
Rules: []spam.Rule{{
ID: "LOCAL_OFFER", Description: "Exclusive offer phrase", Category: "promotion",
Expression: `(?i)\bexclusive offer\b`, DefaultWeight: 1, Provenance: "local policy",
}},
Weights: map[string]int{"LOCAL_OFFER": 2},
})
if err != nil {
log.Fatal(err)
}
result, err := scanner.ScanFile("README.md", []byte("Exclusive offer. Exclusive offer."))
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Score, result.Matches[0].Evidence[0].Count)
// Output:
// 2 1
}
func ExampleScanner_Scan() {
scanner, err := spam.NewScanner(spam.Options{
Weights: map[string]int{"PROMOTIONAL_PHRASES": 2},
})
if err != nil {
log.Fatal(err)
}
result, err := scanner.Scan([]spam.Document{
{Path: "package.json", Data: []byte(`{"description":"An example package"}`)},
{Path: "README.md", Data: []byte("Click here. Buy now. Download now.")},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(result.Documents, result.Score)
// Output:
// 2 2
}
func ExampleScanner_Rules() {
scanner, err := spam.NewScanner(spam.Options{
DisabledRules: []string{"KEYWORD_REPETITION", "REPEATED_URLS", "LINK_HEAVY", "REPEATED_LINES"},
Weights: map[string]int{"PROMOTIONAL_PHRASES": 2},
})
if err != nil {
log.Fatal(err)
}
for _, rule := range scanner.Rules() {
fmt.Println(rule.ID, rule.Category, rule.DefaultWeight)
}
// Output:
// PROMOTIONAL_PHRASES promotion 1
}