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
use crate::{
    CharStringExt, Lint, Token, TokenStringExt,
    expr::{Expr, FirstMatchOf, SequenceExpr},
    linting::{ExprLinter, LintKind, Suggestion, expr_linter::Sentence},
    patterns::WordSet,
};

pub struct WebScraping {
    expr: FirstMatchOf,
}

impl Default for WebScraping {
    fn default() -> Self {
        let scrap_verbs = &["scrap", "scrapped", "scraps", "scrapping"][..];
        let scrap_nouns = &["scrapper", "scrappers"][..];

        let mut closed_compounds = WordSet::new(&[]);
        let mut open_and_hyphenated_compounds = vec![];

        scrap_verbs.iter().chain(scrap_nouns).for_each(|scrap| {
            closed_compounds.add_chars(
                &['w', 'e', 'b']
                    .into_iter()
                    .chain(scrap.chars())
                    .collect::<Vec<char>>(),
            );
            open_and_hyphenated_compounds
                .push(Box::new(SequenceExpr::aco("web").t_ws_h().t_aco(scrap)) as Box<dyn Expr>);
        });

        let hyphenated_compounds = FirstMatchOf::new(open_and_hyphenated_compounds);

        let web_scraps = FirstMatchOf::new(vec![
            Box::new(closed_compounds),
            Box::new(hyphenated_compounds),
        ]);

        let scrapables = &[
            "article",
            "articles",
            "content",
            "doc",
            "docs",
            "document",
            "documents",
            "dom",
            "html",
            "info",
            "information",
            "page",
            "pages",
            "site",
            "sites",
            "text",
            "web",
            "webpage",
            "webpages",
            "website",
            "websites",
        ];

        // An explicitly non-greedy expression. Counterintuitively, greedy doesn't always match.
        let scrap_the_web = SequenceExpr::word_set(scrap_verbs)
            .t_ws()
            .then_zero_or_more(
                SequenceExpr::with(|t: &Token, s: &[char]| {
                    t.kind.is_word() && !t.get_ch(s).eq_any_ignore_ascii_case_str(scrapables)
                })
                .t_ws(),
            )
            .then_word_set(scrapables);

        Self {
            expr: FirstMatchOf::new(vec![Box::new(web_scraps), Box::new(scrap_the_web)]),
        }
    }
}

fn match_web_then_scrap(toks: &[Token], src: &[char]) -> Option<Lint> {
    if ![1, 3].contains(&toks.len()) {
        return None;
    }

    let (web, sep, scrap) = if toks.len() == 1 {
        let (w, s) = toks[0].get_ch(src).split_at(3);
        (w, &[] as &[char], s) // No separator in the 1-token case
    } else {
        (
            toks[0].get_ch(src),
            toks[1].get_ch(src),
            toks[2].get_ch(src),
        )
    };

    // Standardize the prefix (web + optional separator)
    let prefix = web.iter().chain(sep).copied();

    // Generalize the "scrap" -> "scrape" logic
    let replacement_value = match scrap.len() {
        5 | 6 => prefix
            .chain(scrap.iter().take(5).copied())
            .chain(std::iter::once('e'))
            .chain(scrap.iter().skip(5).copied())
            .collect(),
        _ => prefix
            .chain(scrap.iter().take(5).copied())
            .chain(scrap.iter().skip(6).copied())
            .collect(),
    };

    Some(Lint {
        span: toks.span()?,
        lint_kind: LintKind::Eggcorn,
        suggestions: vec![Suggestion::replace_with_match_case(
            replacement_value,
            toks.span()?.get_content(src),
        )],
        message:
            "`Scrap` means `discard`. The word for gathering information from websites is `scrape`."
                .to_string(),
        ..Default::default()
    })
}

fn match_scrap_then_web(toks: &[Token], src: &[char]) -> Option<Lint> {
    let scrap = toks[0].get_ch(src);

    // Generalize the "scrap" -> "scrape" logic
    let replacement_value = match scrap.len() {
        5 | 6 => scrap
            .iter()
            .take(5)
            .copied()
            .chain(std::iter::once('e'))
            .chain(scrap.iter().skip(5).copied())
            .collect(),
        _ => scrap
            .iter()
            .take(5)
            .copied()
            .chain(scrap.iter().skip(6).copied())
            .collect(),
    };

    Some(Lint {
        span: toks[0].span,
        lint_kind: LintKind::Eggcorn,
        suggestions: vec![Suggestion::replace_with_match_case(
            replacement_value,
            toks[0].span.get_content(src),
        )],
        message:
            "`Scrap` means `discard`. The word for gathering information from websites is `scrape`."
                .to_string(),
        ..Default::default()
    })
}

impl ExprLinter for WebScraping {
    type Unit = Sentence;

    fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
        match toks.first()?.get_ch(src).first()?.to_ascii_lowercase() {
            'w' => match_web_then_scrap(toks, src),
            's' => match_scrap_then_web(toks, src),
            _ => None,
        }
    }
    fn expr(&self) -> &dyn Expr {
        &self.expr
    }

    fn description(&self) -> &'static str {
        "Corrects `scrapping` the web to `scraping`."
    }
}

#[cfg(test)]
mod tests {
    use crate::linting::tests::assert_suggestion_result;

    use super::WebScraping;

    // Basic closed compound tests

    #[test]
    fn scrap() {
        assert_suggestion_result("webscrap", WebScraping::default(), "webscrape");
    }

    #[test]
    fn scraps() {
        assert_suggestion_result("webscraps", WebScraping::default(), "webscrapes");
    }

    #[test]
    fn scrapped() {
        assert_suggestion_result("webscrapped", WebScraping::default(), "webscraped");
    }

    #[test]
    fn scrapper() {
        assert_suggestion_result("webscrapper", WebScraping::default(), "webscraper");
    }

    #[test]
    fn scrappers() {
        assert_suggestion_result("webscrappers", WebScraping::default(), "webscrapers");
    }

    #[test]
    fn scrapping() {
        assert_suggestion_result("webscrapping", WebScraping::default(), "webscraping");
    }

    // Basic open compound tests

    #[test]
    fn scrap_open() {
        assert_suggestion_result("web scrap", WebScraping::default(), "web scrape");
    }

    #[test]
    fn scraps_open() {
        assert_suggestion_result("web scraps", WebScraping::default(), "web scrapes");
    }

    #[test]
    fn scrapped_open() {
        assert_suggestion_result("web scrapped", WebScraping::default(), "web scraped");
    }

    #[test]
    fn scrapper_open() {
        assert_suggestion_result("web scrapper", WebScraping::default(), "web scraper");
    }

    #[test]
    fn scrappers_open() {
        assert_suggestion_result("web scrappers", WebScraping::default(), "web scrapers");
    }

    #[test]
    fn scrapping_open() {
        assert_suggestion_result("web scrapping", WebScraping::default(), "web scraping");
    }

    // Basic hyphenated compound tests

    #[test]
    fn scrap_hyphenated() {
        assert_suggestion_result("web-scrap", WebScraping::default(), "web-scrape");
    }

    #[test]
    fn scraps_hyphenated() {
        assert_suggestion_result("web-scraps", WebScraping::default(), "web-scrapes");
    }

    #[test]
    fn scrapped_hyphenated() {
        assert_suggestion_result("web-scrapped", WebScraping::default(), "web-scraped");
    }

    #[test]
    fn scrapper_hyphenated() {
        assert_suggestion_result("web-scrapper", WebScraping::default(), "web-scraper");
    }

    #[test]
    fn scrappers_hyphenated() {
        assert_suggestion_result("web-scrappers", WebScraping::default(), "web-scrapers");
    }

    #[test]
    fn scrapping_hyphenated() {
        assert_suggestion_result("web-scrapping", WebScraping::default(), "web-scraping");
    }

    // Verb+object basic functionality tests

    #[test]
    fn scrap_page() {
        assert_suggestion_result("scrap page", WebScraping::default(), "scrape page");
    }

    #[test]
    fn scrapped_pages() {
        assert_suggestion_result("scrapped pages", WebScraping::default(), "scraped pages");
    }

    #[test]
    fn scraps_html() {
        assert_suggestion_result("scraps html", WebScraping::default(), "scrapes html");
    }

    #[test]
    fn scrapping_web() {
        assert_suggestion_result("scrapping web", WebScraping::default(), "scraping web");
    }

    #[test]
    fn scrapping_web_all_caps() {
        assert_suggestion_result("SCRAPPING WEB", WebScraping::default(), "SCRAPING WEB");
    }

    #[test]
    fn scrapping_web_mixed_case() {
        assert_suggestion_result("Scrapping Web", WebScraping::default(), "Scraping Web");
    }

    // Real-world examples harvested from GitHub

    #[test]
    fn web_scrap_lowercase() {
        assert_suggestion_result(
            "The goal of the project is to web scrap data from all pages of the website with capability of handling exceptions.",
            WebScraping::default(),
            "The goal of the project is to web scrape data from all pages of the website with capability of handling exceptions.",
        );
    }

    #[test]
    fn web_scrap_open_both_words_titlecase() {
        assert_suggestion_result(
            "Web Scrap on Jabama website to generate and analyze a dataset",
            WebScraping::default(),
            "Web Scrape on Jabama website to generate and analyze a dataset",
        );
    }

    #[test]
    fn web_scrapped_open_first_word_titlecase() {
        assert_suggestion_result(
            "Web scrapped an amazon page , automated the scraping, stored the data in csv file and created an email alert when the drop prices",
            WebScraping::default(),
            "Web scraped an amazon page , automated the scraping, stored the data in csv file and created an email alert when the drop prices",
        );
    }

    #[test]
    fn web_scrapped_open_both_words_titlecase() {
        assert_suggestion_result(
            "This project uses the data collected (Web Scrapped) from a website that list the houses for sale in Rwanda",
            WebScraping::default(),
            "This project uses the data collected (Web Scraped) from a website that list the houses for sale in Rwanda",
        );
    }

