home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume04 / nspell < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  6.3 KB

  1. From decwrl!labrea!agate!pasteur!ames!mailrus!ncar!tank!nic.MR.NET!hal!ncoast!allbery Sat Oct 22 16:51:38 PDT 1988
  2. Article 671 of comp.sources.misc:
  3. Path: granite!decwrl!labrea!agate!pasteur!ames!mailrus!ncar!tank!nic.MR.NET!hal!ncoast!allbery
  4. From: argv@maui.UUCP (Dan Heller)
  5. Newsgroups: comp.sources.misc
  6. Subject: v04i129: yet another spelling checker
  7. Message-ID: <8810160134.AA08817@maui.island.uucp>
  8. Date: 22 Oct 88 01:20:28 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Reply-To: argv@maui.UUCP (Dan Heller)
  11. Lines: 212
  12. Approved: allbery@ncoast.UUCP
  13.  
  14. Posting-number: Volume 4, Issue 129
  15. Submitted-by: "Dan Heller" <argv@maui.UUCP>
  16. Archive-name: nspell
  17.  
  18. [Wants csh and "look" (a BSD-only program); also hardcoded paths galore.  ++bsa]
  19.  
  20. Here's yet another spelling checker given the wave of spelling
  21. checkers being posted.  However, this is more of an exercize in
  22. csh shell script writing.
  23.  
  24. The program uses many unix commands that should be available
  25. on all unix systems, but if they aren't or options aren't supported,
  26. let me know.  It assumes that programs are located in specific
  27. directories (like /usr/bin/spell and /usr/lib/spell)
  28.  
  29. There is a variable called BSD which can be set on or off,
  30. but I don't know what'll happen to sys-v boxes if it's off.
  31. Please send comments, bug fixes and other humorous material
  32. to island!argv@sun.com
  33. --------------------
  34. #!/bin/csh -f
  35. # This file is csh code to do an interactive spelling checker/correcter.
  36. # This shell script was written by Dan Heller one late night in 1985.
  37.  
  38. set BSD = 1        # set to 0 if you're NOT on a BSD system.
  39. onintr Quit
  40. set dict = "dict"
  41. set noglob
  42. set err_msg = "usage: $0 [-p privdict] file"
  43. @ argc = 1
  44. if (! $?PAGER) setenv PAGER more
  45.  
  46. if ( $#argv < 1 ) then
  47.     echo $err_msg
  48.     goto Quit
  49. endif
  50. if ( $argv[1] == '-p' ) then
  51.     @ argc = 3
  52.     set dict = $argv[2]
  53. endif
  54. if ( $argc > $#argv ) echo $err_msg
  55. while ( $argc <= $#argv )
  56.     set file = $argv[$argc]
  57.     @ argc++
  58.     /bin/cp /dev/null $$words
  59.     /bin/echo -n Looking for misspelled words in the file \"$file\" ...
  60.     /usr/bin/spell $file >! $$temp
  61.     set total = ( `wc -l $$temp` ) #get the size of the misspelled words file
  62.     echo Found $total[1] words.
  63.     if (-e $dict) then
  64.     /bin/echo -n Checking against words in \"$dict\"...
  65.     foreach w ( `cat $$temp` )
  66.         grep -s \\\<$w\\\> $dict    # for those who don't have -w with grep
  67.         if ($status == 1) echo $w >> $$words  # use if not in dict
  68.     end
  69.     /bin/rm $$temp
  70.     else
  71.     /bin/echo -n No private dictionary to check.
  72.     /bin/mv $$temp $$words
  73.     endif
  74.     if (-z $$words) then
  75.     echo no misspelled words
  76.     continue
  77.     endif
  78.     echo " "
  79.     set total = ( `wc -l $$words` ) #get the size of the misspelled words file
  80.     @ total = $total[1]            #set count to be an int variable
  81.     @ count = 1
  82.     /bin/echo Found $total misspelled words in \"$file\"
  83.     @ backup = 0
  84.     echo Type ? for help.
  85. mark:
  86.     while ($count <= $total)
  87.     set word = `sed -n ${count}p $$words`
  88.     if ($backup > 0) echo -n "> "
  89.     echo -n "$count) $word? "
  90.     if ( $BSD ) then
  91.         set newword = $<   # prompt for respelling of word.
  92.     else
  93.         set newword = `gets`
  94.     endif
  95.     if ( "$newword" == '' ) then
  96.         if ($backup == 0) then
  97.         echo $word >> $dict
  98.         else @ backup--
  99.         endif
  100.         @ count++
  101.         continue
  102.     endif
  103.     if ( "$newword" == '?' ) then
  104.         echo $err_msg
  105.         cat <<END
  106. "dictfile" is a list of your own words that you know
  107. are not misspelled or from a previous nspell session.
  108. Words found to be misspelled that you say are correct are 
  109. appended to the private dictionary.
  110.  
  111. > 3) thanx?
  112.  
  113. Possible responses:
  114.   RETURN    -- word is right, add it to the dictionary.
  115.   b         -- go back to the previous word.
  116.   l pattern -- look for pattern in dictionary.
  117.   s         -- search for 'thanx' in your file. (via grep)
  118.   d         -- don't save word, but it's correct.
  119.   D         -- Delete all occurances of this word.
  120.   ?         -- prints this list.
  121.   Q         -- Quit: don't correct words.
  122.   q         -- quit, correct words so far.
  123.   thanks    -- Attempted correct spelling.
  124.  
  125. If there is a > before the word, this means that you've
  126. already visited this word (you had backed up over it).
  127. RETURN will use the previous spelling if you had corrected it.
  128.  
  129. END
  130.         continue
  131.     endif
  132.     if ("$newword" == 'b') then
  133.         if ($count == 1) echo You\'re at the first word already.
  134.         if ($count > 1) then
  135.         @ count--
  136.         @ backup++
  137.         endif
  138.         continue
  139.     endif
  140.     if ("$newword" == 's') then
  141.         echo " "
  142.         echo in file \"$file\":
  143.         grep -n \\\<$word\\\> $file   # some systems don't have -w with grep
  144.         echo " "
  145.         continue
  146.     endif
  147.     set Newword = ( $newword )
  148.     if ($Newword[1] == 'l' || $Newword[1] == 'look') then
  149.         echo " "
  150.         /usr/bin/look $Newword[2] | $PAGER    # look bur*c doesn't work
  151.         echo " "
  152.         continue
  153.     endif
  154.     if ("$newword" == 'D') then
  155.         set newword = ""
  156.         goto force_feed
  157.     endif
  158.     if ("$newword" == 'd') then
  159.         @ count++
  160.         continue
  161.     endif
  162.     if ("$newword" == 'Q') then
  163.         echo " "
  164.         echo " Exiting: no changes made."
  165.         goto Quit 
  166.     endif
  167.     if ("$newword" == 'q') goto break
  168.     echo $newword | /usr/lib/spell /usr/dict/hlista $$misp > /dev/null
  169.     if ( -z $$misp ) then
  170.         if (-e $dict) then
  171.         grep -s "\\\<$newword\\\>" $dict
  172.         else goto cont
  173.         endif
  174.         if ($status == 0) then
  175. cont:
  176.         echo \"$newword\" seems to be misspelled also.
  177.         /bin/echo -n "Is it correct? [n] "
  178.         if ( $BSD ) then
  179.             set answer = ( $< )
  180.         else
  181.             set answer = ( `gets` )
  182.         endif
  183.         if ( $answer == 'y' ) then
  184.             echo $newword >> $dict
  185.         else 
  186.             continue
  187.         endif
  188.         endif
  189.     endif
  190. force_feed:
  191.     if ($backup > 0) then
  192.         (echo "g/\<$word\>/d";echo "w") | ex - $$newfile > /dev/null
  193.         if (-e dict) then
  194.         (echo "g/\<$word\>/d";echo "w") | ex - $dict >/dev/null
  195.         endif
  196.         @ backup--
  197.     endif
  198.     /bin/echo "%s/\<$word\>/$newword/g" >> $$newfile
  199.     @ count++
  200.     end
  201.     echo -n "Done. Hit return to continue, or 'b' to back..."
  202.     if ( $BSD ) then
  203.     set answer = ( $< )
  204.     else
  205.     set answer = ( `gets` )
  206.     endif
  207.     if ( $answer == 'b' ) then
  208.     @ count--
  209.     @ backup = 1
  210.     goto mark
  211.     endif
  212.     break:
  213.     if ( -z $$newfile || ! -e $$newfile ) then
  214.     echo No misspelled words
  215.     continue
  216.     endif
  217.     echo "w" >> $$newfile
  218.     echo -n Correcting misspelled words in file \"$file\"...
  219.     # cp $$newfile fixes  # for debugging
  220.     ex - $file < $$newfile
  221.     echo " done."
  222. end # main loop
  223. Quit:
  224. unset noglob
  225. /bin/rm -f $$*
  226.  
  227.  
  228.