Letterwalk.cgi: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
Assumes the presence of a words list (from NLTK?) named words.txt
[[File:Letterwalk.png|link=http://pzwart3.wdka.hro.nl/~mmurtaugh/cgi-bin/letterwalk/letterwalk.cgi]]
 
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


<source lang="python">
<source lang="python">
Line 51: Line 53:
</source>
</source>


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


[[Category: Python]]
[[Category: Python]]

Latest revision as of 13:00, 14 May 2013

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