-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathoptions.go
More file actions
98 lines (87 loc) · 1.76 KB
/
options.go
File metadata and controls
98 lines (87 loc) · 1.76 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package gomtch
import (
"bytes"
"github.com/PuerkitoBio/goquery"
"github.com/jdkato/prose/tokenize"
"regexp"
"strings"
"unicode"
)
type Option func(*Document)
func WithHMTLParsing() Option {
return func(d *Document) {
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(strings.NewReader(d.Text))
if err != nil {
d.optError = err
return
}
d.Text = doc.Text()
}
}
func WithTransform(t Transformer) Option {
return func(d *Document) {
s, err := t.Transform(d.Text)
if err != nil {
d.optError = err
return
}
d.Text = s
}
}
func WithSequentialEqualCharsRemoval() Option {
var buf bytes.Buffer
var pc rune
return func(d *Document) {
for i, c := range d.Text {
if i == 0 {
pc = c
buf.WriteRune(c)
}
if pc == c {
if !unicode.IsNumber(pc) {
continue
}
}
pc = c
buf.WriteRune(c)
}
d.Text = buf.String()
}
}
func WithSetLower() Option {
return func(d *Document) {
d.Text = strings.ToLower(d.Text)
}
}
func WithSetUpper() Option {
return func(d *Document) {
d.Text = strings.ToUpper(d.Text)
}
}
func WithReplacer(pattern *regexp.Regexp, rep string) Option {
return func(d *Document) {
d.Text = pattern.ReplaceAllString(d.Text, rep)
}
}
func WithMinimumMatchScore(score int) Option {
return func(d *Document) {
d.matchScoreFunc = func(matchScore, wordLength int) bool {
return matchScore >= score*wordLength/100
}
}
}
func WithConditionalMatchScore(f func(int, int) bool) Option {
return func(d *Document) {
d.matchScoreFunc = f
}
}
func WithCustomRegexpTokenizer(t *tokenize.RegexpTokenizer) Option {
return func(d *Document) {
if t == nil {
d.Tokens = []string{regexp.MustCompile(`\s+`).ReplaceAllString(d.Text, "")}
return
}
d.Tokens = t.Tokenize(d.Text)
}
}