harper-core 2.0.0

The language checker for developers.
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
use crate::{
    CharStringExt, Lint, Token, TokenKind,
    expr::{Expr, FirstMatchOf, SequenceExpr},
    linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
    spell::Dictionary,
};

static NON_MODAL_AUX: &[&str] = &[
    "do", "don't", "does", "doesn't", "have", "has", "haven't", "hasn't", "dont", "doesnt",
    "havent", "hasnt",
];
static IRREGULAR: &[(&str, &str)] = &[("don't", "doesn't"), ("have", "has"), ("haven't", "hasn't")];
static SUBJUNCTIVE: &[&str] = &[
    // "if" and "that" can take the subjunctive mood: "if he go", "that he go" - as in the US constitution
    // "if" TODO: "if" is more complicated to support than "that"
    "that",
    // Verbs that take the subjunctive mood can omit the "that":
    "demanded",
    "demanding",
    "insisted",
    "insisting",
    "recommended",
    "recommending",
    "requested",
    "requesting",
    "suggested",
    "suggesting",
];
static DITRANSITIVE: &[&str] = &[
    "give", "gave", "given", "gives", "giving", "lose", "lost", "loses", "losing",
];

pub struct PronounVerbAgreement<D> {
    expr: FirstMatchOf,
    dict: D,
}

impl<D> PronounVerbAgreement<D>
where
    D: Dictionary,
{
    pub fn new(dict: D) -> Self {
        // TODO: allowing "you" leads to false positives:
        // "8 years to give you rewards", "all I can do is give you examples"
        let non_3p_sing_pres_pron_with_3p_sing_pres_verb = SequenceExpr::default()
            .then_kind_both_but_not(
                (
                    TokenKind::is_personal_pronoun,
                    TokenKind::is_subject_pronoun,
                ),
                TokenKind::is_third_person_singular_pronoun,
            )
            .t_ws()
            // NOTE: allowing verbs that are also nouns leads to false positives:
            // "Are they colors or colours?"
            // "8 years to give you rewards"
            // "all I can do is give you examples"
            .then_verb_third_person_singular_present_form();

        // NOTE: But excluding them causes many more false positives:
        // boxes, does, drops, flies, gets, goes, likes, site, wakes
        // .then_kind_where(|k| k.is_verb_third_person_singular_present_form() && !k.is_plural_noun());

        let third_person_sing_pres_pron = |t: &Token, _: &[char]| {
            t.kind.is_subject_pronoun()
                && !t.kind.is_object_pronoun()
                && t.kind.is_personal_pronoun()
                && t.kind.is_third_person_singular_pronoun()
                && !t.kind.is_plural_pronoun()
        };

        let verb_lemma = |t: &Token, src: &[char]| {
            t.kind.is_verb_lemma()
                && !t.kind.is_verb_third_person_singular_present_form()
                && !t.kind.is_verb_simple_past_form() // eg. not "put"
                && !t.kind.is_adverb() // eg. not "even"
                && !t.kind.is_conjunction() // "and"
                && (!t.kind.is_auxiliary_verb() // "I go"≠"he goes" but "I can"="he can"
                // We don't want modals because they don't inflect, but we want the other auxiliaries.
                || t.get_ch(src).eq_any_ignore_ascii_case_str(NON_MODAL_AUX))
        };

        Self {
            expr: FirstMatchOf::new(vec![
                // One Expr for the "I walks" type:
                Box::new(non_3p_sing_pres_pron_with_3p_sing_pres_verb),
                // Two Expr's for the "he walk" type:
                Box::new(
                    SequenceExpr::with(third_person_sing_pres_pron)
                        .t_ws()
                        .then(verb_lemma),
                ),
                Box::new(SequenceExpr::aco("it").t_ws().t_aco("don't")),
            ]),
            dict,
        }
    }

    fn third_person_singular_present_to_lemma(&self, form: &[char]) -> Vec<Vec<char>> {
        let mut words: Vec<Vec<char>> = Vec::new();

        // -s
        if form.ends_with_ignore_ascii_case_chars(&['s']) {
            words.push(form[0..form.len() - 1].to_vec());

            // -es
            if form.ends_with_ignore_ascii_case_chars(&['e', 's']) {
                words.push(form[0..form.len() - 2].to_vec());

                // -ies -> -y
                if form.ends_with_ignore_ascii_case_chars(&['i', 'e', 's']) {
                    words.push(
                        format!("{}y", &form[0..form.len() - 3].iter().collect::<String>())
                            .chars()
                            .collect(),
                    );
                }
            }
        }

        if let Some((lemma, _)) = IRREGULAR.iter().find(|(_, f)| form.eq_str(f)) {
            words.push(lemma.chars().collect::<Vec<char>>());
        }

        words
            .iter()
            .filter(|&w| {
                self.dict
                    .get_word_metadata(w)
                    .is_some_and(|md| md.is_verb_lemma())
            })
            .map(|w| w.to_vec())
            .collect()
    }

    fn lemma_to_third_person_singular_present(&self, input: &str) -> Vec<Vec<char>> {
        let mut words: Vec<Vec<char>> = Vec::new();

        words.push(format!("{input}s").chars().collect());
        words.push(format!("{input}es").chars().collect());

        if input.ends_with("y") {
            words.push(
                format!("{}ies", &input[0..input.len() - 1])
                    .chars()
                    .collect(),
            );
        }

        if let Some((_, form)) = IRREGULAR
            .iter()
            .find(|(lemma, _)| input.eq_ignore_ascii_case(lemma))
        {
            words.push(form.chars().collect());
        }

        words
            .iter()
            .filter(|&w| {
                self.dict
                    .get_word_metadata(w)
                    .is_some_and(|md| md.is_verb_third_person_singular_present_form())
            })
            .map(|w| w.to_vec())
            .collect()
    }
}

