forked from Automattic/harper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworth_to_do.rs
More file actions
317 lines (280 loc) · 9.89 KB
/
Copy pathworth_to_do.rs
File metadata and controls
317 lines (280 loc) · 9.89 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use crate::{
CharStringExt, Lint, Token, TokenStringExt,
char_ext::CharExt,
expr::{Expr, SequenceExpr},
linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
spell::Dictionary,
};
pub struct WorthToDo<D>
where
D: Dictionary,
{
expr: SequenceExpr,
dict: D,
}
impl<D> WorthToDo<D>
where
D: Dictionary,
{
pub fn new(dict: D) -> Self {
Self {
expr: SequenceExpr::word_seq(&["worth", "to"])
.t_ws()
.then_verb_lemma(),
dict,
}
}
}
impl<D> ExprLinter for WorthToDo<D>
where
D: Dictionary,
{
type Unit = Chunk;
fn expr(&self) -> &dyn Expr {
&self.expr
}
fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
let tolemtoks = &toks[toks.len() - 3..];
let lemtok = toks.last()?;
let tolemspan = tolemtoks.span()?;
let lemspan = lemtok.span;
let tolemchars = tolemspan.get_content(src);
let lemchars = lemspan.get_content(src);
let lemstr = lemspan.get_content_string(src);
let mut gerunds = Vec::new();
let glom_ing = format!("{}ing", lemstr);
if self.dict.contains_word_str(&glom_ing) {
gerunds.push(glom_ing);
}
if lemchars.ends_with_ignore_ascii_case_chars(&['e']) {
let replace_e_with_ing = format!("{}ing", &lemstr[..lemstr.len() - 1]);
if self.dict.contains_word_str(&replace_e_with_ing) {
gerunds.push(replace_e_with_ing);
}
}
if let Some(last_letter) = lemstr.chars().last()
&& !last_letter.is_vowel()
{
let double_consonant = format!("{}{}ing", lemstr, last_letter);
if self.dict.contains_word_str(&double_consonant) {
gerunds.push(double_consonant);
}
}
let suggestions = gerunds
.into_iter()
.map(|gerund| {
Suggestion::replace_with_match_case(
gerund.chars().collect::<Vec<char>>(),
tolemchars,
)
})
.collect();
Some(Lint {
span: tolemspan,
lint_kind: LintKind::Grammar,
suggestions,
message: "Use the `gerund` of the verb, the form that ends in `-ing`".to_owned(),
..Default::default()
})
}
fn description(&self) -> &str {
"Corrects `worth to` + a verb to `worth` + the gerund of the verb."
}
}
#[cfg(test)]
mod tests {
use super::WorthToDo;
use crate::{linting::tests::assert_suggestion_result, spell::FstDictionary};
#[test]
fn worth_to_add() {
assert_suggestion_result(
"Is it worth to add those files?",
WorthToDo::new(FstDictionary::curated()),
"Is it worth adding those files?",
);
}
#[test]
fn worth_to_adjust() {
assert_suggestion_result(
"If yes, it would be worth to adjust the description to make it easier to understand",
WorthToDo::new(FstDictionary::curated()),
"If yes, it would be worth adjusting the description to make it easier to understand",
);
}
#[test]
fn worth_to_ask() {
assert_suggestion_result(
"So it is worth to ask for this there or take a look at their wiki pages. ",
WorthToDo::new(FstDictionary::curated()),
"So it is worth asking for this there or take a look at their wiki pages. ",
);
}
#[test]
fn worth_to_buy() {
assert_suggestion_result(
"and it makes it really worth to buy the software",
WorthToDo::new(FstDictionary::curated()),
"and it makes it really worth buying the software",
);
}
#[test]
fn worth_to_deal() {
assert_suggestion_result(
"CC2531 is considered as crap in 2024 and its not worth to deal with it.",
WorthToDo::new(FstDictionary::curated()),
"CC2531 is considered as crap in 2024 and its not worth dealing with it.",
);
}
#[test]
fn worth_to_do() {
assert_suggestion_result(
"Is it worth to do the credit-card-balance-transfer?",
WorthToDo::new(FstDictionary::curated()),
"Is it worth doing the credit-card-balance-transfer?",
);
}
#[test]
fn worth_to_experiment() {
assert_suggestion_result(
"Hello @tkchia, thanks for the hint, I agree it is worth to experiment.",
WorthToDo::new(FstDictionary::curated()),
"Hello @tkchia, thanks for the hint, I agree it is worth experimenting.",
);
}
#[test]
fn worth_to_fix() {
assert_suggestion_result(
"i dont know if this is worth to fix, i just wanted to point this and start a discussion about this.",
WorthToDo::new(FstDictionary::curated()),
"i dont know if this is worth fixing, i just wanted to point this and start a discussion about this.",
);
}
#[test]
fn worth_to_get_published() {
assert_suggestion_result(
"think that would be worth to get published in my Thesis.",
WorthToDo::new(FstDictionary::curated()),
"think that would be worth getting published in my Thesis.",
);
}
#[test]
fn worth_to_imagine() {
assert_suggestion_result(
"Might be worth to imagine how the current Nu style would look like in other programming languages.",
WorthToDo::new(FstDictionary::curated()),
"Might be worth imagining how the current Nu style would look like in other programming languages.",
);
}
#[test]
fn worth_to_invest() {
assert_suggestion_result(
"It doesn't seem worth to invest much effort in this though...",
WorthToDo::new(FstDictionary::curated()),
"It doesn't seem worth investing much effort in this though...",
);
}
#[test]
fn worth_to_investigate() {
assert_suggestion_result(
"to get a feeling how CP-SAT works and what are directions worth to investigate i wanted to ask",
WorthToDo::new(FstDictionary::curated()),
"to get a feeling how CP-SAT works and what are directions worth investigating i wanted to ask",
);
}
#[test]
fn worth_to_play() {
assert_suggestion_result(
"Is worth to play with thread count if there are no issues?",
WorthToDo::new(FstDictionary::curated()),
"Is worth playing with thread count if there are no issues?",
);
}
#[test]
fn worth_to_put() {
assert_suggestion_result(
"Do you think it would be worth to put a suggestion to remove the kind network if cluster creation fails",
WorthToDo::new(FstDictionary::curated()),
"Do you think it would be worth putting a suggestion to remove the kind network if cluster creation fails",
);
}
#[test]
fn worth_to_read() {
assert_suggestion_result(
"Stored books worth to read.",
WorthToDo::new(FstDictionary::curated()),
"Stored books worth reading.",
);
}
#[test]
fn worth_to_revisit() {
assert_suggestion_result(
"we've had discussions before #260 and it maybe worth to revisit again",
WorthToDo::new(FstDictionary::curated()),
"we've had discussions before #260 and it maybe worth revisiting again",
);
}
#[test]
fn worth_to_rewrite() {
assert_suggestion_result(
"is puppet so bad that it is worth to rewrite everything?",
WorthToDo::new(FstDictionary::curated()),
"is puppet so bad that it is worth rewriting everything?",
);
}
#[test]
fn worth_to_try() {
assert_suggestion_result(
"is it really worth to try and what are facebook long-term plans about this engine.",
WorthToDo::new(FstDictionary::curated()),
"is it really worth trying and what are facebook long-term plans about this engine.",
);
}
#[test]
fn worth_to_update() {
assert_suggestion_result(
"Hi, maybe it's worth to update doc with the script for Bullseye given by @frenchfaso ?",
WorthToDo::new(FstDictionary::curated()),
"Hi, maybe it's worth updating doc with the script for Bullseye given by @frenchfaso ?",
);
}
#[test]
fn worth_to_upgrade() {
assert_suggestion_result(
"Your PR should've fixed that issue so I think it's worth to upgrade to 10.33 and see if that brings the delta down.",
WorthToDo::new(FstDictionary::curated()),
"Your PR should've fixed that issue so I think it's worth upgrading to 10.33 and see if that brings the delta down.",
);
}
#[test]
fn worth_to_use_and_develop() {
assert_suggestion_result(
"I think It worth to use and worth to develop further",
WorthToDo::new(FstDictionary::curated()),
"I think It worth using and worth developing further",
);
}
#[test]
fn works_with_uppercase_glom() {
assert_suggestion_result(
" YES IT IS WORTH TO DO",
WorthToDo::new(FstDictionary::curated()),
" YES IT IS WORTH DOING",
);
}
#[test]
fn works_with_uppercase_final_e() {
assert_suggestion_result(
"THIS LINTER WAS WORTH TO MAKE",
WorthToDo::new(FstDictionary::curated()),
"THIS LINTER WAS WORTH MAKING",
);
}
#[test]
fn works_with_uppercase_double_consonant() {
assert_suggestion_result(
"SO YEAH IT WAS WORTH TO GET THIS DONE",
WorthToDo::new(FstDictionary::curated()),
"SO YEAH IT WAS WORTH GETTING THIS DONE",
);
}
}