Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can aspell output line number and not offset in pipe mode?

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).

like image 216
MaXal Avatar asked Apr 06 '11 14:04

MaXal


3 Answers

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.

like image 55
HoboBen Avatar answered Nov 06 '22 14:11

HoboBen


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 "$"
like image 1
André Laszlo Avatar answered Nov 06 '22 14:11

André Laszlo


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.

like image 1
Christian Hujer Avatar answered Nov 06 '22 14:11

Christian Hujer