Can aspell
output line number and not offset in pipe mode for html and xml files? I can't read the file line by line because in this case aspell
can't identify closed tag (if tag situated on the next line).
This will output all occurrences of misspelt words with line numbers:
# Get aspell output...
<my_document.txt aspell pipe list -d en_GB --personal=./aspell.ignore.txt |
# Proccess the aspell output...
grep '[a-zA-Z]\+ [0-9]\+ [0-9]\+' -oh | \
grep '[a-zA-Z]\+' -o | \
while read word; do grep -on "\<$word\>" my_document.txt; done
Where:
- my_document.txt is your original document
- en_GB is your primary dictionary choice (e.g. try en_US)
- aspell.ignore.txt is an aspell personal dictionary (example below)
- aspell_output.txt is the output of aspell in pipe mode (ispell style)
- result.txt is a final results file
aspell.ignore.txt example:
personal_ws-1.1 en 500
foo
bar
example results.txt output (for an en_GB dictionary):
238:color
302:writeable
355:backends
433:dataonly
You can also print the whole line by changing the last grep -on
into grep -n
.
grep '[a-zA-Z]\+ [0-9]\+ [0-9]\+' aspell_output.txt -oh | grep '[a-zA-Z]\+' -o | sort | uniq | while read a; do grep -no "$a" my_document.txt; done > result.txt
–
Selfrighteous This is just an idea, I haven't really tried it yet (I'm on a windows machine :(). But maybe you could pipe the html file through head (with byte limit) and count newlines using grep to find your line number. It's neither efficient nor pretty, but it might just work.
cat icantspell.html | head -c <offset from aspell> | egrep -Uc "$"
I use the following script to perform spell-checking and to work-around the awkward output of aspell -a
/ ispell
. At the same time, the script also works around the problem that ordinals like 2nd
aren't recognized by aspell by simply ignoring everything that aspell reports which is not a word of its own.
#!/bin/bash
set +o pipefail
if [ -t 1 ] ; then
color="--color=always"
fi
! for file in "$@" ; do
<"$file" aspell pipe list -p ./dict --mode=html |
grep '[[:alpha:]]\+ [0-9]\+ [0-9]\+' -oh |
grep '[[:alpha:]]\+' -o |
while read word ; do
grep $color -n "\<$word\>" "$file"
done
done | grep .
You even get colored output if the stdout
of the script is a terminal, and you get an exit status of 1
in case the script found spelling mistakes, otherwise the exit status of the script is 0
.
Also, the script protects itself from pipefail
, which is a somewhat popular option to be set i.e. in a Makefile
but doesn't work for this script. Last but not least, this script explicitly uses [[:alpha:]]
instead of [a-zA-Z]
which is less confusing when it's also matching non-ASCII characters like German äöüÄÖÜß
and others. [a-zA-Z]
also does, but that to some level comes at a surprise.
aspell pipe
/ aspell -a
/ ispell
output one empty line for each input line (after reporting the errors of the line).
Demonstration printing the line number with awk:
$ aspell pipe < testFile.txt |
awk '/^$/ { countedLine=countedLine+1; print "#L=" countedLine; next; } //'
produces this output:
@(#) International Ispell Version 3.1.20 (but really Aspell 0.60.7-20110707)
& iinternational 7 0: international, Internationale, internationally, internationals, intentional, international's, Internationale's
#L=1
*
*
*
& reelly 22 11: Reilly, really, reel, rely, rally, relay, resell, retell, Riley, rel, regally, Riel, freely, real, rill, roll, reels, reply, Greeley, cruelly, reel's, Reilly's
#L=2
*
#L=3
*
*
& sometypo 18 8: some typo, some-typo, setup, sometime, someday, smote, meetup, smarty, stupor, Smetana, somatic, symmetry, mistype, smutty, smite, Sumter, smut, steppe
#L=4
with testFile.txt
iinternational
I say this reelly.
hello
here is sometypo.
(Still not as nice as hunspell -u
(https://mcmap.net/q/1317545/-is-it-possible-to-make-hunspell-print-the-line-numbers-of-the-misspelled-words). But hunspell misses some command line options I like.)
For others using aspell
with one of the filter modes (tex
, html
, etc), here's a way to only print line numbers for misspelled words in the filtered text. So for example, it won't print misspellings in the comments.
ASPELL_ARGS="--mode=html --personal=./.aspell.en.pws"
for file in "$@"; do
for word in $(aspell $ASPELL_ARGS list < "$file" | sort -u); do
grep -no "\<$word\>" <(aspell $ASPELL_ARGS filter < "$file")
done | sort -n
done
This works because aspell filter
does not delete empty lines. I realize this isn't using aspell pipe
as requested by OP, but it's in the same spirit of making aspell
print line numbers.
© 2022 - 2024 — McMap. All rights reserved.