impl<D> ExprLinter for PronounVerbAgreement<D>
where
    D: Dictionary,
{
    type Unit = Chunk;

    fn expr(&self) -> &dyn Expr {
        &self.expr
    }

    fn match_to_lint_with_context(
        &self,
        toks: &[Token],
        src: &[char],
        ctx: Option<(&[Token], &[Token])>,
    ) -> Option<Lint> {
        let pron_tok = &toks[0];
        let is_3psg = pron_tok.kind.is_third_person_singular_pronoun();

        // Skip when the pronoun is part of a hyphenated compound (e.g. "co-founded").
        // The tokenizer splits "co-founded" into separate tokens, but "co" as a prefix
        // is not acting as a pronoun.
        if pron_tok.span.end < src.len() && src[pron_tok.span.end] == '-' {
            return None;
        }

        let verb_tok = toks.last()?;

        if let Some((before, _)) = ctx
            && let [.., prev_word_tok, ws_tok] = before
            && ws_tok.kind.is_whitespace()
        {
            let prev_word = prev_word_tok.get_ch(src);
            let is_exempt = if is_3psg {
                prev_word_tok.kind.is_auxiliary_verb()
                    || prev_word.eq_any_ignore_ascii_case_str(SUBJUNCTIVE)
            } else if pron_tok.kind.is_subject_pronoun() {
                // Clause structure: (... in you) is ... ≠ you is
                // Look for "true" prepositions, not ones that are more like adverbial particles
                prev_word_tok.kind.is_preposition() && !prev_word.eq_str("up")
                    // When the verb is ditransitive, the pronoun is object case, the verb position is actually a noun
                    || (prev_word.eq_any_ignore_ascii_case_str(DITRANSITIVE) && verb_tok.kind.is_noun())
            } else {
                false
            };

            if is_exempt {
                return None;
            }
        }

        let verb_span = verb_tok.span;
        let verb_chars = verb_tok.get_ch(src);
        let verb_str = verb_tok.get_str(src);

        let suggs = if is_3psg {
            self.lemma_to_third_person_singular_present(&verb_str)
        } else {
            self.third_person_singular_present_to_lemma(verb_chars)
        };

        let suggestions = suggs
            .into_iter()
            .map(|s| Suggestion::replace_with_match_case(s, verb_chars))
            .collect();

        Some(Lint {
            span: verb_span,
            lint_kind: LintKind::Agreement,
            suggestions,
            message: "The form of the verb must agree in grammatical number with the pronoun."
                .to_string(),
            ..Default::default()
        })
    }

    fn description(&self) -> &str {
        "Ensures pronouns agree with their verbs."
    }
}

#[cfg(test)]
mod lints {
    use super::PronounVerbAgreement;
    use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
    use crate::spell::FstDictionary;

    // Expected to be fixed, but there are exceptions

    #[test]
    fn issue_233_1() {
        assert_suggestion_result(
            "I likes this place.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "I like this place.",
        );
    }

    #[test]
    fn issue_233_2() {
        assert_suggestion_result(
            "I sits under the AC.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "I sit under the AC.",
        );
    }

    #[test]
    #[ignore = "because 'like' is an adjective as well as a verb."]
    fn issue_233_1_reverse() {
        assert_suggestion_result(
            "He like this place.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "He likes this place.",
        );
    }

