#!/usr/bin/python # # Written 2012-01-26 by Steven J. DeRose. # import sys import os import re from string import * import argparse from sjdUtils import * version = "2012-01-26" ############################################################################### # Process options # parser = argparse.ArgumentParser(version='Version of '+version) parser.add_argument( "--color", action='store_true', help='Colorize the formerely-invisible characters.') parser.add_argument( "-nocolor", dest="color", action='store_false', help='Turn off colorizing.') parser.add_argument( "-minLength", dest="minLength", type=int, default=1, help='Only return words at least this long.') parser.add_argument( "-i", dest='ignoreCase', action='store_true', help='Ignore case distinctions when matching.') parser.add_argument( "-q", dest='quiet', action='store_true') parser.add_argument( "-tickInterval", type=int, default=10000) parser.add_argument( "-verbose", action='count') parser.add_argument( 'source', type=str, help='Word whose characters can be used.') args = parser.parse_args() if (args.verbose>0): print(args) if (os.environ["PYTHONIOENCODING"] != "utf_8"): print("Warning: PYTHONIOENCODING is not utf_8.") su = sjdUtils() cs = "" ce = "" if (args.color): cs = su.getColorString("red") ce = su.getColorString("off") if (args.verbose): print("Turned on " + cs + "color" + ce + ".") ############################################################################### # def isSpellable(word, charsAvailable): if (not word): return(0) global args if (args.verbose>0): print("Testing word '" + word + "' in '" + charsAvailable + "'") if (args.ignoreCase): word = upper(word) charsAvailable = upper(charsAvailable) for c in (word): if (args.verbose>2): print(" c is '" + c + "', remaining: '" + charsAvailable + "'") try: loc = str.index(charsAvailable,c) except: if (args.verbose>0): print(" Missing " + c) return(0) charsAvailable = (charsAvailable[0:loc] + charsAvailable[loc+1:]) return(1) ############################################################################### # Main # wordCount = 0 recnum = 0 # Grab user's word # if (not args.source): print("No source word specified.\n") exit(0) print("Looking for words you can make from letters in '" + args.source + "', minimum length " +`args.minLength`) # Load the dictionary # dictPath = "/usr/share/dict/words" df = open(dictPath, "r") word = df.readline() while(word): recnum += 1 word = word.rstrip().lstrip() #print(word + ": " + `len(word)`) if (isSpellable(word, args.source)): if (len(word) > args.minLength): print("*** " + word) wordCount += 1 word = df.readline() df.close() if (not args.quiet): print("Done, " + `wordCount` + " words found, of " + `recnum` + " checked.") sys.exit(0) ############################################################################### ############################################################################### ############################################################################### # perldoc = """ =pod =head1 Usage findWordsWithin.py Find all the words know to the system dictionary, that use only a subset of the letters given. =head1 Options =over =item * B<-i> Ignore case when matching. =back =head1 Known bugs and limitations =head1 Ownership This work by Steven J. DeRose is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. For further information on this license, see L. The author's present email is sderose at acm.org. For the most recent version, see L. =cut """