Advertisement
DeaD_EyE

guessing german words with fixed length and given chars

Feb 8th, 2015
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.45 KB | None | 0 0
  1. """
  2. This is a small programm to solve
  3. word guess games with given chars and a given wordlength
  4.  
  5. Download the dict at: http://extensions.libreoffice.org/extension-center/german-de-de-frami-dictionaries/releases/2013.12.06
  6. Unzip it (change the extension from .ocx to .zip)
  7. You'll need the file de_DE_frami/de_DE_frami.dic
  8. """
  9.  
  10. import argparse
  11. import os
  12. import sys
  13. from collections import Counter
  14.  
  15. def collect(dictionary, num_chars):
  16.     """
  17.    This function collects from a dict-file each
  18.    line and saves it to a set.
  19.    """
  20.     with open(dictionary, 'r') as fd:
  21.         wordlist = set()
  22.         for word in fd:
  23.             if not word.startswith('#'):
  24.                 (word, _, _) = word.decode('ISO8859-1').strip().partition('/')
  25.                 if len(word) == num_chars:
  26.                     wordlist.add(word)
  27.     return wordlist
  28.        
  29.  
  30. def find_matches(allowed_chars, num_chars, wordlist):
  31.     allowed_chars = Counter(allowed_chars.decode('utf8').lower())
  32.     for word in wordlist:
  33.         c_word = Counter(word.lower())
  34.         #print allowed_chars
  35.         diff = c_word & allowed_chars
  36.         if sum(diff.values()) == num_chars:
  37.             yield word
  38.  
  39. if __name__ == '__main__':
  40.     # example:
  41.     # ./woerter.py -d "/home/deadeye/Downloads/de_DE_frami/de_DE_frami.dic" -c "ZDTTXUCEERME" -n6
  42.  
  43.     parser = argparse.ArgumentParser(description='Find words from allowed chars and length from a dict')
  44.     parser.add_argument('-n', metavar='word_length', type=int, default=[-1],
  45.                      help='lenght of the word to find',
  46.                      action='store', nargs=1, required=False)
  47.     parser.add_argument('-c', metavar='allowed_chars', type=str,
  48.                         help='allowed chars in a word', action='store', nargs=1, required=True)
  49.     parser.add_argument('-d', metavar='dictionary', type=str,
  50.                         help='dic file from libre office', action='store', nargs=1, required=True)
  51.     args = parser.parse_args()
  52.     if not os.path.isfile(args.d[0]) or not os.access(args.d[0], os.R_OK):
  53.         print "Dictionary is not accessible"
  54.         sys.exit(1)
  55.     if args.n[0] == -1:
  56.         n = len(args.c[0])
  57.     else:
  58.         n = args.n[0]
  59.     if len(args.c[0]) < args.n[0]:
  60.         print "Required chars are less then the required num of chars"
  61.         sys.exit(2)
  62.     wordlist = collect(args.d[0], n)
  63.     matches = find_matches(args.c[0], n, wordlist)
  64.     for match in matches:
  65.         print match
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement