#!/usr/bin/python

"""
Authors are Disad and Bradbase 2004
Issued under a GPL style licence

This script is a _VERY HACKED_ CGI script to assist in the completion of
crossword puzzles.

Our concept is to bring together the technologies of regular expressions, and HTML
to offer options for incomplete words.  Which you can then find the meaning by using
http://word.sytes.org, and hopefully find a solution.

We have decided to use egrep to parse the dictionary word file, for speed
and efficiency.  We have identified that without egrep, we would have to
load the entire word file into RAM, parse it using the Python Regex stuff
for each web request. (not a good practice having the word file floating in
and out of RAM frequently)

Our regex searchstring looks like this:  ^[cC].e.l.p$
Where:
	"^" is the beginning of the word
	"[cC]" allows for caps or no caps
	"." is the place wild card (often in other places a "?" is used)
	"$" is the end of the word
	
Using Gentoo, the word file is in /usr/share/dict/brad.  This is a compilation of the files:
	words (standard linux [Gentoo distro])
	2of12inf.txt (from the 12dicts package, found on the site http://wordlist.sourceforge.net)
	connectivesi (standard linux [Gentoo distro])
	propernouns (standard linux [Gentoo distro])
	
Compiled by concatenating into a common file, and using the command sort -udf on the file.

egrep {searchstring} {file}

egrep {searchstring} /usr/share/dict/brad
"""

# BEGIN

# import the modules to talk to the OS, and CGI
from os import popen3
import cgi

def allOk(word):
	#define variables
	i = 0
	start = "^"
	end = "$"
	#HTMLcontent = "<P>%s</P><BR><P>%s</P><BR><P>%s</P>" % (word.count("."), len(word), word)
	HTMLcontent = ""
	HTMLstartBody = """<BODY onload=window.parent.meaning.location="about:blank">\n<P>%d Word(s) found:</P>"""
	HTMLdic = "http://%s.word.sytes.org"
	HTMLendBody = "</BODY>"

	# find if the first letter is a period"." or a "real" letter
	# If it's a letter, make the no/cap prefix
	# in both cases, create the regex command
	if ( word[0] != "." ):
		letter = ( "[" + word[0].lower() + word[0].upper() + "]" )
		regex = start + letter + word[1:] + end
	else:
		regex = start + word + end

	# make the egrep copmmand
	command = "egrep %s /usr/share/dict/brad" % regex
	
	# open a pipe to the OS, and give it the command.  Also close the Input pipe to the OS.
	comIn, comOut, comErr = popen3(command)
	comIn.close()
	comErr.close()

	# read all the output from the stdout pipe coming from the OS.
	var1 = comOut.readlines()

	# make the links to the found words
	for j in range(len(var1)):
		HTMLcontent = HTMLcontent + '<A HREF="' + HTMLdic % var1[j][:-1] + '">%s</A><BR>\n' % var1[j][:-1]
		i = i + 1

	# start printing the HTML page
	print HTMLstartBody % i
	print HTMLcontent
	print HTMLendBody

def dotsOnly(word):
	#define variables
	i = 0
	start = "^"
	end = "$"
	#HTMLcontent = "<P>%s</P><BR><P>%s</P>" % (word.count("."), len(word))
	HTMLcontent = ""
	HTMLheader = """<BODY onload=window.parent.meaning.location="about:blank">\n<P>%d Word(s) found:</P>"""
	HTMLdic = "http://%s.word.sytes.org"
	HTMLfooter = "</BODY>"

	regex = start + word + end

	# make the egrep copmmand
	command = "egrep %s /usr/share/dict/brad" % regex
	
	# open a pipe to the OS, and give it the command.  Also close the Input pipe to the OS.
	comIn, comOut, comErr = popen3(command)
	comIn.close()
	comErr.close()

	# read all the output from the stdout pipe coming from the OS.
	var1 = comOut.readlines()

	# make the links to the found words
	for j in range(len(var1)):
		HTMLcontent = HTMLcontent + '<A HREF="' + HTMLdic % var1[j][:-1] + '">%s</A><BR>\n' % var1[j][:-1]
		i = i + 1

	# start printing the HTML page
	print HTMLheader % i
	#print HTMLcontent
	print HTMLfooter

def straightWord(word):		
	# define variables
	HTMLredirect1 = """<BODY onload=window.parent.meaning.location="http://%s.word.sytes.org">\n</BODY>"""
	
	print HTMLredirect1 % word

if __name__ == '__main__':
	dots = 0
	HTMLheader = "Content-Type: text/html\n\n<HTML><HEADER><TITLE>Disad's Crossword helper</TITLE><BASE TARGET='meaning'</HEADER>"
	HTMLfooter = "</HTML>"
	# get imput from CGI environment variables
	form = cgi.FieldStorage()
	
	print HTMLheader
	
	try:
		word = form['crossstring'].value
	except KeyError:
		word = "^$"
	
	if word.count(".") > 0 :
		allOk(word)
	elif word == "^$":
		print """<BODY onload=window.parent.meaning.location="about:blank" ><P>You have entered nothing.</P><BR><P>Please Enter a word, or parts thereof</P></BODY>"""
	elif word.find(".") != 1 :
		straightWord(word)
	elif word.count(".") == len(word):
		dotsOnly(word)
	else:
		print "<BODY><P>Sorry, that doesn't work yet</P></BODY>"

	print HTMLfooter
		
# END