Letterwalk.cgi: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "Assumes the presence of a words list (from NLTK?) named words.txt <source lang="python"> #!/usr/bin/python import cgi import cgitb; cgitb.enable() fs = cgi.FieldStorage() ...")
 
m (Michael Murtaugh moved page Letterwalk to Letterwalk.cgi)
(No difference)

Revision as of 15:17, 10 May 2013

Assumes the presence of a words list (from NLTK?) named words.txt

#!/usr/bin/python
 
import cgi
import cgitb; cgitb.enable()

fs = cgi.FieldStorage()
word = fs.getvalue("w", "")

print "Content-type: text/html; charset=utf8"
print
print "<html><head><title>letterwalk</title></head><body>"
print "<h2>%s</h2>" % word

lettercounts = {}
f = open("words.txt", "r")
for w in f:
  w = w.strip()
  if word == "":
    l = w[0]
    lettercounts[l] = lettercounts.get(l, 0) + 1
  else:
    if w.startswith(word):
      if len(w) > len(word):
        l = w[len(word)]
        lettercounts[l] = lettercounts.get(l, 0) + 1

letters = lettercounts.keys()
letters.sort()

(min_count, max_count) = (None, None)
for l in letters:
  if min_count == None or lettercounts[l] < min_count:
    min_count = lettercounts[l]
  if max_count == None or lettercounts[l] > max_count:
    max_count = lettercounts[l]

(min_size, max_size) = (8, 96)
for l in letters:
  if min_count == max_count:
    rank = 1.0
  else:
    rank = float(lettercounts[l] - min_count) / (max_count - min_count)
  size = int(min_size + rank * (max_size - min_size))
  print """<a href="?w=%s"><span style="font-size: %dpx">%s</span></a>""" % (word+l, size, l)

print """<p><a href="?">start over</a></p>"""
print "</body></html>"