    #[test]
    fn why_we_cant_flag_like_yet() {
        assert_no_lints(
            "What is he like?",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn issue_233_2_reverse() {
        assert_suggestion_result(
            "She sit under the AC.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "She sits under the AC.",
        );
    }

    #[test]
    fn dont_flag_correct_agreement() {
        assert_no_lints(
            "He likes this place. I sit under the AC.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    // Every pronoun systematically

    // Expected to get corrected

    #[test]
    fn fixes_i() {
        assert_suggestion_result(
            "I wakes up.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "I wake up.",
        );
    }

    #[test]
    fn fixes_we() {
        assert_suggestion_result(
            "We gets dressed.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "We get dressed.",
        );
    }

    #[test]
    fn fixes_you() {
        assert_suggestion_result(
            "You drops off the kids.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "You drop off the kids.",
        );
    }

    #[test]
    fn fixes_he() {
        assert_suggestion_result(
            "He work hard.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "He works hard.",
        );
    }

    #[test]
    fn fixes_she() {
        assert_suggestion_result(
            "She study hard.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "She studies hard.",
        );
    }

    #[test]
    #[ignore = "Becasue 'it' is also object case. Eg. 'watch it break down'"]
    fn we_cant_fix_it_yet() {
        assert_suggestion_result(
            "It break down.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "It breaks down.",
        );
    }

    #[test]
    fn why_we_cant_fix_it_yet() {
        assert_no_lints(
            "I heard it break down.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn fixes_they() {
        assert_suggestion_result(
            "They repairs it.",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "They repair it.",
        )
    }

    // Correct phrases that are expected not to get corrected

    #[test]
    fn dont_flag_i() {
        assert_no_lints("I eat", PronounVerbAgreement::new(FstDictionary::curated()));
    }

    #[test]
    fn dont_flag_we() {
        assert_no_lints(
            "We drink",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn dont_flag_you() {
        assert_no_lints(
            "You walk",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn dont_flag_he() {
        assert_no_lints(
            "He runs",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn dont_flag_she() {
        assert_no_lints(
            "She swims",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn dont_flag_it() {
        assert_no_lints(
            "It works!",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn dont_flag_they() {
        assert_no_lints(
            "They finish",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    // Ceck changing verb endings

    // -ies ↔ -y
    #[test]
    fn fix_flies() {
        assert_suggestion_result(
            "I flies",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "I fly",
        );
    }
    #[test]
    fn fix_cry() {
        assert_suggestion_result(
            "He cry",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "He cries",
        );
    }

    // -o ↔ -oes
    #[test]
    fn fix_go() {
        assert_suggestion_result(
            "She go",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "She goes",
        );
    }
    #[test]
    fn fix_goes() {
        assert_suggestion_result(
            "They goes",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "They go",
        );
    }

    // Check irregular changes

    // has ↔ have
    #[test]
    fn fix_has() {
        assert_suggestion_result(
            "You has",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "You have",
        );
    }
    #[test]
    fn fix_have() {
        assert_suggestion_result(
            "She have",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "She has",
        );
    }

    // hasn't ↔ haven't
    #[test]
    fn fix_hasnt() {
        assert_suggestion_result(
            "You hasn't",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "You haven't",
        );
    }
    #[test]
    fn fix_havent() {
        assert_suggestion_result(
            "He haven't",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "He hasn't",
        );
    }

    // -es
    #[test]
    fn fix_box() {
        assert_suggestion_result(
            "He box",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "He boxes",
        );
    }
    #[test]
    fn fix_boxes() {
        assert_suggestion_result(
            "You boxes",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "You box",
        );
    }

    // TODO: Are there any double consonant endings to change?
    // TODO: Are there any f ↔ v endings to change?

    // Negative contractions

    // doesn't ↔ don't
    #[test]
    fn fix_doesnt() {
        assert_suggestion_result(
            "We doesn't",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "We don't",
        );
    }
    #[test]
    // Note: This requires a dedicated branch of the `[Expr]`
    fn fix_dont() {
        assert_suggestion_result(
            "It don't",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "It doesn't",
        );
    }

    // Does do ↔ does behave differently to box ↔ boxes due to being an auxiliary verb?
    #[test]
    fn fix_do() {
        assert_suggestion_result(
            "He do",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "He does",
        );
    }
    #[test]
    fn fix_does() {
        assert_suggestion_result(
            "You does",
            PronounVerbAgreement::new(FstDictionary::curated()),
            "You do",
        );
    }

    // False positives found by Elijah

    #[test]
    fn false_positive_she_consider() {
        assert_no_lints(
            "On April 10th, I suggested she consider a smaller, more intimate gathering.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_she_sell() {
        assert_no_lints(
            "I suggested she sell it and use the proceeds to help with her relocation expenses, or perhaps rent a similar camera while in Barcelona.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_she_rent() {
        assert_no_lints(
            "I suggested she sell it and use the proceeds to help with her relocation expenses, or perhaps rent a similar camera while in Barcelona.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_he_donned() {
        assert_no_lints(
            "He donned his heavy oilskins and descended the winding staircase, his boots echoing in the hollow tower.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_he_cannot() {
        assert_no_lints(
            "Surely, he cannot offer the same sum as the developers.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_insisting_she_return() {
        assert_no_lints(
            "Am I the asshole for insisting she return the dress?",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_pride_in_you_is() {
        assert_no_lints(
            "It’s also important to recognize that your family's pride in you is a genuine reflection of your value.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_she_sought() {
        assert_no_lints(
            "She sought out Mrs. Hawthorne, the village’s oldest resident, a woman known for her vast knowledge of local history and her unsettlingly accurate intuition.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_lose_you_points() {
        assert_no_lints(
            "I admire your dedication to consistently drafting players who are actively trying to lose you points.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }

    #[test]
    fn false_positive_she_hung_up() {
        assert_no_lints(
            "When I reiterated the conditions I'd previously set, she hung up on me.",
            PronounVerbAgreement::new(FstDictionary::curated()),
        );
    }
}