/
capitalization.go
127 lines (109 loc) · 3.36 KB
/
capitalization.go
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package check
import (
"unicode/utf8"
"github.com/errata-ai/regexp2"
"github.com/jdkato/twine/strcase"
"golang.org/x/exp/maps"
"github.com/errata-ai/vale/v2/internal/core"
"github.com/errata-ai/vale/v2/internal/nlp"
)
// Capitalization checks the case of a string.
type Capitalization struct {
Definition `mapstructure:",squash"`
// `match` (`string`): $title, $sentence, $lower, $upper, or a pattern.
Match string
Check func(s string, re *regexp2.Regexp) bool
// `style` (`string`): AP or Chicago; only applies when match is set to
// $title.
Style string
// `exceptions` (`array`): An array of strings to be ignored.
Exceptions []string
// `indicators` (`array`): An array of suffixes that indicate the next
// token should be ignored.
Indicators []string
// `threshold` (`float`): The minimum proportion of words that must be
// (un)capitalized for a sentence to be considered correct.
Threshold float64
exceptRe *regexp2.Regexp
}
// NewCapitalization creates a new `capitalization`-based rule.
func NewCapitalization(cfg *core.Config, generic baseCheck, path string) (Capitalization, error) {
rule := Capitalization{}
err := decodeRule(generic, &rule)
if err != nil {
return rule, readStructureError(err, path)
}
err = checkScopes(rule.Scope, path)
if err != nil {
return rule, err
}
re, err := updateExceptions(rule.Exceptions, cfg.AcceptedTokens)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
}
rule.exceptRe = re
// NOTE: This is OK since setting `Threshold` to 0 would mean that the rule
// would never trigger.
if rule.Threshold == 0 {
rule.Threshold = 0.8
}
rule.Exceptions = append(rule.Exceptions, maps.Keys(cfg.AcceptedTokens)...)
if rule.Match == "$title" {
var tc *strcase.TitleConverter
if rule.Style == "Chicago" {
tc = strcase.NewTitleConverter(
strcase.ChicagoStyle,
strcase.UsingVocab(rule.Exceptions),
)
} else {
tc = strcase.NewTitleConverter(
strcase.APStyle,
strcase.UsingVocab(rule.Exceptions),
)
}
rule.Check = func(s string, re *regexp2.Regexp) bool {
return title(s, re, tc, rule.Threshold)
}
} else if rule.Match == "$sentence" {
sc := strcase.NewSentenceConverter(
strcase.UsingVocab(rule.Exceptions),
strcase.UsingIndicator(wasIndicator(rule.Indicators)),
)
rule.Check = func(s string, re *regexp2.Regexp) bool {
return sentence(s, re, sc, rule.Threshold)
}
} else if f, ok := varToFunc[rule.Match]; ok {
rule.Check = f
} else {
re2, errc := regexp2.CompileStd(rule.Match)
if errc != nil {
return rule, core.NewE201FromPosition(errc.Error(), path, 1)
}
rule.Check = func(s string, r *regexp2.Regexp) bool {
return re2.MatchStringStd(s) || isMatch(r, s)
}
}
return rule, nil
}
// Run checks the capitalization style of the provided text.
func (o Capitalization) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
alerts := []core.Alert{}
txt := blk.Text
if !o.Check(txt, o.exceptRe) {
pos := []int{0, utf8.RuneCountInString(txt)}
a, err := makeAlert(o.Definition, pos, txt)
if err != nil {
return alerts, err
}
alerts = append(alerts, a)
}
return alerts, nil
}
// Fields provides access to the internal rule definition.
func (o Capitalization) Fields() Definition {
return o.Definition
}
// Pattern is the internal regex pattern used by this rule.
func (o Capitalization) Pattern() string {
return ""
}