#!D:/Python/python.exe

# Date: Fri, 27 Oct 2000 12:44:55 EDT
# From: GregHolmes@aol.com
# To: htdig@htdig.org
# Subject: [htdig] spell check - python wrapper script

# In case anyone might find this useful, I have attached a python wrapper
# script that uses ispell to suggest alternatives to search words that may be
# typos.  The alternatives are presented as links that trigger the same search,
# with the suggested word replacing the typo.

# It's win32 speciifc, but should be easy to adapt to *nix.  I imagine it would
# be easy to reimplement in Perl as well.

import os
import sys
import string
import win32pipe

def main():
	#if main returns 0 the user will get an error message
	
	theQuery=os.environ['QUERY_STRING']
	
	#Build an exec string to pass to the OS
	execString="c:\opt\www\cgi-bin\htsearch.exe "
	execString=execString + '"'
	execString=execString + theQuery
	execString=execString + '" '
	
	#Do the search; put the result page into a temporary file

	resultsFile=win32pipe.popen(execString, 'r')
	if (resultsFile):
		resultsOut=resultsFile.read()
		resultsTestLine=string.split(resultsOut, "\n")
		resultsFile.close()
		if (string.find(resultsTestLine[2], "<title>No match for")!=-1):
			mispell1=string.replace(resultsTestLine[2], "<html><head><title>No match for '", "")
			mispell2=string.replace(mispell1, "'</title></head>", "")
			ispellString="echo " + mispell2 + " | c:\dos\ispell -a -d //c/dos/english | c:\dos\sed '/^&./!d'"
			spellFile=win32pipe.popen(ispellString, 'r')
			if (spellFile):
				spellOut=spellFile.readlines()
				spellFile.close()
				replaceString="Check the spelling of the search word(s) you used.</p><table border=1 bgcolor='yellow'><tr><td>Search Word<br>&nbsp;</td><td>Suggested Spelling(s)<br><b>Click on a suggestion to search for that word</b></td></tr>"
				if (len(spellOut)):
					for Line in spellOut:
						replaceString=replaceString + "<tr><td>"
						fragment=string.split(Line, ":")
						origWordList=string.split(fragment[0])
						origWord=origWordList[1]
						replaceString=replaceString + origWord + "</td><td>"
						suggestList=string.split(fragment[1], ", ")
						newQuery=string.lower(theQuery)
						for suggestWord in suggestList:
							suggestWord1=string.strip(suggestWord)
							newQuery1=string.replace(newQuery, origWord, suggestWord1)
							replaceString=replaceString + "<a href='/cgi-bin/searchme.py?" + newQuery1 + "'>" + suggestWord1 + "</a>&nbsp;"

						replaceString=replaceString +"</td></tr>"
					replaceString=replaceString+ "</table><br>"
					resultsOut2=string.replace(resultsOut, "Check the spelling of the search word(s) you used.", replaceString)
				else:
					resultsOut2=resultsOut
				print resultsOut2
				return 1


		resultsOut2=string.replace(resultsOut, "<i><b>Description</b></i><br><br><b><i>Excerpt</i>", "<b><i>Excerpt</i>")
		resultsOut3=string.replace(resultsOut2, "<b><i>Excerpt</i>", "<br><b><i>Excerpt</i>")
		resultsOut4=string.replace(resultsOut3, "<b><i>Excerpt</i></b><br> <br>", "")

		print resultsOut4
	else:
		return 0		
	return 1

if (main()):
	pass
else:
	print "Content-type: text/html\n\n"
	print "<html>\n<head>\n"

	print "\n\n<br><p>There was some sort of error.</p>\n"
	print "\n</body>\n</html>"

