[comp.sources.misc] v04i129: yet another spelling checker

argv@maui.UUCP (Dan Heller) (10/22/88)

Posting-number: Volume 4, Issue 129
Submitted-by: "Dan Heller" <argv@maui.UUCP>
Archive-name: nspell

[Wants csh and "look" (a BSD-only program); also hardcoded paths galore.  ++bsa]

Here's yet another spelling checker given the wave of spelling
checkers being posted.  However, this is more of an exercize in
csh shell script writing.

The program uses many unix commands that should be available
on all unix systems, but if they aren't or options aren't supported,
let me know.  It assumes that programs are located in specific
directories (like /usr/bin/spell and /usr/lib/spell)

There is a variable called BSD which can be set on or off,
but I don't know what'll happen to sys-v boxes if it's off.
Please send comments, bug fixes and other humorous material
to island!argv@sun.com
--------------------
#!/bin/csh -f
# This file is csh code to do an interactive spelling checker/correcter.
# This shell script was written by Dan Heller one late night in 1985.

set BSD = 1		# set to 0 if you're NOT on a BSD system.
onintr Quit
set dict = "dict"
set noglob
set err_msg = "usage: $0 [-p privdict] file"
@ argc = 1
if (! $?PAGER) setenv PAGER more

if ( $#argv < 1 ) then
    echo $err_msg
    goto Quit
endif
if ( $argv[1] == '-p' ) then
    @ argc = 3
    set dict = $argv[2]
endif
if ( $argc > $#argv ) echo $err_msg
while ( $argc <= $#argv )
    set file = $argv[$argc]
    @ argc++
    /bin/cp /dev/null $$words
    /bin/echo -n Looking for misspelled words in the file \"$file\" ...
    /usr/bin/spell $file >! $$temp
    set total = ( `wc -l $$temp` ) #get the size of the misspelled words file
    echo Found $total[1] words.
    if (-e $dict) then
	/bin/echo -n Checking against words in \"$dict\"...
	foreach w ( `cat $$temp` )
	    grep -s \\\<$w\\\> $dict    # for those who don't have -w with grep
	    if ($status == 1) echo $w >> $$words  # use if not in dict
	end
	/bin/rm $$temp
    else
	/bin/echo -n No private dictionary to check.
	/bin/mv $$temp $$words
    endif
    if (-z $$words) then
	echo no misspelled words
	continue
    endif
    echo " "
    set total = ( `wc -l $$words` ) #get the size of the misspelled words file
    @ total = $total[1]		    #set count to be an int variable
    @ count = 1
    /bin/echo Found $total misspelled words in \"$file\"
    @ backup = 0
    echo Type ? for help.
mark:
    while ($count <= $total)
	set word = `sed -n ${count}p $$words`
	if ($backup > 0) echo -n "> "
	echo -n "$count) $word? "
	if ( $BSD ) then
	    set newword = $<   # prompt for respelling of word.
	else
	    set newword = `gets`
	endif
	if ( "$newword" == '' ) then
	    if ($backup == 0) then
		echo $word >> $dict
	    else @ backup--
	    endif
	    @ count++
	    continue
	endif
	if ( "$newword" == '?' ) then
	    echo $err_msg
	    cat <<END
"dictfile" is a list of your own words that you know
are not misspelled or from a previous nspell session.
Words found to be misspelled that you say are correct are 
appended to the private dictionary.

> 3) thanx?

Possible responses:
  RETURN    -- word is right, add it to the dictionary.
  b         -- go back to the previous word.
  l pattern -- look for pattern in dictionary.
  s         -- search for 'thanx' in your file. (via grep)
  d         -- don't save word, but it's correct.
  D         -- Delete all occurances of this word.
  ?         -- prints this list.
  Q         -- Quit: don't correct words.
  q         -- quit, correct words so far.
  thanks    -- Attempted correct spelling.

If there is a > before the word, this means that you've
already visited this word (you had backed up over it).
RETURN will use the previous spelling if you had corrected it.

END
	    continue
	endif
	if ("$newword" == 'b') then
	    if ($count == 1) echo You\'re at the first word already.
	    if ($count > 1) then
		@ count--
		@ backup++
	    endif
	    continue
	endif
	if ("$newword" == 's') then
	    echo " "
	    echo in file \"$file\":
	    grep -n \\\<$word\\\> $file   # some systems don't have -w with grep
	    echo " "
	    continue
	endif
	set Newword = ( $newword )
	if ($Newword[1] == 'l' || $Newword[1] == 'look') then
	    echo " "
	    /usr/bin/look $Newword[2] | $PAGER    # look bur*c doesn't work
	    echo " "
	    continue
	endif
	if ("$newword" == 'D') then
	    set newword = ""
	    goto force_feed
	endif
	if ("$newword" == 'd') then
	    @ count++
	    continue
	endif
	if ("$newword" == 'Q') then
	    echo " "
	    echo " Exiting: no changes made."
	    goto Quit 
	endif
	if ("$newword" == 'q') goto break
	echo $newword | /usr/lib/spell /usr/dict/hlista $$misp > /dev/null
	if ( -z $$misp ) then
	    if (-e $dict) then
		grep -s "\\\<$newword\\\>" $dict
	    else goto cont
	    endif
	    if ($status == 0) then
cont:
		echo \"$newword\" seems to be misspelled also.
		/bin/echo -n "Is it correct? [n] "
		if ( $BSD ) then
		    set answer = ( $< )
		else
		    set answer = ( `gets` )
		endif
		if ( $answer == 'y' ) then
		    echo $newword >> $dict
		else 
		    continue
		endif
	    endif
	endif
force_feed:
	if ($backup > 0) then
	    (echo "g/\<$word\>/d";echo "w") | ex - $$newfile > /dev/null
	    if (-e dict) then
		(echo "g/\<$word\>/d";echo "w") | ex - $dict >/dev/null
	    endif
	    @ backup--
	endif
	/bin/echo "%s/\<$word\>/$newword/g" >> $$newfile
	@ count++
	end
    echo -n "Done. Hit return to continue, or 'b' to back..."
    if ( $BSD ) then
	set answer = ( $< )
    else
	set answer = ( `gets` )
    endif
    if ( $answer == 'b' ) then
	@ count--
	@ backup = 1
	goto mark
    endif
    break:
    if ( -z $$newfile || ! -e $$newfile ) then
	echo No misspelled words
	continue
    endif
    echo "w" >> $$newfile
    echo -n Correcting misspelled words in file \"$file\"...
    # cp $$newfile fixes  # for debugging
    ex - $file < $$newfile
    echo " done."
end # main loop
Quit:
unset noglob
/bin/rm -f $$*