Created
August 13, 2021 15:04
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So it should be possible to run this as
python3 sil-aspell-highlighter.py
anywhere where you might have runaspell
.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 bepersonal_ws-1.1 en 0
, put one ignored word per line, and then run this aspython3 sil-aspell-highlighter.py --personal=mydict.en.pws --home-dir=$HOME/some/folder/