    #[test]
    fn web_scrapped_hyphenated_both_words_titlecase() {
        assert_suggestion_result(
            "Web-Scrapped Datasets",
            WebScraping::default(),
            "Web-Scraped Datasets",
        );
    }

    #[test]
    fn web_scrapper_open_both_words_titlecase() {
        assert_suggestion_result(
            "Web Scrapper Built Using Golang.",
            WebScraping::default(),
            "Web Scraper Built Using Golang.",
        );
    }

    #[test]
    fn web_scrappers_lowercase() {
        assert_suggestion_result(
            "Internet bots and web scrappers that will save a lot of your time!",
            WebScraping::default(),
            "Internet bots and web scrapers that will save a lot of your time!",
        );
    }

    #[test]
    fn web_scrappers_hyphenated() {
        assert_suggestion_result(
            "A Collection of web-scrappers with GUI written in Pyside6/PyQt6.",
            WebScraping::default(),
            "A Collection of web-scrapers with GUI written in Pyside6/PyQt6.",
        );
    }

    #[test]
    fn web_scrapping_lowercase() {
        assert_suggestion_result(
            "ScrapPaper is a web scrapping method to extract journal information from PubMed and Google Scholar using Python script.",
            WebScraping::default(),
            "ScrapPaper is a web scraping method to extract journal information from PubMed and Google Scholar using Python script.",
        );
    }

    #[test]
    fn web_scrapping_titlecase() {
        assert_suggestion_result(
            "Web Scrapping Examples using Beautiful Soup in Python.",
            WebScraping::default(),
            "Web Scraping Examples using Beautiful Soup in Python.",
        );
    }

    #[test]
    fn web_scrapping_hyphenated() {
        assert_suggestion_result(
            "some websites allow web-scrapping some don't.",
            WebScraping::default(),
            "some websites allow web-scraping some don't.",
        );
    }

    #[test]
    fn webscrapped() {
        assert_suggestion_result(
            "Example of webscrapped document : click here.",
            WebScraping::default(),
            "Example of webscraped document : click here.",
        );
    }

    #[test]
    fn webscrapper() {
        assert_suggestion_result(
            "A webscrapper to scrape all the words and their meanings from urban dictionary.",
            WebScraping::default(),
            "A webscraper to scrape all the words and their meanings from urban dictionary.",
        );
    }

    #[test]
    fn webscrappers_capitalized() {
        assert_suggestion_result(
            "A collection of Webscrappers I built using Scrapy while learning it hands on - SIdR4g/Scrapy_practice.",
            WebScraping::default(),
            "A collection of Webscrapers I built using Scrapy while learning it hands on - SIdR4g/Scrapy_practice.",
        );
    }

    #[test]
    fn webscrappers_camelcase() {
        assert_suggestion_result(
            "Awesome-WebScrappers. Collection of powerful and efficient web scrapers built using Python and BeautifulSoup.",
            WebScraping::default(),
            "Awesome-WebScrapers. Collection of powerful and efficient web scrapers built using Python and BeautifulSoup.",
        );
    }

    #[test]
    fn webscrapping() {
        assert_suggestion_result(
            "Webscrapping to identify and download latest pdf documents.",
            WebScraping::default(),
            "Webscraping to identify and download latest pdf documents.",
        );
    }

    #[test]
    fn webscraps_lowercase() {
        assert_suggestion_result(
            "gostapafor is a tool that webscraps and forwards html pages to other consumers",
            WebScraping::default(),
            "gostapafor is a tool that webscrapes and forwards html pages to other consumers",
        );
    }

    #[test]
    fn webscraps_camelcase() {
        assert_suggestion_result(
            "WebScraps the University of California, Santa Cruz's menu and texts it to the user for Breakfast, Lunch, Dinner, and Late Night.",
            WebScraping::default(),
            "WebScrapes the University of California, Santa Cruz's menu and texts it to the user for Breakfast, Lunch, Dinner, and Late Night.",
        );
    }

    #[test]
    fn scrap_html() {
        assert_suggestion_result(
            "Scrap a website's HTML, CSS and JS using this API.",
            WebScraping::default(),
            "Scrape a website's HTML, CSS and JS using this API.",
        );
    }

    #[test]
    fn scrapped_news_articles() {
        assert_suggestion_result(
            "Scrapped various news article including all the details using scrapy framework.",
            WebScraping::default(),
            "Scrapped various news article including all the details using scrapy framework.",
        );
    }

    #[test]
    fn scrapping_different_websites() {
        assert_suggestion_result(
            "This repository contains Python scripts based on Scrapping different websites.",
            WebScraping::default(),
            "This repository contains Python scripts based on Scraping different websites.",
        );
    }

    #[test]
    fn scrapping_web_content() {
        assert_suggestion_result(
            "Scrapping web content using PHP and Python.",
            WebScraping::default(),
            "Scraping web content using PHP and Python.",
        );
    }

    #[test]
    fn scraps_from_pages() {
        assert_suggestion_result(
            "It requests the website and scraps news from different pages.",
            WebScraping::default(),
            "It requests the website and scrapes news from different pages.",
        );
    }
}