Created
March 8, 2011 20:49
-
-
Save MarkNenadov/861011 to your computer and use it in GitHub Desktop.
Anagram Fetcher (WARNING: obsolete code)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Anagram Fetcher | |
# | |
# NOTE: This is an old, historical script, just posted from my archives for fun. Was | |
# written to a quite old version of Python. Worked at least up to Python 2.3.4, | |
# but no longer works in newer versons such as 2.6 and beyond! It silently fails | |
# in 2.6 and 2.7 and crashes and burns with syntax errors in 3.1 | |
# | |
# This utility generates a list of anagrams out of a word dictionary. The word | |
# dictionary is simply a text file containing a list of words with new lines in | |
# between each one. | |
# | |
# Use it as follows: | |
# python anagram.py dict.txt | |
# | |
# Mark Nenadov - do whatever you want with it | |
import sys, string | |
# returns a signature that will be the same | |
# for a whole class of anagrams | |
def getAnagramSignature( item ): | |
item_chars = [] | |
for i in range(len(item)): | |
item_chars.append(item[i]) | |
item_chars.sort() | |
return string.join(item_chars, "") | |
def stripNewLines( list ): | |
for i in range(len(list)): | |
list[i] = list[i][:-1] | |
return list | |
# sort a list by a given field | |
def sortBy(list, n): | |
nlist = map(lambda x, n=n: (x[n], x), list) | |
nlist.sort() | |
return map(lambda (key, x): x, nlist) | |
# main loop | |
if ( len(sys.argv) != 2 ): | |
print "You must pass one input file to this program. That file" | |
print "should contain lowercase words seperated by new lines." | |
print "Example: python anagram.py dict.txt" | |
sys.exit() | |
inFile = "" | |
try: | |
inFile = open(sys.argv[1], "r" ) | |
except IOError: | |
print "Error opening input file" | |
sys.exit() | |
dict = stripNewLines(inFile.readlines()) | |
newDict = [] | |
for item in dict: | |
newDict.append([item, getAnagramSignature(item)]) | |
newDict = sortBy(newDict, 1) | |
finalDict = {} | |
for item in newDict: | |
if not (finalDict.has_key(item[1])): | |
finalDict[item[1]] = [] | |
finalDict[item[1]].append(item[0]) | |
print "Anagrams Found:" | |
for group in finalDict.values(): | |
if ( len(group) > 1 ): | |
print group |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment