Skip to content

Instantly share code, notes, and snippets.

@stuartlangridge
Created August 13, 2021 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartlangridge/630ea24397bc7007a505336c8cd0ef6b to your computer and use it in GitHub Desktop.
Save stuartlangridge/630ea24397bc7007a505336c8cd0ef6b to your computer and use it in GitHub Desktop.
A small script for Ian Lloyd. Pass text to it on stdin and it outputs that same text with spelling errors highlighted. Change the highlight characters at the top if you need to.
#!/usr/bin/env python3
import sys, subprocess
HIGHLIGHT_BEFORE = "👉"
HIGHLIGHT_AFTER = "👈" # if you just want a highlight before, make this ""
cmd = ["aspell", "pipe"]
# add any extra options given to our command to the aspell command
if len(sys.argv) > 2:
cmd += sys.argv[1:]
for line in sys.stdin.readlines():
# feed the line to aspell; will throw obvious error on failure
proc = subprocess.run(cmd, input=line, text=True, capture_output=True)
corrections = []
for oline in proc.stdout.split("\n"):
if not oline.startswith("&"): continue
parts = oline.split()
from_start = int(parts[3][:-1]) # will be 28: so strip off colon
corrections.append((from_start, from_start + len(parts[1])))
if not corrections: continue
# Once we've got all the corrections, handle them in reverse order
# so that the indices for later ones aren't affected by earlier
new_line = line[:].strip()
for start, end in sorted(corrections, key=lambda n:n[0], reverse=True):
new_line = new_line[:end] + HIGHLIGHT_AFTER + new_line[end:]
new_line = new_line[:start] + HIGHLIGHT_BEFORE + new_line[start:]
print(new_line)
@stuartlangridge
Copy link
Author

stuartlangridge commented Aug 13, 2021

So it should be possible to run this as python3 sil-aspell-highlighter.py anywhere where you might have run aspell.

Pass additional args to this script to pass them to aspell. So if you need a list of words to ignore, create a file which is in $HOME/some/folder/mydict.en.pws, make the first line be personal_ws-1.1 en 0, put one ignored word per line, and then run this as

python3 sil-aspell-highlighter.py --personal=mydict.en.pws --home-dir=$HOME/some/folder/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment