The Nanagram - Sea-Gee-Eye

By Fuzzyman

Jumbling Words for Fun and Profit

What Is Nanagram

Nanagram is a Python script that finds anagrams of words.
It uses a 'built-in' dictionary of 17890 words. The main 'loop' that finds the anagrams is only 23 lines of code !! There is also a desktop version of Nanagram (Nanagram - Gooey) that has a much larger dictionary included with it.

Using Nanagram is dead easy - type the word(s) you want an anagram of and hit enter ! For example, did you know that an anagram of George W Bush is 'whose bugger'.....

You can set the minimum number of letters that each word should have and any words that you want the anagram to contain. Both these options are useful for reducing the number of anagrams that Nanagram finds - when it finds hundreds it can be difficult to pick out the really funny ones. For the CGI-version I've set the maximum number of lettters that Nanagram will let you enter to 25 - but in practise I've found that any more than about 15 and Nanagram slows to a crawl.

If you want to see it in practise then go to Nanagram-CGI. You can download the desktop 'gooey' version at - Nanagram Home Page.

For those of you who wonder about such things - The CGI in Nanagram Sea-Gee-Eye refers to the 'Common Gateway Interface' and is the way that web-browsers and servers communicate with programs like Nanagram. This page here is mainly for people who want to download Nanagram-CGI to include it in their websites.

Anyway - I hope you enjoy Nanagram.

Installing Nanagram CGI

If you want a version of Nanagram-CGI to include in your own website then the main thing you need is a server with python installed. Python comes with most Linux installations, but is less common on Microsoft servers. Nanagram is actually quite a basic CGI script (I wrote it to learn about CGI)- so once you've overcome this hurdle you're most of the way there. Even if you're not a python programmer the scripts can easily be customised with any text editor, to be included as part of any website that supports Python CGIs.

When you get the it will contain the following important files (plus a few not so important ones probably) :

You'll need to do the usual things for CGI - like make sure that the first line of is set correctly for the server. It will usually need to be something like '#!/usr/bin/python' or '#!/usr/bin env python'. You will also have to change (chmod) the permissions of the python scripts to '755' (executable by everyone). It's also possible you might have to rename the main script to end with '.cgi' and it might need to be installed into the 'cgi-bin' directory. The last thing is to make sure that the script is uploaded to your server in 'ascii' mode rather than binary - this ensures that the line ends are properly converted when uploading from a windows machine to a Linux server. If you are using something like cPanel to put the files onto the server then cutting and pasting into a blank file will ensure the same thing... (Unfortunately at the time of writing cPanel has a bug that prevents you from correctly changing the permissions of the files - I use a web based FTP client to get round this. If this affects you then e-mail me and I'll explain how to use it). I also have a simple little script that will do the DOS to UNIX conversion before uploading if anyone needs it.

Nanagram-CGI uses HTML templates to generate it's output. This means it can be easily incorporated into any website. All of these templates are contained in a file called '' which can be edited with any text editor. This has various variables containing the HTML in sections which is output by the script. You choose the look of the output page produced by Nanagram-GUI by inserting your own HTML into these variables (everything between the triple-quotations). There is a note by each one explaining where each one is used - but it's only basic HTML and quite straightforward. The first entry in this file though is 'scripturl' which must be set to the actual URL that you are installing the script to. If this is incorrectly set then results produced by the script will direct your users to my site rather than keeping them on your own !

If you've never used HTML forms before then it's probably best to leave the form that collects the information from the user as it is - this is the variable called 'entrytable' in If you do modify it then you must include my 'credits' somewhere in, or near it. This is the section :

<B><A HREF= target=_blank>Nanagram</A> the word jumbler</B> is written and maintained by <I><A HREF= target=_blank>Fuzzyman</A></I>, using <B><A HREF= target=_blank>Python</A></B>.

When displayed it looks like this :

Nanagram the word jumbler is written and maintained by Fuzzyman, using Python.

(By the way it is the 'maxlength=25' in entrytable that sets the maximum number of characters you can pass to Nanagram-CGI to be 25. Editing this value changes it).

If the CGI is executed without parameters (or the user hits enter or submit without typing any words in) - then just a welcome type message will be displayed. This is the value 'blankout' in This means you don't have to include a separate HTML page to launch the script - just include a link straight to the script and when it is called it will display the welcome message with the entry form for the anagrams.

The last place you might want to modify is the part of the code that actually prints the anagrams as it finds them. You will only be able to safely modify this if you understand the basics of Python programming though. It prints four spaces between each word and then a '<BR>' after the last one. It's the section of code in the function findgramsCGI that looks like this :

                if len(testword) == 0:
                    if containing: print containing + '&nbsp;&nbsp;&nbsp;&nbsp;',
                    for thing in wordssofar:
                        print thing+'&nbsp;&nbsp;&nbsp;&nbsp;',                    
                    print word+'<BR>'

Thanks to a little bit of magic called 'flushing the output buffer' - Nanagram-CGI now displays the results as it finds them, rather than making you wait for the script to finish executing...... Hurrah ! (and thanks to the folk at comp.lang.python for helping me).


Python Source with Wordlist (71k) :

Nanagram-CGI 1.0 .zip

The Dictionary

The wordlist included is of the same sort used by Nanagram-GUI and from the same source. You could replace it with any from that package, including the extra dictionaries - but using longer dictionaries is a much bigger drain on your server and takes longer to produce results. The 17 000 word list is a good compromise between speed and including common words. To use a different wordlist - just replace 'smalldict.txt' with a different file.

I found the dictionary on the internet (it's nothing more than a long wordlist with every word on a seperate line). It comes from and it includes the following license information as well as the GPL license (which should be included with this file but only applies to the wordlist - Nanagram itself is issued under a slightly different license). The frequency classifcation has been removed from the wordlist I use - but it came from this source.

    UK English Wordlist With Frequency Classification
    Version 1.01 February 2003
    Copyright (C) 2003 Brian Kelk

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

Version 1.0.2
email : michael AT foord DOT me DOT uk

Version Log

Version 1.0.2     10th June 2004
Learned about the CHECKED keyword in HTML !!

Version 1.0.1     10th Feb 2004
Moved the HTML sections in to a separate template for easy editing.
Added the welcome message when the script is called without parameters. This removes the need for a separate HTML page to launch it.
Added 'output buffer flushing' so that it feeds results to the browser as it finds them, rather than make you wait until it has found them all.

Nanagram-CGI (and it's Python source code) is available for non-commercial use by anyone who might want it, without fee. No warranty as to it's correctness, fitness for purpose or anything else is made - use at your own risk :-)
The home page for this program - where it can be downloaded from and any news/updates can be found - is Nanagram-CGI Home Page. Any python modules arising out of the project will be available at the Python-Utils Home Page. If you want to be kept up to date about changes and improvements to Nanagram-CGI then send me an e-mail, alternatively if you have any questions or comments (or bug reports) feel free to contact me - Michael Foord.
(If you let me know any places you use my scripts I can include a link back to you as examples).

Back To Top