Writing a Phrase Correction for Harper

This is part of a se­ries. Go to the start.

There are sev­eral ways to add a gram­mat­i­cal rule to Harper. This post aims to out­line the eas­i­est (and most con­strained): a phrase cor­rec­tion”.

Sometimes you’ll see a sim­ple but com­mon gram­mat­i­cal er­ror that does­n’t have much to do with any broader con­text. For ex­am­ple, I of­ten mistype in the” as int he”. It hap­pens quite of­ten, and there is­n’t a broad pat­tern at play here. This is a per­fect can­di­date for a phrase cor­rec­tion”.

From a tech­ni­cal point of view, a phrase cor­rec­tion” is just a map­ping from one or more phrases to an­other set of phrases. But they’re more than a sim­ple search-and-re­place. Un­der-the-hood, Harper will ac­count for var­i­ous cap­i­tal­iza­tion and styl­is­tic changes that can be dif­fi­cult to cover man­u­ally. For­tu­nately, they’re pretty easy to add to Harper.

Before we be­gin, make sure you prop­erly set up your en­vi­ron­ment.

Open up the Harper monorepo. In harper-core/src/linting/phrase_corrections/mod.rs, you’ll find a list of en­tries that look some­what like this:

"GildedAge" => (
    ["guilded age"],
    ["Gilded Age"],
    "The period of economic prosperity is called the `Gilded Age`.",
    "If referring to the period of economic prosperity, the correct term is `Gilded Age`."
),
"GoingTo" => (
    // The value to map _from_.
    ["gong to"], 
    // The suggestions to present to the user, which replace the problematic text.
    ["going to"], 
    // The message for the user.
    "Did you mean `going to`?",
    // The rule description to be shown in settings pages.
    "Corrects `gong to` to the intended phrase `going to`."
),
"GotRidOff" => (
    ["got rid off", "got ride of", "got ride off"],
    ["got rid of"],
    "Did you mean `got rid of`?",
    "Ensures `got rid of` is used instead of `got rid off`."
),

Each of these is a phrase cor­rec­tion”. To add one for the prob­lem I out­lined above, we just need to ap­pend to the end of the list and open a pull re­quest.

"InThe" => (
    ["int he"],
    ["in the"],
    "Did you mean `in the`?",
    "Detects and corrects a spacing error where `in the` is mistakenly written as `int he`. Proper spacing is essential for readability and grammatical correctness in common phrases."
),

If you want to go the ex­tra mile, we’d re­ally ap­pre­ci­ate if you added one or two test cases to harper-core/src/linting/phrase_corrections/tests.rs to make sure every­thing works as ex­pected:

#[test]
fn corrects_int_he() {
    assert_suggestion_result(
        "That pizza stayed int he box.",
        lint_group(),
        "That pizza stayed in the box.",
    );
}

Published July 9, 2025 at 6:00 AM

Proofread by Harper.

Comments