[net.sources] spellfix enhancements

oer@isosvax.UUCP (Ed Reeder) (10/12/85)

Enclosed is yet another set of enhancements to spellfix, a spelling
corrector previously distributed by Bill Silvert.   I corrected a
few minor bugs, made it also operate on XENIX systems, added an
ability to look up correct spellings in real time (using look),
and way to specify an editor other than vi.  I am enclosing Bill's
original commentary.  I have tested this on UCB 4.2 and Intel's
version of XENIX (a superior, and compatible, version of the XENIX
available on the PC/AT).

Ed Reeder
Intel Corporation
Phoenix, AZ

-----------------Bill's Commentary---------------------------------
Here is an enhanced version of spellfix with the following features:
menu driven rather than requiring editing of spell.errors in vi;
supports user dictionary $HOME/dict/words and menu allows user to
	add words to this dictionary;
option for automatic correction of words.

This was created by modifying Rex Saunders' script, and he gets most
of the credit for it.  My changes are inspired mainly by the CP/M
spelling checker "The Word Plus", which is a superb utility with
these and more features.

It has been suggested that spellfix check for complete words.
That is too messy for my taste -- my spelling isn't that bad!
Also, a recent item in net.sources.bugs points out that you can
get into trouble with troff commands.

Another problem which I haven't a fix for is case differences.
I have chosen to ignore all words with embedded numerals (this
includes 2nd) and anything which is all in capitals (since I
write a lot of stuff with embedded Fortran code).
---------------------------cut here-------------------------------
#! /bin/sh
# run through /bin/sh to create script and manual entry
cat > spellfix << xxSHELLxx
#! /bin/sh
# <@(#)spellfix	Ver. 1.7, 85/09/27 12:03:48> - interactive spelling checker and fixer
#   Rex Sanders, USGS Pacific Marine Geology
#   Modifications by Bill Silvert, MEL
#   Modifications by Ed Reeder, Intel
t1=/tmp/spf$$.1
t2=/tmp/spf$$.2
t3=/tmp/spf$$.3
prog=`basename $0`
udict=$HOME/dict
uwords=$udict/words
SPELLEDITOR=${SPELLEDITOR-'vi +/###/'}

case $# in
1)	trap 'rm -f $t1 $t2 $t3; exit' 0 1 2 15 ;;
*)	echo "Usage: $prog filename" >&2
	exit 1 ;;
esac

echo "Looking for spelling errors in $1 ..."
# ignore upper-case 'words' and alphnumerics
spell $1 | egrep "[a-z]" | egrep -v "[0-9]" | sort > $t2
if test -s $uwords
then	uwexists=y
	sort -u -f $uwords -o $uwords	     # clean up user's dictionary.
	sort $uwords | comm -23 $t2 - > $t1  # comm won't work with folded order
else	mv $t2 $t1
fi
test -s $t1 || exit 0

test -d $udict || mkdir $udict

echo "Found `wc -l <$t1` misspellings"
echo "Responses:	A=add to user dictionary, B=bypass, C=correct"
echo "		L=look for correct spelling, M=mark for correction"

for word in `cat $t1`
do
	egrep $word $1
	while :
	do
		echo -n "${word}: (A/B/C/L/M?) "
		read response
		case $response in
		A|a)	echo $word >> $uwords
			break ;;
		B|b)	break ;;
		C|c)	echo -n "Correct spelling: "
			read response
			case $response in
			"")	continue ;;
			esac
			echo "s/${word}/${response}/" >> $t3
			break ;;
		L|l)	echo -n "First letters of word: "
			read response
			case $response in
			"")	continue ;;
			esac
			case $uwexists in
			y)	look -f $response $uwords > $t2
				look $response | sort -f -m - $t2 ;;
			*)	look $response ;;
			esac | more ;;
		M|m)	echo "/${word}/i\\" >> $t3
			echo "### spell: ${word} %%%" >> $t3
			mark=y
			break ;;
		*)	;;
		esac
	done
done

test -s $t3 || exit 0

