Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This is a small programm to solve
- word guess games with given chars and a given wordlength
- Download the dict at: http://extensions.libreoffice.org/extension-center/german-de-de-frami-dictionaries/releases/2013.12.06
- Unzip it (change the extension from .ocx to .zip)
- You'll need the file de_DE_frami/de_DE_frami.dic
- """
- import argparse
- import os
- import sys
- from collections import Counter
- def collect(dictionary, num_chars):
- """
- This function collects from a dict-file each
- line and saves it to a set.
- """
- with open(dictionary, 'r') as fd:
- wordlist = set()
- for word in fd:
- if not word.startswith('#'):
- (word, _, _) = word.decode('ISO8859-1').strip().partition('/')
- if len(word) == num_chars:
- wordlist.add(word)
- return wordlist
- def find_matches(allowed_chars, num_chars, wordlist):
- allowed_chars = Counter(allowed_chars.decode('utf8').lower())
- for word in wordlist:
- c_word = Counter(word.lower())
- #print allowed_chars
- diff = c_word & allowed_chars
- if sum(diff.values()) == num_chars:
- yield word
- if __name__ == '__main__':
- # example:
- # ./woerter.py -d "/home/deadeye/Downloads/de_DE_frami/de_DE_frami.dic" -c "ZDTTXUCEERME" -n6
- parser = argparse.ArgumentParser(description='Find words from allowed chars and length from a dict')
- parser.add_argument('-n', metavar='word_length', type=int, default=[-1],
- help='lenght of the word to find',
- action='store', nargs=1, required=False)
- parser.add_argument('-c', metavar='allowed_chars', type=str,
- help='allowed chars in a word', action='store', nargs=1, required=True)
- parser.add_argument('-d', metavar='dictionary', type=str,
- help='dic file from libre office', action='store', nargs=1, required=True)
- args = parser.parse_args()
- if not os.path.isfile(args.d[0]) or not os.access(args.d[0], os.R_OK):
- print "Dictionary is not accessible"
- sys.exit(1)
- if args.n[0] == -1:
- n = len(args.c[0])
- else:
- n = args.n[0]
- if len(args.c[0]) < args.n[0]:
- print "Required chars are less then the required num of chars"
- sys.exit(2)
- wordlist = collect(args.d[0], n)
- matches = find_matches(args.c[0], n, wordlist)
- for match in matches:
- print match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement