Letterwalk.cgi

From XPUB & Lens-Based wiki
Revision as of 13:00, 14 May 2013 by Michael Murtaugh (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Letterwalk.png

A script that navigates through a text file containing around 100000 most common words in the English language (part of the NLTK package) 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>"

See live: http://pzwart3.wdka.hro.nl/~mmurtaugh/cgi-bin/letterwalk/letterwalk.cgi