if (sed -f $t3 < $1 > $t2 2> /dev/null)
then	case "$mark" in
	y)	echo "Here is a temporary copy of $1 to edit: use n to find next error"
		echo "The errors are marked by the ### character string"
		sleep 3
		$SPELLEDITOR $t2
		sed -e '/^### spell: .* %%%$/d' $t2 > $t3 ;;
	*)	mv $t2 $t3 ;;
	esac
	while :
	do
		echo -n "Do you want to overwrite $1 <y/n>? "
		read reply
		case "$reply" in
		y*|Y*)	cp $t3 $1
			exit 0 ;;
		n*|N*)	exit 0 ;;
		*)	continue ;;
		esac
	done
else	echo "$prog: error marking misspelled words - file $1 unchanged." >&2
fi
xxSHELLxx
chmod +x spellfix
cat >spellfix.1 <<xxMANxx
.ta .5i 1i 1.5i 2i 2.5i 3i
.TH SPELLFIX 1 "Local Utility"
.SH NAME
spellfix - interactively fix spelling errors
.SH SYNOPSIS
.B spellfix
.I file
.SH DESCRIPTION
.PP
.I spellfix
is an interactive spelling checker and fixer.
It calls
.I spell, look
and also uses a local word list of the user.
Using
.I spellfix
is a 4 step process:
.TP
1.
Type:
.sp
.B
          spellfix
.I file
.sp
where
.I file
is the name of your manuscript.
.I spellfix
only handles one file at a time.
.TP
2.
.I spellfix
will list all apparent misspellings in your manuscript,
along with all lines in which each occurs.
Respond to each with:
.B A
if you want it added to your word list;
.B B
to bypass the word;
.B C
to correct it immediately;
.B L
to look up the correct spelling;
and
.B M
to mark it for later correction via a text editor.

If you respond with C,
.I spellfix
will prompt you for the correct spelling.
Make sure that you want this correction made in all lines shown!

If you respond with L,
.I spellfix
will ask you to enter the first letters of the word.  Enter as many
letters as you are sure of. All words in the system and user word
lists beginning with those letters will be shown.
.TP
3.
Optionally edit any
.I Marked
words in a temporary copy of your original file.
The words you marked in step 2 will be flagged with lines like:
.br
.sp
.ec +
        ###  spell:  foobar  %%%
.ec
.br
.sp
where
.I foobar
is a misspelled word.
You should correct the misspellings on the text lines that follow.
The flag lines will be removed automatically before the next step.
.TP
4.
Answer
.IR spellfix 's
question:
.sp
          overwrite
.I file
?
.sp
with
.B yes
or
.BR no .
If your answer is yes,
.I spellfix
will replace your original file with the file containing your corrections.
.SH NOTES
In step 3, the search string for
.IR vi (1)
is set to "###";
you can conveniently search for the next marked spelling error with the
"n" request.
.PP
If you have specified a different SPELLEDITOR, use its search techniques to
find the "###" string.
.PP
.I spellfix
was inspired by
.IR error (1).
.SH FILES
.in +1.5i
.ti -1.5i
~/dict/words		user word list
.ti -1.5i
/usr/dict/words		system word list
.ti -1.5i
$SPELLEDITOR	variable containing the pathname of the
.I Mark
editor, and any desired parameters.  Defaults to 'vi +/###/'.
.in -1.5i
.SH SEE ALSO
error(1), look(1), more(1), sed(1), spell(1), vi(1).
.SH BUGS
.PP
.I spellfix
can change more lines than needed.
For example, if
.I spell
rejects 'teache',
lines with 'teacher' will be marked as erroneous.
.SH AUTHOR
Rex Sanders, U.S. Geological Survey, Pacific Marine Geology.
Modifications by William Silvert, Marine Ecology Laboratory;
Ed Reeder, Intel Corporation.
xxMANxx
-- 
Ed Reeder
Intel Corporation
Phoenix, AZ
hplabs!hao!noao!terak!asuvax!isosvax!oer