# grope: a shell procedure designed to suggest spelling
#        corrections for a misspelled word
#
# Brian Downs  July 27, 1985
#
awk 'BEGIN{
        vow[1]="a";vow[2]="e";vow[3]="i";vow[4]="o";vow[5]="u"
	vowels="aeiouy"

        rep["i"]="a";
	rep["a"]="i";
	rep["o"]="a";
        rep["c"]="s";
	rep["s"]="c"; 
	rep["y"]="ie";
	rep["z"]="s";
	rep["e"]="i";
	rep["u"]="o"

	srep["a"]="e"; 
	srep["s"]="z"; 
	srep["e"]="a";
	srep["o"]="u";
	srep["i"]="e"; 

	trep["a"]="o"

	junk="*************************************************************"
        w="'$1'"
	print w;print substr(w,2)
        len=length(w)

        # try inserting vowels
        for(i in vow) for(j=1;j<len;j++){
          print substr(w,1,j) vow[i] substr(w,j+1)
        }

        # try exchanging every other letter
        for(j=1;j<len;j++){
           print substr(w,1,j-1) substr(w,j+1,1) substr(w,j,1) substr(w,j+2)
           # and try deleting each character except for first
           print substr(w,1,j) substr(w,j+2)
	   # and try replacing all single non-vowels with a double
	   if(index(vowels,c=substr(w,j+1,1)) || c==substr(w,j,1) || c==substr(w,j+2,1)) continue;
	   print substr(w,1,j+1) c substr(w,j+2)
        }

        # try making the replacements listed in 'rep' array
        for(i in rep){
	     tw=w
             while((j=index(tw,i))!=0){
                       print substr(w,1,j-1) rep[i] substr(w,j+length(i))
		       tw=substr(junk,1,j) substr(w,j+1)
             }
        }

        # try making the replacements listed in 'srep' array
        for(i in srep){
	     tw=w
             while((j=index(tw,i))!=0){
                       print substr(w,1,j-1) srep[i] substr(w,j+length(i))
		       tw=substr(junk,1,j) substr(w,j+1)
             }
        }

        # try making the replacements listed in 'trep' array
        for(i in trep){
	     tw=w
             while((j=index(tw,i))!=0){
                       print substr(w,1,j-1) trep[i] substr(w,j+length(i))
		       tw=substr(junk,1,j) substr(w,j+1)
             }
        }
	exit
   }' | sort -u | tee /tmp/grp$$ | spell | sort >/tmp/grp1$$ 
comm -13 /tmp/grp1$$ /tmp/grp$$
rm /tmp/grp$$ /tmp/grp1$$
