Skip to main content

pygrammar-fix

Fast, offline long-paragraph grammar and spelling correction that orchestrates harper-py, nlprule (optional), and pyspellchecker.

  • Fast and offline — no server, no API key, no JVM. Everything runs locally.
  • Returns fixed text — fix() gives you the corrected string directly.
  • Handles long paragraphs — sentence-aware chunking keeps each engine call reliable.
  • Three-stage pipeline — spelling pass → grammar pass → join, each independently tunable.
  • Honest about limits — see Known limitations below.

Install

pip install pygrammar-fix

For multilingual correction (German, Spanish), install the nlprule extra. nlprule has no macOS ARM64 wheel, so this works on Linux, Windows, and Intel Macs:

pip install "pygrammar-fix[nlprule]"

On Apple Silicon you can still use pygrammar-fix for English — spelling and grammar are handled by pyspellchecker and harper-py, both of which ship native ARM64 wheels. nlprule-dependent features raise a clear RuntimeError instead of failing silently.

Quick start

from pygrammar_fix import GrammarFixer

fixer = GrammarFixer(prefer="harper")
print(fixer.fix("I definately recieve to many seperate emails becuase my inbox is allways full."))
# → I definitely receive to many separate emails because my inbox is always full.

That example shows the pipeline working end-to-end:

  • definately, recieve, seperate, becuase, allways — fixed by the spelling pass
  • inbox — not mangled (distance guard rejects inbox → into)
  • to many — left alone (homophone; grammar-engine territory, see limitations)

Usage

Fix text

from pygrammar_fix import GrammarFixer

fixer = GrammarFixer()
fixed = fixer.fix("This is an test of the the linter.")
print(fixed)  # → This is a test of the linter.

Structured suggestions

for suggestion in fixer.suggest("She was not been here since Monday."):
    print(suggestion["start"], suggestion["end"], suggestion["message"], suggestion["replacements"])

Note: suggest() exposes the grammar engines only. Spelling corrections are applied by fix(). Use suggest() when you want to present the raw grammar diagnostics to a user and let them choose.

Engine selection

# Fastest path for English (harper-py)
fixer = GrammarFixer(prefer="harper")

# Default: nlprule where available, falls back to a clear error on macOS ARM64
fixer = GrammarFixer(prefer="nlprule")

# German or Spanish (requires the nlprule extra)
fixer = GrammarFixer(lang="de")

Disable spelling

# Per call
fixer.fix(text, spellcheck=False)

# Globally
fixer = GrammarFixer(spellcheck=False)

Tune chunk size

# Smaller chunks are more reliable on tricky text; larger chunks are faster.
fixer = GrammarFixer(max_chunk_chars=3000)

Protect domain-specific words

pyspellchecker will flag unusual proper nouns as misspellings. Register them:

from pygrammar_fix import add_to_whitelist

add_to_whitelist("Kubernetes", "Ankit", "Arora", "pygrammar")

fixer = GrammarFixer(prefer="harper")
fixer.fix("Ankit deployed to Kubernetes.")  # neither name is touched

How it works

The package is a thin orchestrator, not a merged binding. Three independent layers run in sequence:

                ┌─────────────────────┐
input text  ──▶ │  1. Spelling pass   │  pyspellchecker + Damerau-Levenshtein guard
                └─────────────────────┘
                            │
                            ▼
                ┌─────────────────────┐
                │  2. Chunker         │  sentence-aware split at ≤ max_chunk_chars
                └─────────────────────┘
                            │
                            ▼
                ┌─────────────────────┐
                │  3. Grammar engine  │  harper-py  (English, fast)
                └─────────────────────┘  or nlprule (English/DE/ES, correct())
                            │
                            ▼
                       corrected text
Layer File Purpose
Spelling src/pygrammar_fix/_spellcheck.py Fix misspellings conservatively; skip acronyms, camelCase, and distance-2 candidates
Chunker src/pygrammar_fix/_chunker.py Split long paragraphs into sentence-aware chunks
Adapters src/pygrammar_fix/_adapters.py Uniform suggest / correct interface over harper-py and nlprule
Orchestrator src/pygrammar_fix/_orchestrator.py Public GrammarFixer API; engine selection; lazy loading

Why three layers instead of two

harper-py is fast and catches both grammar and spelling. But it doesn't know every word: allways gets split into all ways (valid English, wrong meaning) rather than corrected to always. nlprule catches some grammar-adjacent spelling issues via rules but has no ARM64 wheel. A dedicated spellchecker that runs before the grammar engine solves both problems:

  • allways → always reliably
  • No dependency on nlprule for common typos
  • The distance guard prevents the spellchecker from making things worse

Why the distance guard matters

