Letterwalk.cgi: Difference between revisions
No edit summary |
No edit summary |
||
Line 51: | Line 51: | ||
</source> | </source> | ||
See live: http://automatist.org/poc/letterwalk/letterwalk.cgi | See live: http://automatist.org/cgi-bin/poc/letterwalk/letterwalk.cgi | ||
[[Category: Python]] | [[Category: Python]] |
Revision as of 14:19, 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>"
See live: http://automatist.org/cgi-bin/poc/letterwalk/letterwalk.cgi