Last active
December 15, 2015 08:09
-
-
Save martin-wintz/5228765 to your computer and use it in GitHub Desktop.
Little word puzzle solver: save as python script and run with -h for usage
This file contains hidden or 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
import re | |
import argparse | |
def getSortedLetterList(wordList): | |
# Calculate the frequency with which characters appears in the dictionary | |
letterCount = {} | |
totalCount = 0 | |
for word in wordList: | |
word = word.lower() | |
word = re.sub('\W', '', word) | |
for letter in word: | |
totalCount += 1 | |
if letter in letterCount.keys(): | |
letterCount[letter] += 1 | |
else: | |
letterCount[letter] = 1 | |
letterFrequency = {} | |
for letter in letterCount: | |
letterFrequency[letter] = letterCount[letter] / float(totalCount) | |
for letter in sorted(letterCount, key=letterFrequency.get): | |
print letter + ": " + str(letterFrequency[letter]) | |
return sorted(letterCount, key=letterFrequency.get) | |
def findUncommonWords(allowedLetterRank = 15, filename = '2of4brif.txt'): | |
wordList = open(filename).read().splitlines() | |
letterList = getSortedLetterList(wordList) | |
# Look for words including the top "allowedLetterRank" uncommon letters | |
regex = re.compile('^[' + ''.join(letterList[0:allowedLetterRank]) + ']+$') | |
for word in wordList: | |
if regex.match(word): | |
print word | |
def main(): | |
parser = argparse.ArgumentParser(description="Find words with only uncommon letters." + | |
" Specify the range of letters to use (starting from the least common letter)" + | |
" using -r. Lists the frequency rankings of the letters first then the words found.") | |
parser.add_argument("-r", "--rank", type=int, default=15, | |
help="Find words within this range of uncommon letters") | |
parser.add_argument("-f", "--file", default="2of4brif.txt", | |
help="Filename of dictionary (must be in the same directory)") | |
args = vars(parser.parse_args()) | |
rank = args["rank"] | |
filename = args["file"] | |
findUncommonWords(rank, filename) | |
if __name__ == '__main__': | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Find 2of4brif.txt here: http://downloads.sourceforge.net/wordlist/12dicts-5.0.zip