pyspellchecker's default dictionary (~82K words) doesn't include inbox. Left to its own devices, it corrects inbox → into (distance 2, both valid words). The distance guard in _spellcheck.py rejects any correction more than 1 edit away for short words, and more than 2 for long words — so inbox stays inbox, while recieve → receive and becuase → because still work.

Performance

Measured on an M-series MacBook Air, Python 3.12:

Workload Time
1 sentence (~60 chars) ~5 ms
8,500 chars (500 sentences) ~370 ms
First call (dictionary load) adds ~50–100 ms one-time

Grammar correction dominates the cost — Harper itself lints ~1,000 words in ~23 ms, but the orchestrator re-chunks and re-lints each chunk. For most use cases this is more than fast enough. If you need raw speed on a single sentence, use GrammarFixer(max_chunk_chars=10_000, spellcheck=False) to skip spellchecking and minimize chunking overhead.

Known limitations

  • Homophones are not corrected. to/too, its/it's, your/you're are valid words individually — no spellchecker can fix them without grammar context. nlprule catches many of these (Linux/Windows/Intel Mac); on macOS ARM64 they pass through unchanged. This is the honest boundary of the spellchecker layer.
  • Rare misspellings may be missed. pyspellchecker uses a frequency-based dictionary. Very rare words that aren't in it and have no close neighbors stay uncorrected.
  • Proper nouns are flagged as misspellings unless whitelisted. Use add_to_whitelist("YourName", "YourProduct").
  • nlprule has no macOS ARM64 wheel. GrammarFixer(lang="de") and GrammarFixer(prefer="nlprule") on Apple Silicon raise a clear RuntimeError. English works fine without it via prefer="harper".
  • suggest() does not include spelling corrections. That method exposes the grammar engines only. Spelling runs exclusively in fix().

Development

git clone https://github.com/ankitarora05/pygrammar-fix
cd pygrammar-fix

python -m venv .venv
source .venv/bin/activate

pip install -e ".[dev]"
pytest -v

On macOS ARM64 the nlprule-dependent tests are automatically skipped (the dev extra uses a PEP 508 environment marker). On Linux and Windows they run.

Project layout

pygrammar-fix/
├── src/pygrammar_fix/
│   ├── __init__.py
│   ├── _adapters.py         # harper-py and nlprule wrappers
│   ├── _chunker.py          # sentence-aware splitter
│   ├── _orchestrator.py     # GrammarFixer
│   ├── _spellcheck.py       # pyspellchecker + distance guard
│   └── py.typed             # PEP 561 marker
├── tests/
│   ├── test_adapters.py
│   ├── test_chunker.py
│   ├── test_orchestrator.py
│   └── test_spellcheck.py
├── pyproject.toml
└── README.md

Running the tests

pytest -v

Expected on macOS ARM64:

tests/test_chunker.py          ...              [PASS]
tests/test_orchestrator.py     .ss              [PASS + 2 skip]
tests/test_spellcheck.py       .........        [PASS]
tests/test_adapters.py         s                [SKIP — nlprule absent]

Building the wheel

python -m build
ls -lh dist/

Produces pygrammar_fix-1.0.0-py3-none-any.whl and pygrammar_fix-1.0.0.tar.gz. Both are pure-Python — they install on any platform that has wheels for the binary dependencies (harper-py, pyspellchecker, and optionally nlprule).

License

MIT — see LICENSE.

Metadata

Release files for pygrammar-fix 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pygrammar-fix 1.0.0
File Size Uploaded Provenance Actions
pygrammar_fix-1.0.0.tar.gz 15.5 kB With provenance Details for pygrammar_fix-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pygrammar-fix 1.0.0
File Interpreter ABI Platform Actions
pygrammar_fix-1.0.0-py3-none-any.whl Python 3 none any Details for pygrammar_fix-1.0.0-py3-none-any.whl

Total release size: 27.5 kB

Release files / pygrammar_fix-1.0.0.tar.gz

Download URL pygrammar_fix-1.0.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2bc3a0bf33c30da1a967e7a0bc67393fb9151be8c930d22606b517fd3d970265
BLAKE2b-256 checksum
How to use checksums
ce998172839a28519946362b2fcd1e0a10c51ae5ac5c467a43c2348d13a6c4ed
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Release files / pygrammar_fix-1.0.0-py3-none-any.whl

Download URL pygrammar_fix-1.0.0-py3-none-any.whl
Size 12.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
04d36e8a674c0041d3302075d17fd0ea0fdcfb83d6dc20fd1ae991b397d57126
BLAKE2b-256 checksum
How to use checksums
57e031536264bbf5230dbba340c4b199ec46bacf16b5b7bbb03417a07f4a8e02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

Security for pygrammar-fix 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pygrammar_fix-1.0.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 28, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page